TSerialPort.h
#ifndef __SERIALPORT_H__ #define __SERIALPORT_H__
#define WM_COMM_BREAK_DETECTED WM_USER 1 // A break was detected on input. #define WM_COMM_CTS_DETECTED WM_USER 2 // The CTS (clear-to-sennd) sig nal changed state. #define WM_COMM_DSR_DETECTED WM_USER 3 // The DSR (data-set-reaady) si gnal changed state. #define WM_COMM_ERR_DETECTED WM_USER 4 // A line-status error ooccurre d. Line-status errors are CE_FRAME, CE_OVERRUN, and CE_RXPARITY.
#define WM_COMM_RING_DETECTED WM_USER 5 // A ring indicator was etec ted. #define WM_COMM_RLSD_DETECTED WM_USER 6 // The RLSD (receive-lin -sig nal-detect) signal changed state. #define WM_COMM_RXCHAR WM_USER 7 // A character w s received and pl aced in the input buffer. #define WM_COMM_RXFLAG_DETECTED WM_USER 8 // The event character w s received and placed in the input buffer. #define WM_COMM_TXEMPTY_DETECTED WM_USER 9 // The last character in th e output buffer was sent.
class TSerialPort { public: // contruction and destruction TSerialPort(); virtual ~TSerialPort();
// port initialisation BOOL InitPort(TForm* pPortOwner, UINT portnr = 1, UINT baud = 19200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCom mEvents = EV_RXCHAR | EV_CTS, UINT nBufferSize = 512);
// start/stop comm watching BOOL StartMonitoring(); BOOL RestartMonitoring(); BOOL StopMonitoring();
DWORD GetWriteBufferSize(); DWORD GetCommEvents(); DCB GetDCB();
void WriteToPort(char* string);
protected: // protected memberfunctions void ProcessErrorMessage(char* ErrorText); static DWORD _stdcall CommThread(LPVOID pParam); static void ReceiveChar(TSerialPort* port, COMSTAT comstat); static void WriteChar(TSerialPort* port);
// thread HANDLE m_HThread;
// synchronisation objects CRITICAL_SECTION m_csCommunicationSync; BOOL m_bThreadAlive;
// handles HANDLE m_hShutdownEvent; HANDLE m_hComm; HANDLE m_hWriteEvent;
// Event array. // One element is used for each event. There are two event handles fo r each port. // A Write event and a receive character event which is located in th e overlapped structure (m_ov.hEvent). // There is a general shutdown when the port is closed. HANDLE m_hEventArray[3];
// structures OVERLAPPED m_ov; COMMTIMEOUTS m_CommTimeouts; DCB m_dcb;
// owner window TForm* m_pOwner;
// misc UINT m_nPortNr; char* m_szWriteBuffer; DWORD m_dwCommEvents; DWORD m_nWriteBufferSize; };
#endif __SERIALPORT_H__
TSerialPort.cpp
#include #pragma hdrstop #include "SerialPort.h" #include #include #pragma package(smart_init) // // Constructor // TSerialPort::TSerialPort() { m_hComm = NULL;
// initialize overlapped structure members to zero m_ov.Offset = 0; m_ov.OffsetHigh = 0;
// create events m_ov.hEvent = NULL; m_hWriteEvent = NULL; m_hShutdownEvent = NULL;
m_szWriteBuffer = NULL;
m_bThreadAlive = false; }
// // Delete dynamic memory // TSerialPort::~TSerialPort() { do { SetEvent(m_hShutdownEvent); } while (m_bThreadAlive);
delete [] m_szWriteBuffer; }
// // Initialize the port. This can be port 1 to 4. // BOOL TSerialPort::InitPort(TForm* pPortOwner, // the owner (CWnd) of t he port (receives message) UINT portnr, / portnumber (1..4) UINT baud, / baudrate char parity, / parity UINT databits, / databits UINT stopbits, / stopbits DWORD dwCommEvents, // EV_RX HAR, EV_CTS etc UINT writebuffersize) / size to the writebuffer { assert(portnr > 0 && portnr < 5); assert(pPortOwner != NULL);
// if the thread is alive: Kill if (m_bThreadAlive) { do { SetEvent(m_hShutdownEvent); } while (m_bThreadAlive); }
// create events if (m_ov.hEvent != NULL) ResetEvent(m_ov.hEvent); m_ov.hEvent = CreateEvent(NULL, true, false, NULL);
if (m_hWriteEvent != NULL) ResetEvent(m_hWriteEvent); m_hWriteEvent = CreateEvent(NULL, true, false, NULL);
if (m_hShutdownEvent != NULL) ResetEvent(m_hShutdownEvent); m_hShutdownEvent = CreateEvent(NULL, true, false, NULL);
// initialize the event objects m_hEventArray[0] = m_hShutdownEvent; // highest priority m_hEventArray[1] = m_ov.hEvent; m_hEventArray[2] = m_hWriteEvent;
// initialize critical section InitializeCriticalSection(&m_csCommunicationSync);
// set buffersize for writing and save the owner m_pOwner = pPortOwner;
if (m_szWriteBuffer != NULL) delete [] m_szWriteBuffer; m_szWriteBuffer = new char[writebuffersize];
m_nPortNr = portnr;
m_nWriteBufferSize = writebuffersize; m_dwCommEvents = dwCommEvents;
BOOL bResult = false; char *szPort = new char[50]; char *szBaud = new char[50];
// now it critical! EnterCriticalSection(&m_csCommunicationSync);
// if the port is already opened: close it if (m_hComm != NULL) {
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
|