One knows the problem of frame synchronization in serial communication

When the bottom layer of the STM32 serial port was encapsulated, serial frame synchronization was encountered. Although similar situations have been encountered in the past, the written code can basically solve the problem, but it cannot always completely explain some details in logic.

Current working environment:

Because the code wants to use in a simple PID closed loop, do online parameter tuning. Assume that the current PID solution cycle is 1ms, that is, once every 1ms, do a serial port packet receiving, unpacking, Pid solution, data collection, and then package and send packets. In other words, it is a fixed-step unwrapping.

The scheme of the serial port is to open and receive the DMA and DMA interrupts. (Resolutely not considering the use of serial port interrupts directly. A one-byte interrupt is too costly.) The DMA array acts as a serial FIFO queue (not really a queue).

One knows the problem of frame synchronization in serial communication

Current requirements:

1. When the time comes, check if there is data received. Jump out if there is no, then go to the next step

2. Check the packet format in the data, such as whether the header is correct, whether the frame length is aligned, CRC (has not been done yet), etc.

3, Check the package format error, add a flag bit back packet, declare the packet format error request retransmission. If there is no error in the packet format, unpack and set the corresponding register and assignment.

4, have a reasonable receive buffer, greater than the buffer data to give up.

5, can detect missing bytes, multi-byte frame length error.

Several sets of tried and tested programs:

1, DMA array length and frame length are equal.

Trigger condition: The DMA count value is reduced to 0 (that is, the data that has already filled up the length of one frame) generates a DMA interrupt and writes the trigger flag bit. PC can be achieved by opening a thread to monitor the number of buffers.

Unpack operation: Set up a union, where the structure is a frame protocol, and a u8 array is shared as a DMA array. Determine the trigger condition. If it is satisfied, read the header of the header in the shared object. If it is correct, continue reading the member and unpack the assignment.

Buffering mechanism: None. The DMA is set to normal mode and stops when the count is reduced to zero. New data arrives without being passed into the array. The serial port can be manually closed on the PC.

Error mechanism:

Frame misalignment: It will be found in the packet header check that the current frame is discarded and the retransmission flag is set to request retransmission.

Missing byte: After the sending of the missing byte frame is completed, the trigger condition will not be satisfied. After the first few bytes of the data of the second frame fill the missing frame, the unpacking operation is triggered. When checking the siege, an error response is given. Discard bytes without frames, but it is difficult to ensure that the last few frames of a missing byte frame can be successfully received. And the error and error response are not synchronized. That is, the error response appears in the wrong next frame.

Byte exceeds: A frame whose byte is exceeded will respond in time, and due to an error in the trailer, it will immediately respond to the error and request retransmission.

Unpacking too fast: There will be no unwrapping speed faster than the receiving speed. Data is unpacked because the data is full of one frame length.

2, DMA array points to the linked list of element type frame structure

Trigger conditions: DMA count value is reduced to 0 (that is, the data has been full of a frame length) generates a DMA interrupt, DMA interrupt Push_back List operation, an element, and then the DMA memory address points to the new element address. The triggering condition is that the size of List is greater than 1 (before receiving any message, there must be an empty element for placing the incoming message);

Unpack operation: Check the header of the first element of the List header. If it is correct, read the member unwrapping assignment and then pop_front the List until the size of the list is equal to 1.

Buffering mechanism: The natural buffering mechanism of the linked list. The only concern is the heap overflow. An upper limit can be set and judged in the interrupt.

Error mechanism:

Frame misalignment: It will be found in the packet header check, but it needs to discard all the frames after the misplaced frame in the buffer. Because the inevitable behind are misplaced.

Missing byte: When the second frame arrives, it is detected when the end of packet is checked. There is also the problem that the error response is not synchronized.

Byte exceeded: Error synchronization response. Discard all frames in the buffer

Unpacking too fast: There is no such problem. Reasons the same way 1.

3, DMA array points to the first address of the array multiple of the frame length

Trigger condition: The buffer queue is not empty. Immediately after the response is triggered, the buffer queue memcpy is unpacked into a temporary array. Clear the queue at the same time.

Unpack operation: search for the first byte of the header in a temporary array. When a single packet is satisfied, check immediately: the second byte of the packet header, whether the packet trailer is within the buffer length, and whether the packet trailer is correct. If all four conditions are met, the unpacking assignment starts immediately. Repeat the previous step and search for the second header in the array. Until the end of the buffer, the first part of the remaining frame discards the tailless frame.

Buffering mechanism: Buffer queues act as buffers.

Error mechanism:

Frame misplacement: There is no concept of frame misalignment in a temporary array. Frame misalignment can be completely unpacked.

Missing byte: In the unwrapping step, it is detected that the packet end is wrong, then retransmission is requested. And can respond synchronously.

Byte exceeds: missing of same byte

Unpacking too fast: Because the trigger mode is non-empty. If the trigger condition is checked, if the partial frame is received exactly, the trigger condition can still be satisfied. At this point, the received frame will be discarded as an error byte and errored.

summary:

Comparing the three methods, the third method has superior performance and can be transplanted to the PC. However, there is still need for discussion on the issue of expedited unpacking.

The missing byte synchronization response and unpacking contradiction:

The problem can be reduced to a 10-byte frame. When unpacking, if there are only 9 bytes in the packet, then the frame is not finished or the byte is missing.

If you use "received more than 10 bytes of data" as the trigger condition for unpacking, then there will always be 10 bytes when unpacking, and you can determine whether the last byte is the end of packet. But byte misses will always only respond in the next frame.

If "buffer data is more than 0" is used as an unwrapping trigger condition, although a byte miss can be immediately responded, it is also possible to misjudge an unposted frame.

Therefore, it needs to analyze the current application. The current frame rate and frame length for the microcontroller are:

Baud rate: 115200

Transmission frame rate: 5f/s

Send frame length: 20 Bytes

Received frame detection period: 1ms

Received frame length: 10 Bytes

Send 1Byte data, because there is no check digit, 1 stop bit, so it needs 10bits.

Then, the transmission rate is 11520 B/s (approximately 11 KB/S), that is, 86.8 us (approximately 0.1 ms) is required to transmit 1 byte.

20Bytes per frame, 1.736ms (about 2ms)

Receiving frame 10Bytes per frame requires 0.858ms (about 1ms)

So for the receiving condition of the one-chip computer under the present situation, 1ms unpacks once, do not need the buffer at all, but it is likely to happen in the frame cut-off.

Therefore, you should use "has received data equal to the frame length bytes" as a trigger condition. Abandon the synchronization response of the missing byte frame.

However, for the PC, if the same trigger condition is detected for an interval of 1ms, the received frame always changes to 1.736 milliseconds, and then one frame must be less than one frame.

Therefore, it is also possible to use "data that has been received equal to the frame length bytes" as a trigger condition. Abandon the synchronization response of the missing byte frame.

However, for the serial port class on Qt, we still haven't figured out how it works. We can't discuss what method is appropriate.

/********************************** Updated on November 1 ********************************/

A scheme that can improve the response speed of missing byte frame error:

Judging whether the frame is missing or not finished is actually a matter of time.

For example, the aforementioned 115200 baud rate, 20Bytes frame, the transfer time should be less than 2ms.

Therefore, when there is data in the receive buffer and the single data does not reach 20 bytes, if this state is maintained for more than 2 ms, the transfer is completed and the bytes are missing.

The program's own step timer already has a timer function. Therefore, the implementation is as follows.

Declare a flag bit 1: FIFO queue has data but is not full.

Declares a counter 1: Count of flag 1 bits.

When the FIFO queue data transitions from 0 to 1, the set flag is set.

When CheckMailBox is set, flag bit 1 is set, then the value of counter 1 is incremented by one.

Since 20Bytes frames should be sent within 2ms. The solution period is 1ms.

Therefore, when the value of the counter 1 is greater than 2, if the data length of the FIFO queue still does not reach the frame length, it indicates that the packet has data lost. Set error flag.

Lost-byte frames can be detected.

Vapesoul Rechargeable Vape Kit

Vapesoul Rechargeable Vape Kit is so convenient, portable, and small volume, you just need to take them
out of your pocket and take a puff, feel the cloud of smoke, and the fragrance of fruit surrounding you. It's so great.
We are the distributor of the Vapesoul & Voom vape brand, we sell vapesoul disposable vape,vapesoul vape bar, voom vape disposable, and so on.
We are also China's leading manufacturer and supplier of Disposable Vapes puff bars, disposable vape kit, e-cigarette
vape pens, and e-cigarette kit, and we specialize in disposable vapes, e-cigarette vape pens, e-cigarette kits, etc.

vapesoul rechargeable vape starter kit,vapesoul rechargeable vape kit battery,vapesoul rechargeable vape kit pen,vapesoul rechargeable vape kit device,vapesoul rechargeable vape kit mod

Ningbo Autrends International Trade Co.,Ltd. , https://www.supervapebar.com