用Builder C++如何对串行口COM1或COM2进行读写操作

一个通讯类（对串口的读写），可以不用做修改就用在程序中
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
#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 o