What to send on serial ports

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.

+4
source share
2 answers

The biggest problem here is that there is a lack of standards. Almost everyone implements their own protocol for restriction devices or when it comes to low-level transport, such as UART / Serial Line. But with the growing IoT trend, this, hopefully, is starting to change.

Please see SLIP and SLIPMUX for sending packet-oriented protocols over a serial line.

Regardless of whether you send JSON, XML, or anything else, you most likely need a stop flag to interrupt your messages. Often \n used for readability in conjunction with ASCII encoded content, but this is not suitable for machine communication with binary encoding of machine 2.

SlipMux is backward compatible with SLIP (for IPv4 and IPv6 packets), but also supports new types of messages, such as CoAP and Diagnostic (human-readable) messages. You can simply implement your own types of packages, such as "JSON" or "XML" messages.

As a protocol, I can recommend CoAP in combination with SlipMux. CoAP is very similar to HTTP, but much more lightweight and thus easily handles most developers.

+3
source

The binary package format is attractive because of its obvious simplicity and efficiency. In addition, some coomunication links already send data in the form of fixed-size packets (USB, TCP / IP, file systems, etc.), but if I had to do it differently, I would not go this way again for the following disadvantages:

  • not human readable (unless you want to confuse things and protect your protocol :-))
  • dependent computer architecture: think about content issues
  • tool bindings: for example, how does the X compiler pack data structures compared to the Y compiler?
  • link dependencies: if the link changes, what if there is an odd match between the size of the protocol packet and the size of the new link packet?

JSON will be my way: ASCII data transfer is less efficient than binary transfers, but its convenience and portability are compensated if the application does not have a strict bandwidth limit. I personally wrote a JSON parser for the Arduino Uno board and works on less than 2 KB of RAM, including the system stack. Well, it can suffocate from deeply nested JSON transfers, but it was good enough to cut out key elements from a tweet tweet, and proved that it can work on tiny systems.

Yves MacDonald

+2
source

Source: https://habr.com/ru/post/1500802/


All Articles