Serial communication protocol frame commonly used in single chip microcomputer

foreword

Recently, serial ports are used a lot. I feel that it is necessary to briefly list the used communication protocol frames, so that I can summarize them and communicate with you. If necessary, you can directly refer to them.

1. Introduction to MCU serial port

The serial port (UART) in the microcontroller is a communication method of single-byte sending and receiving. Generally, three wires are enough. If the standard wire sequence is connected, it is a nine-pin DB9 type. The commonly used pair of three wires (rx tx gnd | AB gnd) is simple and convenient to use , often used for short-distance communication with fewer connected devices.
Adding an external control chip to the serial port will change the serial port to the level of two communication types, RS232 and RS485. The logic 1 and 0 of RS232 are divided into +15V and -15V, and the logic 1 and 0 of RS485 correspond to the voltage difference of the AB port +2 ~ +6 and -2 ~ -6. RS232 cannot connect multiple sub-devices and can realize full-duplex communication. RS485 can realize the mounting of multiple devices and single-duplex communication.
Because it is single-byte sending and receiving, a prescribed communication format is required to realize parsing and sending. This is the communication protocol.

2. Commonly used communication protocol types

1. No format

Sometimes no format is king, and many device manufacturers directly use string imperative control. for example

/s1000 

Set speed to 1000

/r0000

Restore factory settings.
This has the advantage that it can be used directly in some devices with general resources and performance, without queues or caches, and directly reads and assigns data in interrupts, which is simple and rude. The switch case is easily resolved. It is suitable for simple short commands and short data occasions.
The disadvantage is that the communication data cannot be too long, and it is prone to misoperation (whoever sends the data indiscriminately and happens to change the core data will be finished).

2. Frame header and frame tail + data

frame header Data length data Check Digit end of frame
0XF5 0X4 0X01 0X02 0X03 0X04 0XC5 0X5F

The simple and commonly used format is this format with frame header and frame tail or check digit. Usually, the data is received in the single-chip microcomputer and then stored in the cache queue, and then the data in the cache queue is extracted in the main loop according to the frame header and frame tail, and the assignment operation is performed frame by frame.
There are many checksums, such as sum check, CRC, etc. Here, I think encryption can also be implemented. You can set an encryption algorithm yourself, and the check digit can be obtained using the encryption algorithm. Others can’t get the encryption algorithm. Only the communication protocol can’t be driven. of your device. (Show operation, many do not even have a check digit, the simpler the more reliable)
This is a bit simple and reliable, and the upper and lower computers can easily perform data interaction according to the communication format, which is suitable for longer data transmission.

3. Frame header and frame tail + data + additional function code

frame header function code Data length data Check Digit end of frame
0XF5 0X01 0X4 0X01 0X02 0X03 0X04 0XC5 0X5F

This format is compatible with the second and can handle different types of commands, such as read and write.
Function codes can be added indefinitely, such as using two function codes, one to indicate which variable to operate on, and the other to indicate whether to read or write. This should be the most widely used, the workload is not very big, and it is easy to debug by yourself.

4. Frame header and frame tail + data + additional function code + target address

frame header address function code Data length data Check Digit end of frame
0XF5 0XCC 0X01 0X4 0X01 0X02 0X03 0X04 0XC5 0X5F

This kind of addressing is interesting, and it has the ability to identify devices. For example, if you randomly access a device of the same type but with a different address, the device will not kill you if you send a command. This is suitable for use on the 485 communication bus, and all devices identify whether to execute according to the address.
The advantage is that it has the function of device identification and the uniqueness of command execution, which is suitable for 485 communication.
The disadvantage is that 232 is a bit tasteless when used. Most of what I have seen so far is this kind of communication, whether it is 485 or 232 (in fact, the address is also a function code, so it is not a big problem).

5. Frame header and frame tail + data + additional function code + forwarding function

frame header source length source address target length target address function code Data length data Check Digit end of frame
0XF5 0X01 0XCC 0X02 0XDD 0XBB 0X01 0X4 0X01 0X02 0X03 0X04 0XC5 0X5F

This is the most powerful serial port communication protocol I have ever used. It can be used to judge whether it is necessary to forward the next level or perform an operation command through the address length and address field.
The process of this command is: the device of 0XCC sends a command frame to the device of 0XDD, the device of 0XDD finds that it is not the final target, reorganizes the frame and sends it to the next-level device with the address of 0XBB, and 0XBB finds itself to be the final target after receiving the command So execute the command.
The advantage of this communication protocol is that it has complete functions and can realize forwarding.
The disadvantage is that the procedure for parsing frames is complicated, adding a forwarding will add a time for reframing, and the more forwarding, the more time-consuming. (However, who can use the serial port to string so many sub-levels? Are you sick? You can consider using other communication methods.)
This should be to adapt the serial port to other communication methods (such as can, tcp/ip, etc.), so that the program If it is handled properly, it should be possible to mix and match different communication methods. For example, the main board sends through the serial port, and the next level sends to more devices through the CAN bus. With forwarding, there is a certain degree of communication compatibility.
It is a bit complicated to use the fourth way for the serial port.

6.MODBUS RTU

This is awesome, I have never used it, let me just say it. This single-chip microcomputer needs to use 485 communication. It is a formal unified standard. There are off-the-shelf integrated modules that can be used after buying. PLC is commonly used in electrical industrial control.
Modbus-RTU mode means that when the controller is set to communicate in RTU (remote terminal mode) mode on the Modbus network, each 8Bit in the message contains two 4Bit hexadecimal characters. Markdown converts text to HTML 1 .
The operation method is similar to that of 4 (but people have made it a recognized standard, which is bullish), and it involves more operations on registers. Registers are generally operated on a bit-by-bit basis, and each bit has the function of each bit, which can be expanded at once. It has many uses.

7. Other

Others are other, some methods that can be lumped together, and these are mostly a mix and match of the above-mentioned types. Some have a frame header but no frame trailer, some have a frame trailer but use the address as the frame header, some do not add a checksum, some use a fixed length to realize the function of the frame trailer, and some weird ones also use the address as the frame header, The checksum does not have a fixed length at the end of the frame.

Summarize

That's about it, the basic communication protocol frame of a serial port is just that, and I will add it later when I think of it. Don't blindly pursue more functions, simple and practical is the most reliable. It is best not to add the communication protocol.
Suddenly thought of an idea, can the MCU program also modularize each function bit in the above protocol frame to achieve a complete compatibility, for example, if I use the frame header, I will call an add program to add the frame header, If the function code is used, add a function code bit to put the function code, and then make a large package outside, so that it will be easy for a communication template to be compatible with all communication protocols. (Maybe it's not necessary, most of them are very simple, and the extracted data is not much, not to mention my C++ is still in a mess, I will think about it when I have the strength, don't panic)

references


  1. Modbus communication protocol (2)—RTU . ↩︎

Guess you like

Origin blog.csdn.net/weixin_43058521/article/details/119494326