Click here to Skip to main content
15,850,750 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.


What I have tried:

1 week wasted for this problem.
Posted
Updated 17-Oct-17 21:52pm
v2
Comments
Patrice T 18-Oct-17 1:13am    
And you have some code ?
Member 12330615 20-Oct-17 10:35am    
I would like to know the last parameter of the following

ReadFile(hComm, &rx, 1, dwBytesTransferred, X); // <--- X

I guess that "X = null" mean waiting data from the serial port.
But I don't know the meaning of "X != null" and the usage of examples.

Please let me know the purpose of this and advise me the full usage of example as "X != null"

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900