Re: Serial COM port communication problems
- From: "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
- Date: Sun, 20 May 2007 21:21:50 -0400
an0047@xxxxxxxxx wrote:
Hi,
Working on a application that does extensive Serial I/O and using MSDN
functions as WaitCommEvent(). CreateFile is used to open the com
port, and use ReadFile() and WriteFile() to read and write to the
serial port.
The serial port receives char packets of different sizes, each packet
is divided by different size sub-packets, each of those sub packets
contains a CR char that signals it's (sub-package) end. The end of the
hole package is embedded only on the interpretation of the last sub-
packet inside the main packet...
The receiving buffer is read by setting the mask to rx flag being the
event char CR, so, each time the CR is detected, an event is triggered
and the read port operation is then called. The problem is that many
sub-packages arrive in succession and some sub-packages are lost. I
suppose some bytes are lost while the Read operation triggered by
arrival of the CR event reads the Buffer.
As for now the only way to get the hole package is by reading the
receiving buffer after some time has elapsed or when one knows for
sure that the package has arrived by counting nearly by hand the
number of events, not so nice...
I would like to read the receiving buffer each time a sub-package is
detected but I don't know if that is a good approach as sub-packages
arrive in quick succession and information is lost.
The other approach that was tested is to read each byte that arrives
and later build up the entire package and analyze it, in this case the
Overlapped method was used after requesting the number of bytes in the
receive buffer. But for some reason packages are also lost.
signalization, overhead or a fixed package size.From the other side of the transmition there is no further
Unfortunately I don't know how to continue or even if the approach is
a good one. Any advise or ideas would be kindly appreciated, Thank you
very much.
Best Regards
PD: I have read the document serial comminication in Win32 and many
other related documents from code guru. I found pertinent to ask some
advice at dejanews too.
It is not really practical to use the rx flag to detect end of message. The problem is that your program is subject to latency. It may not resume execution for 50-100 msec after the CR is received, and in that time more characters can arrive.
But this should not cause a loss of data, assuming you are reading the actual number of characters retured by WaitCommEvent. If you are losing characters you have a bug in your program, or perhaps ClearCommError will show a hardware timing error.
I suggest that you use overlapped I/O and the SetCommTimeouts to cause it to return when the buffer is full or a brief silence (a few character times) is detected. Then parse the returned buffer to find the CR characters yourself. With this approach I have successfully stress tested the COM ports at extreme speeds and loads without ever losing a character.
--
Scott McPhillips [MVP VC++]
.
- Follow-Ups:
- Re: Serial COM port communication problems
- From: an0047
- Re: Serial COM port communication problems
- From: an0047
- Re: Serial COM port communication problems
- References:
- Serial COM port communication problems
- From: an0047
- Serial COM port communication problems
- Prev by Date: Re: Performance problem on shared memory over 2GB on XP x64
- Next by Date: GetProcessId from Handle / Win9x/me
- Previous by thread: Serial COM port communication problems
- Next by thread: Re: Serial COM port communication problems
- Index(es):
Relevant Pages
|