I grappled with the same question for a while. Here is how I solved the issue.
1- Find a suitable exchange format and protocol
Here you need to decide what is the most important thing on the communication. Is it speed? Is it error detection? Is it memory footprint? For my project, for instance, I decided that I need my communication to be as error free as possible, with minimal memory footprint (up to 64 bytes) and I don't really care about speed, so I gone for a JSON messaging protocol where the desktop application send a short (up to 64 characters) JSON message and receives a short answer. This solved MY problem, your requirements might be different. If you need speed and don't care for memory, for instance, you can use a binary protocol with a header specifing size, the message and a CRC footer to check vailidity. Or you can use BSON and take this library to avoid the hassle.
2- Define the Serial Polling Cycle
How will the arduino firmware knows it is time to send or receive messages? It is always talking? Always ready to receive? It looks at the serial port buffer once each few microsseconds to know if there is an incomming message? How that is handled by your device? If you just need to output data without bothering whether there is someone listening or not, like an GPS device, just throw a
Serial.print now and then, but if you need to wait for data, you will have to compartimentalize the process inside an FSM or using some kind of scheduler. If you are not using a scheduler, I recommend this one.
3- Define the Communication Cycle
Here you will take the data from serial port and storing it on a buffer for later usage.
The trick here is
1- Check the serial FIFO for incomming data.
Serial.available() does it already.
2- If there is incomming data, copy the data to next available space in the buffer.
3- Check if there is more incomming data after a few millisseconds.
4- If there is no incomming data, mark the buffer as ready to use, and parse it.
The following code is unscheduled for simplicity,and I recommend you to replace the
sleep() calls to proper scheduling least you want the arduino go unresponsive. It is also waiting text data, so you will need to make changes if you are waiting binary data.
memset(buffer, 0, 64);
Serial.readBytes(buffer + strlen(buffer), Serial.available());