02-03-2009 07:42 AM
Hi all,
I am looking for the best way to read messages arriving in bunches at a serial port with a frequency of 20 Hz.
I am building a simple "gateway" that will read messages arriving at one serial port, log them, filter out some and then immediately send the rest to another serial port. The messages can be up to 80 characters long and start with $ and end with line feed.
I let my application read the com port every 50 ms and use the "Bytes at port" property to decide how many bytes to read. However, I run in to trouble if there are to many messages appearing at the same time. I did a test where I send four messages every 50 ms looking like:
0 abcdefghijklmnopqrstuvxyzåäö
1 abcdefghijklmnopqrstuvxyzåäö
2 abcdefghijklmnopqrstuvxyzåäö
3 abcdefghijklmnopqrstuvxyzåäö
Some times a few characters "get lost" or the read start or stops at the wrong moment and my read messages can start looking like:
opqrstuvxyzåäö
1 abcdefghijklmnopqrstuvxyzåäö
2 abcdefghijklmnopqrstuvxyzåäö
3 abcdefghijklmnopqrstuvxyzåäö
0 abcdefghijklmn
If I send fewer messages this problem becomes smaller. Is there a clever way to avoid this? Should I not use the "Bytes at port" property? Do I have to "paste" the messages back together using the fact that I know how the start and end? All help is appreciated.
Best regards
Martin
02-03-2009 01:25 PM
Hi
I would use bytes at port each 2ms to check for data available and the would read the string with endcharacter enabled and set to your endchar (LF)
and immediately after that check again (wait is not necessary.
So I would not use the value from bytes at serial port only the fact that at least one character has arrived.