How to do serial communication?

I want to communicate with a serial (USB) connected device on an virtual COM-Port. The commands are sent using Hayes AT-commands, and the answers should come in some time (38400 8N1) with a defined message-end symbol (’>’). I knew about the Serial control, but breaking my head to get the communication aligned. It’s simple challenge/answer but i need to wait for answers until going to the next step in sequence. These waiting gives me headaches.

The documentation states that the serial control class has the DataAvailable event, so you would just listen for that, read in the data and then you can move on to the next step in the sequence… This also has the benefit of leaving your application responsive…

Adding to what shao said;

After you send the command your expectation is that you will receive something back. Once something is received back, the DataAvailable event will fire. What ever you need to do with that data should be triggered in that event. You don’t really need to wait or do any type of listening or looping or fussing with timers. That event firing will be your mechanism to take action on the data received.

Now a word of caution; it is not documented how many bytes of data must be received prior to that event firing. Your code should be designed so that you may receive the event after the first byte of data. If you read the buffer at that time then you may not read what you expect which could be partial bytes of a complete message. Because your messages look like they will be delimited by an ending character (’>’), you should collect the bytes and put them together using that end character to build completed messages. I suggest you look at the serial control’s LookAhead and ReadAll methods.

Good luck.

Also do a google search on State Machine. Basically, you keep enough information around to know at any time what you just sent and what you expect to receive in response.