|
-
September 20th, 2011, 02:15 PM
#1
Serialport incoming message manager
Hi, I'm writing a little message control app that will use a custom packet protocol to be sent between two or more devices.
I will be sending and recieving messages with a header containing destination and source address, message number, length, etc.
The problem is, when a message arrives a DataRecieved event will occur for the serialport, but the BytesToRead variable might not have the final value it will have after the entire packet is recieved, so I will not have all the incoming data. Even if I save whatever I got so far in a temp buffer, DataRecieved is not triggered again so until the next message arrives I will not finish recieving the first one.
I've set the threshold to trigger DataRecieved only after SIZEOF_HEADER bytes are recieved, so that I know for sure how many bytes I will be expecting, but I can't increase the threshold more since an empty packet would never trigger an event otherwise...
I could raise a <newData> flag and start a timer to look for data after a few ticks, but I doubt it's very "clean" to do so. How long should I wait? If I wait to long, my input buffer could overflow if continous data is arriving, or I could not have recieved the whole packet yet if I wait to little...
What other ways of recieving data are there? What's the best way of making sure I read everything I need avoing the risk of data loss?
Thanks for the help!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|