views:

9

answers:

0

VB.NET 2010, .NET 4

Hello,

I currently have a class that represents a serial device. The communication paradigm is: Send a command/request, the device then sends an acknowledged or not-acknowledged byte. If acknowledged, the device sends its response.

The way this is being done right now is: A timer periodically sends a command/request then polls BytesToRead in a while loop. If not-acknowledged, the command is resent, if acknowledged, the response is read and parsed. This has been problematic in that, if the device fails to respond for whatever reason, my program gets blocked.

As per some advice I got here, I'm trying to rewrite my class so that the responses are handled by the SerialPort object's DataRecieved event. My question is this: How can I keep my responses from getting mixed up? In other words, what if I send two requests before the first response comes in? What if the first response never comes in (for whatever reason)? How might I keep straight whether the data being parsed for a particular DataRecieved event is in response to the first command or the second (supposing no response is sent for the first, for whatever reason). I don't understand things well enough to know if this is a dumb question. I'd appreciate any kind of insight.

Thanks in advance.