用CserialPort类写的串口程序,为何只能发送一次?
解决方法:
解决的思路是将写串口事件(m_hWriteEvent)的优先级高于接收事件(m_ov.hEvent),如下:
BOOL CSerialPort::InitPort(......){
......
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_hWriteEvent;
m_hEventArray[2] = m_ov.hEvent;
......
}
UINT CSerialPort::CommThread(LPVOID pParam){
......
switch (Event)
{
case 0:
{
......
}
case 2:
{
memset(&comstat, 0, sizeof(COMSTAT));
GetCommMask(port->m_hComm, &CommEvent);
if (CommEvent & EV_RXCHAR)//接收到字符,并置于输入缓冲区中
// Receive character event from port.
ReceiveChar(port, comstat);
......
break;
}
case 1: /// write event发送数据
{
// Write character event from port
WriteChar(port);
break;
}
default:
{
......
}
......
}