?? serialport.cpp
字號:
// SerialPort.cpp: implementation of the CSerialPort class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SerialPort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSerialPort::CSerialPort()
{
m_portHandle=INVALID_HANDLE_VALUE;
m_lBaudRate=CBR_57600;
}
CSerialPort::~CSerialPort()
{
if(IsOpen())
ClosePort();
}
void CSerialPort::monitor_data()
{
DWORD thread_id;
monitor_data_thread_handle = CreateThread(0, 0, CSerialPort::monitor_data_thread, this, 0, &thread_id);
}
DWORD WINAPI CSerialPort::monitor_data_thread(LPVOID arg)
{
CSerialPort *pSerialPort;
pSerialPort = (CSerialPort *) arg;
pSerialPort->monitor_data_thread_terminate = false;
bool done;
BOOL success;
DWORD bytes;
BYTE buf[1024];
while (!pSerialPort->monitor_data_thread_terminate)
{
success = ReadFile (pSerialPort->m_portHandle, buf, 1024, &bytes,NULL);
done = success != 0;
if (!done)
{
DWORD rxgle = GetLastError ();
ASSERT (rxgle == ERROR_IO_PENDING);
}
else
{
if (pSerialPort->data_func && !pSerialPort->monitor_data_thread_terminate)
{
pSerialPort->data_func (buf, bytes, pSerialPort->data_func_context);
}
}
}
TRACE("CSerialPort::monitor_data_thread exit\n");
return 0;
}
int CSerialPort::WriteData(LPBYTE data, int dwLength)
{
bool done;
BOOL success;
DWORD bytes;
if (dwLength == 0) /* Be pessimistic and assume WriteFile would choke on zero length */
{
return 0;
}
else
{
success = WriteFile (m_portHandle, data, dwLength, &bytes,NULL);
done = success != 0;
if (!done)
{
DWORD txgle = GetLastError ();
ASSERT (txgle == ERROR_IO_PENDING);
return 0;
}
}
return bytes;
}
BOOL CSerialPort::OpenPort(int PortNum)
{
DCB dcb;
BOOL success;
COMMTIMEOUTS timeouts;
// TODO: Add your dispatch handler code here
CString portName;
portName.Format(_T("com%d:"), PortNum);
#define WIN32_NO_SHARING 0
#define WIN32_NO_TEMPLATE_FILE 0
m_portHandle = CreateFile (portName,
GENERIC_READ | GENERIC_WRITE,
WIN32_NO_SHARING, // mandatory
0,
OPEN_EXISTING, // mandatory
0,//FILE_FLAG_OVERLAPPED,
WIN32_NO_TEMPLATE_FILE // mandatory
);
if (m_portHandle == INVALID_HANDLE_VALUE)
{
// CString msg;
// msg.Format(_T("Error %d"),GetLastError());
// MessageBox(msg);
return FALSE;
}
memset (&dcb, 0, sizeof (DCB));
dcb.DCBlength = sizeof (DCB);
//success = BuildCommDCB (cfgStr, &dcb); /* Note BuildCommDCB() turns off both hardware and software flow control */
dcb.BaudRate=m_lBaudRate;
dcb.DCBlength=sizeof(dcb);
dcb.Parity=NOPARITY;
dcb.ByteSize=8;
dcb.StopBits =ONESTOPBIT;
dcb.fBinary = 1 ;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = 0;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fOutxDsrFlow=0;
dcb.fDsrSensitivity=0;
dcb.fTXContinueOnXoff=1;
dcb.fOutX = 0;
dcb.fInX = 0;
// dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
// dcb.fOutxCtsFlow = 1;
// dcb.fOutX = 1;
// dcb.fInX = 1;
success = SetCommState (m_portHandle, &dcb);
ASSERT ((_T("SetCommState"), success));
if (!success)
{
CloseHandle (m_portHandle);
m_portHandle=INVALID_HANDLE_VALUE;
// MessageBox(_T("SetCommState Error"));
return FALSE;
}
timeouts.ReadIntervalTimeout = 10;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadTotalTimeoutConstant = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
success = SetCommTimeouts (m_portHandle, &timeouts);
ASSERT ((_T("SetCommTimeouts"), success));
if (!success)
{
CloseHandle (m_portHandle);
m_portHandle=INVALID_HANDLE_VALUE;
// MessageBox(_T("SetCommTimeouts Error"));
return FALSE;
}
monitor_data();
return TRUE;
}
void CSerialPort::set_data_callback(void (*func)(void *,unsigned long,void *), void *context)
{
ASSERT(func!=NULL);
data_func=func;
data_func_context=context;
}
void CSerialPort::ClosePort()
{
if (monitor_data_thread_handle != INVALID_HANDLE_VALUE)
{
monitor_data_thread_terminate = true;
}
Sleep(0);
if (m_portHandle != INVALID_HANDLE_VALUE)
{
CloseHandle (m_portHandle);
}
m_portHandle = INVALID_HANDLE_VALUE;
return ;
}
BOOL CSerialPort::IsOpen()
{
return m_portHandle!=INVALID_HANDLE_VALUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -