Re: Serial COM port communication problems
- From: "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
- Date: Mon, 21 May 2007 18:04:44 -0400
an0047@xxxxxxxxx wrote:
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++]
How can this be done? (SetCommTimeout association to buffer size?)...I
will try to understand the overlapped
method better. Do you mean the Readfile Buffer that receives the data
read? I'm free to set the size of that buffer,
I will need to find the best size for this buffer, subpackages vary
amoung them as much as 20 bytes.
I'm a bit afraid of adding timing parameters to the problem but it
seems an elegant approach, hope
doesn't get more complex.
The problem by this type of signalization (the new timeouted buffers)
is that there will be CRs embeeded
on them. So the CR is first to be found, and regard the left part as a
subpackage or end of a subpackage and the right part as beginning of a
subpackage an so on...? Is there some type of generic algorithm to
build this type of subpackages together?
Thank you very much for your advice
Timing is the essence of the problem. The device driver is fast enough to receive all characters and put them in your buffer, but your program is much slower due to multitasking delays. So your program must process large numbers of characters that are returned occasionally in the ReadFile buffer. The ReadFile buffer should be large enough to hold all data that might be received while your program is suspended - perhaps 100-200 msec, possibly even longer. Do not consider the subpackage size, only the maximum required buffering time, when sizing the buffer.
You must not rely on the queue size reported by ClearCommError: That can cause a timing problem that is probably the cause of your data loss. By the time you read the data the queued size could have changed. The correct size is returned by ReadFile or GetOverlappedResult.
SetCommTimeouts is not set according to buffer size, it should be set according to baud rate and the timing characteristics of your data stream. There are probably gaps in the data stream (intervals with no characters). Use SetCommTimeouts to return when it detects such a gap. Example: 10/9600 = .001 seconds per character, times N character times in a gap. You would like this to be > .05 seconds if possible, to reduce processor loading.
To parse the buffer and put together the subpackages you can use memchr and memcpy.
--
Scott McPhillips [MVP VC++]
.
- References:
- Serial COM port communication problems
- From: an0047
- Re: Serial COM port communication problems
- From: Scott McPhillips [MVP]
- Re: Serial COM port communication problems
- From: an0047
- Serial COM port communication problems
- Prev by Date: Re: Serial COM port communication problems
- Next by Date: Re: Serial COM port communication problems
- Previous by thread: Re: Serial COM port communication problems
- Next by thread: Re: Serial COM port communication problems
- Index(es):
Relevant Pages
|
Loading