I'm sorry if this is a very vague question, but I can't seem to formulate it properly to find anyone else with this issue. The main question is, once you have a serial connection established between two devices, how do you use that connection to implement two way communication?
An example might help. Suppose you have a temperature sensor as an embedded device using a microcontroller and firmware written in C. You have a serial port connection from that sensor to a computer and some software on the computer to interface with it, say a C++ application. I understand how to setup the serial ports on both sides and read and write single bytes of data between the two devices. The real question is what convention do you use to communicate between the two devices?
Assume your requirements are as follows:
1.) You need to be able to send a command to take a single temperature reading from the embedded device and send it to the computer for display purposes.
2.) You need to send commands to make the sensor start and stop streaming temperature values.
3.) You need a set of commands to set various aspects in the firmware such as streaming rate, stream on startup, blink leds, etc.
4.) You need some sort of structure to send complicated forms of data to the computer, perhaps an array of battery voltage readings.
Ways to Accomplish This
It seems there are a couple ways people tend to do this:
Simple String API:
The most common, from dealing with third party sensors, seems use a simple string based API, such that the commands for starting and stopping streams might be "SS,1\r" and "SS,0\r" respectively. In this case you would have to read from the serial port until you get the "\r" character and then parse the data you got to see if it has a command (left of comma) and parameters (right of comma). This works for scenarios 1 to 3 above but doesn't make scenario 4 very easy.
JSON String API:
This works the same as the one above but instead of passing your parameters as simple values, you pass JSON objects that can represent complicated structures of data. Hence you could send the array of battery voltages as a JSON array. This method seems to cover all use cases 1-4 above. But JSON sends strings and it is harder to parse using embedded c. It would work wonders for the computer side which could use a higher level language such as Java that has libraries for reading JSON data.
Packet Style API:
This is the solution we accepted that I am now kind of regretting. It involves sending a structured packet convention of bytes for each piece of data we send. The packet structure is shown below.
[0xFF][0xFF][ID][CMD][D0][D1][D2][D3][D4][D5][D6][D7][0xEE][0xEE][0xEE]
With this structure we send a header and footer (0xFF's and 0xEE's) for validating packet completeness, an id for sending sequential packets (for transmitting an array of data), a data array which we can use to pack longs, floats, ints, etc, and a command byte (CMD) which can be used by the device to determine how to parse the data payload (D0-D7).
So I ask, what is the most preferred way to communicate over a serial port? Is there any other ways I am missing? I have been doing a lot of web development lately and it seems JSON is a nice abstract transmission system, but has its limitations because you have to do a lot more string parsing, which is a bit complicated on the firmware side.