我试图修改 Teunis van Beelen 的 Rs232 库,从轮询到事件驱动和非重叠以适合我的项目。RS232 库
我希望每 200 毫秒接收一次数据块(大约 100 到 200 个字符)。
我遇到的问题是接收到的数据非常不一致,它在随机点被切断,并且不完整。
我希望 ReadFile() 仅在读取一整块数据后返回。(或类似的东西)
我觉得问题出在超时设置上,因为通过更改数字我得到了不同的结果,但我就是不能正确,到目前为止,我最好的结果已将所有超时值设置为 0 并让 ReadFile() 期待150 字节,这样 ReadFile() 不会返回,除非它读取 150 个字符,但这只是在几次传输后不同步,因为我不知道会有多少数据。
这些是Teunis代码中轮询功能的主要变化,除了超时设置,所有其他设置都没有改变:
//Using the EV_RXCHAR flag will notify the thread that a byte arrived at the port
DWORD dwError = 0;
//use SetCommMask and WaitCommEvent to see if byte has arrived at the port
//SetCommMask sets the desired events that cause a notification.
if(!SetCommMask(Cport[comport_number],EV_RXCHAR)){
printf("SetCommMask Error");
dwError = GetLastError();
// Error setting com mask
return FALSE;
}
//WaitCommEvent function detects the occurrence of the events.
DWORD dwCommEvent;
for( ; ; )
{
//wait for event to happen
if (WaitCommEvent(Cport[comport_number],&dwCommEvent,NULL))
{
if(ReadFile(Cport[comport_number], buf, 1, (LPDWORD)((void *)&n), NULL)){
//Byte has been read, buf is processed in main
}
else{
//error occoured in ReadFile call
dwError = GetLastError();
break;
}
else{
//error in WaitCommEvent
break;
}
break; //break after read file
}
正如MSDN 文章在串行 com 上使用 Do While 循环遍历缓冲区中的每个字符所建议的尝试 2 ,这种方法也没有产生任何好的结果。
DWORD dwError = 0;
/*
Using the EV_RXCHAR flag will notify the thread that a byte arrived at the port
*/
//use SetCommMask and WaitCommEvent to see if byte has arrived at the port
//SetCommMask sets the desired events that cause a notification.
if(!SetCommMask(Cport[comport_number],EV_RXCHAR)){
printf("SetCommMask Error");
dwError = GetLastError();
// Error setting com mask
return FALSE;
}
//WaitCommEvent function detects the occurrence of the events.
DWORD dwCommEvent;
for( ; ; )
{
//wait for event to happen
if (WaitCommEvent(Cport[comport_number],&dwCommEvent,NULL))
{
//Do while loop will cycle ReadFile until bytes-read reach 0,
do{
if(ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL)){
//Byte has been read, buf is processed in main
}
else{
//error occoured in ReadFile call
dwError = GetLastError();
break;
}
}while(n);
}
else{
//error in WaitCommEvent
break;
}
break; //break after read file
}
我想知道以重叠模式重写代码是否会有所改善,但我没有看到优势,因为我不需要多线程。任何建议都会很棒!
谢谢你。