//进入临界区
EnterCriticalSection(&pSerialComm->m_CriticalSection);
if (bWrite)
{
pSerialComm->m_Overlapped.Offset = 0;
pSerialComm->m_Overlapped.OffsetHigh = 0;
//输出数据到端口
bResult = WriteFile(pSerialComm->m_hComm, // Handle to COMM Port
pWriteBuffer, // Pointer to message buffer in calling finction
dwCount, // Length of message to send
&BytesSent, // Where to store the number of bytes sent
&pSerialComm->m_Overlapped); // Overlapped structure
if (!bWrite)
{
bWrite = TRUE;
bResult = GetOverlappedResult(pSerialComm->m_hComm, // Handle to COMM port
&pSerialComm->m_Overlapped, // Overlapped structure
&BytesSent, // Stores number of bytes sent
TRUE); // Wait flag
//建立并获取端口句柄
m_hComm = CreateFile(szPort, // communication port string (COMX)
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED, // Async I/O
0); // template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE) return FALSE;
//设置超时延迟
if (SetCommTimeouts(m_hComm, &m_CommTimeOuts))
{
//指定启动事件
if (SetCommMask(m_hComm, m_dwCommEvents))
{
//获取端口状态
if (GetCommState(m_hComm, &m_DCB))
{
m_DCB.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!