解决CserialPort类串口程序中WriteToPort只能发送一次问题

来源:互联网 发布:淘宝零食店铺哪里进货 编辑:程序博客网 时间:2024/06/05 10:11

现象:每次打开串口只能发送一次,需要关闭再打开或者接收完数据才能发送
成因:在自带串口的电脑、或用优质的USB串口线都没有出现改问题,很有可能跟串口线的质量有关,
调试发现在调用玩WriteToPort函数 SetEvent(m_hWriteEvent);后第一次能够正常进入监视线程(CommThread(LPVOID pParam))
调用WriteChar(port),然之后,一直进入ReceiveChar(port, comstat),再次发送SetEvent(m_hWriteEvent)也无法去到WriteChar(port)。

分析:某种原因导致一直存在串口接收消息,而(WaitForMultipleObjects)函数在等待事件具有优先级判断,InitPort()函数中
m_hEventArray[1] = m_ov.hEvent; m_hEventArray[2] = m_hWriteEvent;m_ov.hEvent事件(包含接收事件),写串口事件m_hWriteEvent;优先级更高
所以在出现一直存在接收事件时,无法发送数据;

Event = WaitForMultipleObjects(3,port->m_hEventArray,FALSE,INFINITE);

解决:
解决的思路是将写串口事件(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;
......
}
阅读全文
0 0
原创粉丝点击