Sorry if this is a very vague question, but I can not formulate it correctly to find someone else with this problem. The main question is that, as soon as you have a serial connection between two devices, how do you use this connection to implement two-way communication?
An example may help. Suppose you have a temperature sensor as an embedded device using a microcontroller and firmware written in C. You have a serial port connecting this sensor to a computer and some software on the computer to interact with it, say, in C + + application. I understand how to configure serial ports on both sides and read and write single bytes of data between two devices. The real question is, what agreement do you use to communicate between the two devices?
Suppose your requirements are as follows :
1.) You should be able to send a command to get one temperature reading from the built-in device and send it to a computer for display.
2.) You need to send commands to force the sensor to start and stop the streaming temperature values.
3.) You need a set of commands to set various aspects in the firmware, such as the flow rate, the flow at startup, the flashing LEDs, etc.
4.) You need some kind of structure to send complex data forms to the computer, possibly an array of battery voltage readings.
Ways to accomplish this
There seem to be several ways people tend to do this:
Simple String API:
The most common phenomenon regarding third-party sensors is a simple string-based API, so the commands to start and stop streams can be "SS, 1 \ r" and "SS, 0 \ r", respectively. In this case, you will have to read from the serial port until you get the "\ r" character, and then analyze the received data if it has a command (to the left of the comma) and parameters (to the right of the comma). This works for scenarios 1 through 3 above, but does not make script 4 very simple.
JSON String API:
This works the same as above, but instead of passing your parameters as simple values, you are passing JSON objects that can represent complex data structures. Therefore, you can send an array of battery voltages as a JSON array. This method seems to cover all uses of 1-4 above. But JSON sends strings, and it's harder to parse with the built-in c. This would do wonders for the computer side, which could use a higher level language such as Java, where there are libraries for reading JSON data.
Package Interface API:
This is the decision we made that I am now sorry. This includes sending a structured convention of byte packets for each piece of data that we send. The package structure is shown below.
[0xFF] [0xFF] [ID] [CMD] [D 0] [D1] [D2] [D3] [D4] [D5] [D6] [D7] [0xEE] [0xEE] [0xEE]
In this structure, we send headers and footers (0xFF and 0xEE) to check the completeness of the packet, an identifier for sending consecutive packets (to transmit an array of data), an array of data that we can use to pack lengths, ints, etc. and command byte (CMD), which can be used by the device to determine how to analyze the data payload (D0-D7).
So, I ask, what is the most preferred way to communicate through the serial port? Are there any other ways that I am missing? I have been doing a lot of web development lately, and it seems that JSON is a great abstract transfer system, but it has its limitations because you need to do a lot more parsing of strings, which is a bit complicated on the firmware side.