d061.bmp.txt

来自「Visual C++通信编程工程实例精解(附盘)」· 文本 代码 · 共 41 行

TXT
41
字号
else
l
else
)
else
; // normal operation... continue
 ProcessErrorMessage( " SetCommS tate() ");
 ProcessErrorMessage( " BuildCommDCB O ");
}
else
                      ProcessErrorMessage( " SetCommMask0 ");
   }
ProcessErrorMessage( " SetCommTimeouts0 ") ;
配置好串口参数后,应用PurgeComm函数终止读写并清空接收与发送缓冲区:
            // flush the port
                  PurgeComm(m_hComm, PURGE_RXCLEAR I PURGE  TXCLEAR I PURGE_RXABORT I PURGE_
TXABORT);
    4)建立读数据的监视线程。
    一般我们是在主线程中写数据,因为写操作是可以控制的,而读的时候我们不知道数据
什么时候会到,所以要建立一个线程专门用来读数据。在这个线程中循环地用ReadFile读串
口,同时用WaitCommEvent检测线路状态。由于线程只有拥有Critical Section对象才能访问
数据,而且在一个时刻有多个线程访问数据。以下是该都分代码:
UINT CSeriaIPort::CommThread(LPVOID pParam)
{
           // Cast the void pointer passed to the thread back to a pointer of CSerialPort class
         CSeriaIPort *port = (CSerialPort*)pParam;
           // Set the status variable in the dialog class to TRUE to indicate the thread is running
        port->m bThreadAlive = TRUE;
// Misc. variables
DWORD BytesTransfered = 0;
DWORD Event = 0;
DWORD CommEvent = 0;
DWORD dwError = 0;
COMSTAT comstat;
BOOL   bResult = TRUE;
                    // Clear comm buffers at stmtup
                       if (port->m_hComm)                // check if the port is opened
                    PurgeComm(port->m_hComm, PURGE_RXCLEAR I PURGE_TXCLEAR I PURGE_RXABORT
PURGE_TXABORT);
61

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?