Re: Serial COM port communication problems



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.

From the other side of the transmition there is no further
signalization, overhead or a fixed package size.

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++]

.



Relevant Pages

  • Re: Lost tail-end of reports through Specialix SX on Windows NT 4
    ... looks like the serial port isn't flushing its buffer, causing the report ... to truncate the last n characters, 'n' being the size of the buffer. ...
    (comp.databases.pick)
  • Re: Highly responsive serial port
    ... My problem consists of reading from a serial COM port as fluent as possible. ... If my reader only reads the input buffer after 50ms I'm too late to reply but I don't know it. ... Would a driver(in kernel mode) have enough priority to process such requests in time? ... a general rule of thumb in thread synchronization is to stay away from depending on "sleep" and loop designs in a vain attempt to synchronize I/O. ...
    (microsoft.public.win32.programmer.kernel)
  • Serial Communication in C# Express
    ... ago and I really appreciated Nicholas Paldino's help, ... > I can only achieve to transmit '34', ... After the usual port initialization, ... // Obtain the number of bytes waiting in the port's buffer ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Serial Communications - Lost Event
    ... "Completion of write" is a moment when all data from the app buffer is ... The driver can keep the data in its internal buffer ... > serial port monitoring and test applications. ... > of a serial port means when all of the data has been transfered. ...
    (microsoft.public.win32.programmer.kernel)
  • [PATCH] cpm_uart: Fix dpram allocation and non-console uarts
    ... Makes non-console UART work on both 8xx and 82xx ... static unsigned int cpm_uart_tx_empty(struct uart_port *port) ... /* Write back buffer pointer */ ... * Allocate DP-Ram and memory buffers. ...
    (Linux-Kernel)