There are usually two ways of communication between embedded systems: parallel communication and serial communication. The parallel method has a fast data transmission speed, but occupies more communication lines. The reliability of data transmission decreases with the increase of distance, and it is only suitable for short-distance data transmission. In long-distance data communication, serial communication is generally used, which has the advantages of occupying less communication lines and low cost. At present, RS 232 serial port is the most widely used serial interface in PC and communication industry. It is used in point-to-point communication mode. In actual use, the simplest three-wire connection is often used, that is, the serial ports of the devices at both ends are only connected to the three wires of receiving, sending and grounding, so that simple full-duplex communication can be realized. The communication protocol is the language for data exchange between the devices at both ends and is the guarantee of communication reliability. Under the premise of ensuring the function, the communication protocol should be simple.
1 System communication requirements
This system mainly completes the functions of time interval measurement and transient data collection in the field environment. Each module in the system uses Samsung's S3C2440 chip as the processor, and the operating system uses embedded Linux. The main tasks of inter-module communication are the issuance and response of control commands, reporting of working status and collected data, etc., with high requirements on communication reliability and no data encryption requirements.
According to the system hardware and software conditions, the serial port working parameters are set as follows: 115 200 baud rate, 8 data bits, 1 stop bit, odd parity, no flow control. The baud rate setting needs to comprehensively consider the serial port performance of the selected chip, the length of the serial port connection line, the maximum frame length of the transmitted data, and the bit error rate during the application process; no flow control is because the serial port uses a three-wire connection.
2. Layered Structure of Communication Protocol
In order to ensure the universality of communication protocols between different devices and reduce the complexity of implementation, the communication protocol is divided into two layers: the upper layer is the application layer, which standardizes the application layer data format used by application programs for communication between devices; the lower layer is the link layer, which provides the sending and receiving of physical line data, application layer data splitting and merging, encapsulation and decapsulation, and error detection functions. The application layer part of the protocol uses different data formats according to the different functions of each device, while the link layer part is completely universal. The application layer communication process is shown in Figure 1.
Figure 1 Application layer communication process
The sending device constructs the application layer data according to the agreed application layer data format, and submits it to the link layer for data splitting, encapsulation, and verification, and then sends the generated data to the physical line in the form of data frames; the receiving device receives data from the physical line, performs frame positioning, decapsulation, error detection, data merging, etc., and finally submits the application layer data to the application layer for processing. The application layer of the sending and receiving devices can continuously modify the application layer data format according to changes in user needs, and use the functional interface provided by the link layer to complete the communication function. Therefore, the key to the design of the communication protocol lies in the link layer. The following focuses on the design and implementation of the link layer.
3 Link Layer Design
The link layer mainly includes the following functions: data splitting and merging, data encapsulation and decapsulation, sending and receiving of data frames, and error detection and retransmission mechanism.
3.1 Data Splitting and Merging
Data splitting is to divide the overlong application layer data into several parts, send them in multiple data frames, and then merge the data after receiving them, and hand them over to the application layer for processing. If the overlong application layer data is not split, it may cause the data frame to exceed the designed buffer size, or it may cause the sending time to be too long, resulting in a timeout error. This length needs to be reasonably set according to actual needs. When an error occurs in the data frame transmission, the frame of data needs to be retransmitted. If the length is too large, it will cause a large overhead; if the length is too small, the proportion of overhead bytes generated during encapsulation will be too high, affecting the transmission efficiency.
3.2 Data encapsulation and decapsulation
Data encapsulation is to add function indication, data length and other fields to the split application layer data in a certain format so that the other party knows how to process it after receiving it. The data encapsulation format and the meaning of the function indication field are shown in Table 1 and Table 2.
3.3 Frame sending and receiving
The link layer sends and receives data in frames. A common method to define the start and end of a frame is to add special start and end codes to the head and tail of the data to be sent. If this code pattern appears in the data, it must be escaped before the data is sent to convert it into other code patterns, otherwise it will cause frame positioning errors and data communication failure. Many protocol implementers do not perform this escape for the sake of simplicity, which poses a risk of communication failure. In fact, an escape processing method is described in the Point-to-Point Protocol (PPP protocol). After simplification, it is not complicated to implement. The description is as follows:
The data sender sends 0x7E as the start code at the beginning of the frame, and sends the encapsulated data byte by byte. When encountering 0x7E, it sends a byte sequence of 0x7D and 0x5E. When encountering 0x7D, it sends a byte sequence of 0x7D and 0x5D. Finally, it sends 0x7E as the end code at the end of the frame. [page]
The data receiver searches for the first 0x7E in the serial port data stream as the start of the frame (for consecutive 0x7E, the last one is the start of the frame), receives the data byte by byte, and when it encounters 0x7D, it skips it and adds 0x20 to the next byte of the byte until it encounters 0x7E, which is considered the end of the frame.
Before the link frame is sent, the CRC16 algorithm should be used to check the encapsulated data. The check polynomial is, and the check value is written into the check field. After the link frame is received, it is checked first. If the check succeeds, the data is decapsulated. If the check fails, it is resent according to the following retransmission mechanism.
3.4 Error Detection and Retransmission Mechanism
Considering the simplicity of protocol implementation and the reliability of data transmission and reception, it is decided to use the stop-and-wait protocol for data transmission and reception. The process is as follows:
After sending a data frame, the sender sets a maximum waiting time to wait for the other party's confirmation frame or rejection frame. If a confirmation frame is received, the next frame is sent; if a rejection frame is received or no confirmation frame or rejection frame is received within the timeout period, the current frame is resent. The frame resent due to waiting timeout must set the timeout indication bit. If a rejection frame is received three times in a row or a timeout is resent three times in a row, the other party is considered unreachable, the sending of the current frame is canceled, and the error is reported to the application layer.
After the receiver receives the data frame, when the timeout indication bit is 0, if the check is correct, a confirmation frame is sent and the frame is processed. If the check is wrong, a rejection frame is sent and the frame is not processed. When the timeout indication bit is 1, it means that the other party has not correctly received the confirmation frame or the rejection frame. If the check result of the last non-retransmitted frame is correct, the frame has actually been processed and a confirmation frame can be sent directly. If the check result of the last non-retransmitted frame is wrong, the frame is processed normally according to the check result.
4 Link Layer Implementation
The link layer is implemented in C++ language to facilitate code reuse in various module programs. The application layer data sending and receiving process is shown in Figure 2 and Figure 3.
5 Conclusion
The role of the link layer is to reliably send the application layer data to the peer device, but if that is all, the application is not very convenient to use. If the object-oriented programming method is used to encapsulate the link layer code in a class and provide the application with some relatively simple functional interfaces, such as sending data, receiving data, and detecting whether the peer is reachable, the usability problem can be well solved. In addition, when data transmission fails, the application should be notified by return value or event. When there is application layer data to be processed, it is best to activate the application layer handler by callback function or event to avoid inefficient loop detection of the application. By optimizing both the protocol design and protocol implementation, the protocol has shown excellent reliability and certain versatility in actual application, which can be used for reference.
Previous article:Embedded Internet Video Application Development Based on IPv6
Next article:Design of water-saving irrigation system based on ZigBee wireless sensor network
Recommended ReadingLatest update time:2024-11-16 14:42
- Popular Resources
- Popular amplifiers
- Mission-oriented wireless communications for cooperative sensing in intelligent unmanned systems
- ICCV2023 Paper Summary: Fairness, Privacy, Ethics, Social-good, Transparency, Accountability in Vision
- Introduction to Artificial Intelligence and Robotics (Murphy)
- Embedded system development tips, a novice\'s growth log and a project manager\'s recipe
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Choosing Machine Learning Processors at the Edge
- EEWORLD University Hall----MATLAB Applications Complete Book Video
- All-inclusive OLED
- c0000 28335 Program automatic upgrade problem
- TMS320C62x Boot Mode
- Any port generates a square wave with half duty cycle
- 【MicroPython】Using MCO as a clock
- Good news! Owning an ultra-high performance UXR series oscilloscope is no longer a dream!
- Brain-electromechanical control robot arm (robot arm for elderly care and disabled assistance)
- [NXP Rapid IoT Review] + Mobile Synchronizer 5