[Reprint] How to receive a complete data packet parsing in the serial port, serial port data packet

Source: Internet
Author: User

[Reprint] How to receive a complete data packet parsing in the serial port, serial port data packet

The serial port is used as the transmission medium to introduce how to send and receive a complete data packet. The process involves packaging and unpacking. A good packet transmission mechanism is designed to ensure the stability and correctness of data transmission. The serial port is only a transmission medium. This packet mechanism can also be used for data transmission under the SPI and I2C bus. It is a common problem in single-chip microcomputer Communication Systems (multi-host communication and PC-to-single-chip communication.

1. Detect a data frame based on the end or length of the frame header.

1. Frame header + Data + verification + frame end

This is a typical solution, but you must pay attention to the design of the frame header and frame end, that is, the frame header and frame end cannot appear in the transmitted data domain, it may be misjudged once it appears. If an interrupt is used to receive the message, the program can basically achieve this:

Unsigned char recstatu; // indicates whether a data packet is being received.

Unsigned char ccnt; // count

Unsigned char packerflag; // whether a complete packet flag is received

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

Void uarthur andler ()

{

Unsigned char tmpch;

Tmpch = UARTRBR;

If (tmpch is a packet header) // check whether it is a packet header.

{

Recstatu = 1;

Ccnt = 0;

Packerflag = 0;

Return;

}

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

{

Recstatu = 0;

Packerflag = 1; // indicates that the system has received a complete data packet.

Return;

}

If (recstatu = 1) // whether the data packet is being received

{

Rxbuf [ccnt ++] = tmpch;

}

}

The above refers to receiving a data packet, but again reminds that the packet header and the packet end cannot appear in the data domain. In this case, a false positive will occur. Another one. Data verification algorithms are necessary. due to interference in data transmission, it is inevitable that sometimes data errors may occur. When a verification code is added, the other party may be required to resend the data or simply discard the data. The verification algorithm does not have to be complex. Common addition, XOR, and cyclic redundancy are acceptable. When receiving data, the above receiving program has removed the packet header and packet tail. These can be added based on your own needs. The key is to understand the principle.

The preceding packet Protocol has the following variants:

1.1 frame header + Data Length + Data + verification Value

1.2 package Length + check Value

The above two types actually know the length of the data packet, and then judge a complete data packet based on the length of the received bytes. For example, if the length of a data packet is 256 bytes, we can continue to receive the data packet until we receive 256 bytes. But will there be problems? For example, if the slave machine sends data to the host, half of the data is sent, the data is lost, restarted, and continues to be sent after the host is started, it is obvious that the received data is incorrect, so it is necessary to define an out-of-limit time at this time, for example, we can maintain the following struct.

Struct uartrd {

Char rd [256];

Unsigned int timeout;

}

The rd member variable is used to store the received data bytes. The timeout member variable is used to maintain the timeout value, which is discussed here. How can we maintain this value? You can use a timer to maintain it, or you can maintain it in a common tick interrupt, or you can run an instruction cycle according to the system, maintain it in your own loop and set an initial value for it, for example, 100. When the first data arrives, the timeout decreases by 1 at the specified time to 0, the timeout should be discarded whether or not enough data is received.

2. Judge a data packet based on receiving timeout

2.1 Data + Verification

The core idea is that if data is not received at a certain time, it is considered that the data packet is received completely. The modbus protocol uses the interval to determine the end of a frame. The specific implementation is to use a timer. When receiving the first data, enable the timer, and when receiving a data, reset the timer so that the timer can start timing again, if the set timeout time reaches (the timeout length can be set to five normal reception cycles), it is deemed that new data is not received during this period of time, it is deemed that a complete data packet has been received. The process is as follows:

To make a simple summary, the above methods are still quite common. In terms of implementation, we can design a specific communication protocol based on the actual situation. Sometimes the importance of the data check bit cannot be felt, but it must be added. It is necessary to verify the data. Many MCU now have functions such as FIFO and DMA, so sometimes these features can be used to design a better communication mode. Some people ask whether to interrupt one request when receiving serial data or receive a piece of data after the interruption. I think it is necessary to interrupt one request because the CPU is fast, at least for the serial port, the processor can do other work during the interval of receiving each data. This is a bare metal model. In multithreading, you can directly create a data receiving thread.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.