Serial communication protocol development and configuration process

Serial communication protocol

Serial communication refers to the serial port transmitting and receiving bytes in bits. Although slower than parallel communication in bytes, the serial port can receive data with one line while using one line. In serial communication, commonly used protocols include RS-232, RS-422, and RS-485.

The serial port is used as the transmission medium to introduce how to send and receive a complete data packet. The process involves encapsulation and unpacking. Designing a good packet transport mechanism is very beneficial to the stability and correctness of data transmission. The serial port is just a transmission medium. This packet mechanism can also be used for data transmission under the SPI and I2C bus. In the MCU communication system (multi-machine communication and PC and MCU communication), it is a very common problem.

1. Detecting a data frame according to the end of the frame header or the length of the frame

1, frame header + data + check + frame tail

This is a typical solution, but attention should be paid to the design of the frame header and the end of the frame, that is, the frame header and the end of the frame cannot appear in the transmitted data field, and may be misjudged once it appears. If you use interrupts to receive, the program can basically do this:

Unsigned char recstatu; / / indicates whether it is in the state of receiving a packet

Unsigned char ccnt; //count

Unsigned char packerflag; / / whether to receive a complete packet flag

Unsigned char rxbuf[100];//buffer receiving data

Void UartHandler()

{

Unsigned char tmpch;

Tmpch = UARTRBR;

If(tmpch is the header) //detect if it is a header

{

Recstatu = 1;

Ccnt = 0 ;

Packerflag = 0;

Return ;

}

If (tmpch is the end of the package) / / check if it is the end of the package

{

Recstatu = 0;

Packerflag = 1; / / used to inform the system has received a complete packet

Return ;

}

If(recstatu ==1) //Is it in the state of receiving packets?

{

Rxbuf[ccnt++] = tmpch;

}

}

The above is to receive a data packet, but again reminded that the header and the end of the packet can not appear in the data field, once there is a misjudgment. another one. The data verification algorithm is very necessary. In the data transmission, it is difficult to avoid data errors sometimes due to interference. The check code can be re-sent by the other party when the data transmission error is found, or Perform a simple discard process. The verification algorithm does not have to be complicated, and ordinary addition, XOR, and cyclic redundancy are possible. The receiving program above I has removed the header and the end of the packet when receiving the data. These can be added according to my own needs. The key is to understand the principle.

The following package variants have the following variants:

1.1 frame header + data length + data + check value

1.2 packet length + check value

The above two actually know the length of the packet, and then judge a complete packet according to the length of the received byte. For example, to define a packet with a length of 256 bytes, then we can receive it until it receives 256 bytes, which is considered a packet. But is there a problem? For example, the slave sends data to the host, sends it halfway, powers down, restarts, and continues to send after booting. This obviously indicates that the received data is wrong, so it is necessary to define an overrun time, for example, we can maintain One such structure below.

Struct uartrd{

Char rd[ 256];

Unsigned int timeout;

}

The member variable rd is used to store the received data bytes; the member variable timeout is used to maintain the timeout value, which is mainly discussed here. How to maintain this value can be maintained by a timer, which can be maintained in the normal tick interrupt, or it can be maintained in its own cycle according to the cycle of the system running an instruction, and set an initial value for it. For example, 100, when the first data arrives, the timeout will decrease by 1 at the specified time. When it is reduced to 0, it will be considered as a timeout. Whether or not enough data is received, it should be discarded.

Second, judge a packet according to the reception timeout

2.1 Data + Check

The core idea is that if the data is not received within a certain period of time, the packet reception is considered complete. In the modbus protocol, the time interval is used to judge the end of the frame. The specific implementation is to use a timer. When receiving the first data, the timer is started. When a data is received, the timer is cleared, and the timer is restarted. If the timeout period is set. (The timeout period can be set to 5 normal receiving periods.) If it is considered that no new data has been received during this period of time, it is considered that a complete data packet has been received. The process is roughly as shown below:

Serial communication protocol development and configuration process

To carry out a simple and small summary, the above several methods are still relatively common. In the specific implementation, a specific communication protocol can be designed according to the actual situation. The data check bit sometimes does not feel its importance, but it must be added, and a relevant verification of the data is necessary. Nowadays, MCUs have functions such as FIFO and DMA, so sometimes these features can be used to design a better communication method. Some people ask whether they should interrupt receiving one time when receiving serial data, or receive a piece of data after entering the interrupt. I think it should be interrupted to receive one, because the CPU is very fast, at least for the serial port, accepting each data. During the interval, the processor can still do some other work. This is a model under bare metal. In multi-threading, you can directly create a data receiving thread

72V Battery pack

72V Battery Pack ,Lithium Ion Battery Pack,Lithium Battery Pack,Battery Power Pack

Zhejiang Casnovo Materials Co., Ltd. , https://www.casnovo-new-energy.com

This entry was posted in on