如何修复通过串行通信接收数据丢失的问题? [英] How do I fix to receive loss of data by serial communications?

查看:131
本文介绍了如何修复通过串行通信接收数据丢失的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi, everyone.

I have a serious problem in my VC++ code of Serial Communcations between PCs.

Two PCs are connected 2 serial ports,

A PC is sending data via 2 serial ports, B PC is receiving data from A.

A PC run sendData.exe with 2 sending threads as sending_thread1, sending_thread2.
B PC run recvData.exe with 2 receiving threads as receiving_thread1, receiving_thread2.

If sending_thread1 send data to serial port1 then receiving_thread1 receive each data, as do thread2.

Under 9600 bps, everything is ok.
Each sending and receing data by threads has no problem.

But under 115200 bps, almost everytime, there are very confused problem like as follows.

1. Sending threads send data to serial ports at the almost same time, receiving_thread1 has data with no problem.
   But when receiving_thread2 cannot receive whole body of data from serial port 2 as if some data missing.

2. For example: sending_thread2 send data like STX + Type(char[2]) + Sequence(interger) + Length_of_Data[char[6]) + Data(char[N]) + ETX
   Data format of receiving_thread2 shows STX + Type(char[1]) + Length_of_data[char[3]) + Data(char[?]).....

3. In receive code, I use polling type style code (read one byte and accumlating)  or use EventMask of EV_RXCHAR and WaitCommEvent(), but the results are same.

4. In sending code, I have interval 1 seconds between sending data function of sending_thread, but no effect.

5. The main point of my testings is that under 115200 bps, one of receiving_thread receive loss data packet.

In addition, sometimes, the serial commnication using only 1 port show same problem of data loss.

Please let me know the cause of this and solutions....

Thank you in advance.





我尝试了什么:



这个问题浪费了1周。



What I have tried:

1 week wasted for this problem.

推荐答案

您应该检查是否有溢出错误。要执行此操作,请调用 ClearCommError函数(Windows) [ ^



如果你遇到溢出错误,你应该用 SetupComm功能(Windows) [ ^ ]。缓冲区应足够大,至少可以保持最大值。数据行的大小。



您还可以改进代码以使用事件驱动的重叠IO并一次读取所有可用数据。在 EV_RXCHAR 事件调用 ClearCommError 后检查错误并从 cbInQue 传递的 COMSTAT 结构的成员。
You should check if you have overrun errors. To do this call the ClearCommError function (Windows)[^].

If you got overrun errors you should increase the size of the driver's receive buffer with the SetupComm function (Windows)[^]. The buffer should be large enough to hold at least the max. size of a data line.

You might also improve your code to use event driven overlapped IO and read all available data at once. Upon the EV_RXCHAR event call ClearCommError to check for errors and get the number of available bytes from the cbInQue member of the passed COMSTAT structure.


这篇关于如何修复通过串行通信接收数据丢失的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆