Here the serial port as the transmission medium, the introduction of how to send to receive a complete packet. The process involves wrapping and unpacking. Designing a good packet transfer mechanism is very advantageous to the stability and correctness of data transmission. The serial port is only a kind of transmission medium, this kind of packet mechanism can also be used for the spi,i2c of the bus. In the single-chip microcomputer communication system (multi-computer communication and PC and MCU communication), is a very common problem.
One, according to the frame head frame tail or frame length detection of a data frame
1. Frame header + Data + checksum + frame tail
This is a typical scenario, but the frame head and frame tail in the design of the time to pay attention to, that is, the frame head, frame tail can not appear in the Transmitted data field, once the occurrence may be false. If you use interrupts to receive, the program basically can be implemented as follows:
unsigned char recstatu;//Indicates whether it is in the state of a receiving packet
unsigned char ccnt; Count
unsigned char packerflag;//whether to receive a complete packet flag
unsigned char rxbuf[100];//buffer to receive data
void Uarthandler ()
{
unsigned char tmpch;
tmpch = UARTRBR;
if (tmpch is Baotou)//detection is Baotou
{
Recstatu = 1;
ccnt = 0;
Packerflag = 0;
return;
}
If (tmpch is the end of the package)//detection is the end of the package
{
Recstatu = 0;
Packerflag = 1; Used to inform the system that a complete packet has been received
return;
}
if (Recstatu ==1)//is in the receiving packet state
{
rxbuf[ccnt++] = tmpch;
}
}
The above is to receive a packet, but again to remind, Baotou and packet tail can not appear in the data field, once there will be a miscarriage of error. The other one. Data validation algorithm is necessary, in the data transmission, due to interference, it is inevitable sometimes data errors, coupled with the check code can be found in data transmission errors, can request the other side of the information to resend, or simple discard processing. The validation algorithm does not have to be complex, and common sums, XOR, and cyclic redundancy are all possible. I receive the above program in the receiving data, has been the Baotou and the end of the package, which can be added according to their own needs, the key is to understand the principle.
There are several variants of the package protocol described below:
1.1 Frame Header + data length + data + checksum value
1.2 Packet length + checksum value
Both of the above actually know the length of the packet, and then judge a complete packet based on the length of the bytes received. For example, if you define a packet length of 256 bytes, then we can receive it until 256 bytes are received, which is considered to be a packet. But will there be a problem? For example, from the computer to send data to the host, sent half, power down, restart, after the boot to continue to send, it is obvious that the data received is not right, so it is necessary to define a time limit, such as we can maintain a structure such as the following.
struct uartrd{
Char rd[256];
unsigned int timeout;
}
The member Variable RD is used to hold the data bytes received, and the member variable timeout is used to maintain the timeout value, which is mainly discussed here. How to maintain this value, you can use a timer to maintain, can also be placed in the ordinary tick interrupt inside to maintain, but also according to the system to run an instruction cycle, in its own cycle to maintain, set an initial value, for example, 100, when the first data arrives, Timeout is reduced by 1 at the specified time and reduced to 0 o'clock, and it is considered time-out, whether or not enough data is received, should be discarded.
Second, according to receive timeout to judge a packet
2.1 Data + Checksum
The core idea is that if the data is not received at a certain time, the packet reception is considered complete. In the Modbus protocol, a time interval is passed to determine the end of the frame. The implementation is to use a timer, when the first data is received, the timer, when the receipt of a data, the timer is cleared, let the timer restart the timing, if the Set timeout time (timeout length can be set to 5 normal reception period), It is assumed that no new data has been received within this period of time and that a complete packet is received. The process is generally as follows:
To carry out a simple small summary, the above methods are still more commonly used, in the specific implementation, according to the actual situation, the design of a specific communication protocol. The data check bit, sometimes does not feel the importance, but must add, carries on the correlation verification to the data is necessary. Many MCUs now have features such as FIFO,DMA, so it is sometimes possible to design better communication methods by using these features. Some people ask in the reception of serial data should be interrupted one time to receive one, or into the interruption after receiving a piece of data, I think should be interrupted to receive one, because the CPU is very fast, at least for the serial port is so, at the interval of receiving each data, the processor can do some other work. This is the model under bare metal. In multi-threading, it is possible to create a data-receiving thread directly.
How to receive the parsing of a complete packet in the "reprint" string