What are the MCU communication protocols?

In modern systems, most instruments and equipment are controlled through host computer software, which simplifies debugging and improves operational efficiency. This process involves communication between devices. Based on the author’s experience in developing several devices, this article summarizes common methods for writing communication programs, covering both upper and lower-level devices. **1. Custom Data Communication Protocol** The data protocol discussed here refers to a packet format built on top of the physical layer. The physical layer includes common communication methods such as RS232, RS485, infrared, fiber optics, and wireless. At this level, the underlying software provides two basic functions: sending one byte of data and receiving one byte of data. All higher-level protocols rely on these two operations. Data is typically transmitted in the form of packets, known as frames. Similar to the TCP/IP protocol in network communication, a reliable protocol usually consists of the following components: frame header, address information, data type, data length, data block, check code, and frame tail. The frame header and tail help ensure the integrity of the data packet. A fixed-length sequence is often used for these markers. The goal is to minimize the chance of random matching within the entire data stream. To achieve this, either reduce the probability of feature bytes matching or increase their length. In most cases, the first approach is preferred because the data is predictable, and we can manually choose unique feature bytes to avoid false matches. However, increasing the length of the feature bytes is also effective, especially when dealing with unpredictable data streams. Even if some matches occur, they are rare, and the check code can be used to verify the correctness of the received data. Address information is crucial in multi-device communication, allowing different terminals to be identified by their addresses. In one-to-many systems, only the destination address may be included. For many-to-many systems, both source and destination addresses are necessary. The data type, length, and content define the main data payload. The data type helps identify whether the next part contains a command or actual data. The data length specifies how many valid bytes follow. The check code ensures that the received data is correct and intact. It is typically calculated using the data type, length, and content. Common methods include simple checksums or more complex CRC algorithms, depending on performance and tolerance requirements. **2. Data Transmission Between Upper and Lower Devices** At the physical communication layer, two basic functions are available: sending and receiving one byte of data. These are the building blocks of all data transmission processes. When sending a packet, each byte is sent sequentially from the start to the end of the frame. In microcontroller-based systems, it's common to use direct serial port transmission to send a single byte. While this method requires the processor to handle the entire transmission, it allows immediate data output on the communication line. Another approach is interrupt-based transmission, where all data is stored in a buffer and sent via interrupts. This reduces CPU usage but introduces slight delays. For 51-series microcontrollers, direct transmission is often preferred due to its simplicity. Here’s an example of a function for sending a single byte in a 51-series microcontroller: ```c void SendByte(unsigned char ch) { SBUF = ch; while (TI == 0); TI = 0; } ``` On the host computer side, serial communication is usually buffered. There are three main approaches: using Windows’ built-in serial communication control, accessing the serial port via system APIs, or using a serial port class like `CSerialPort`. The latter is particularly useful for handling data efficiently. `CSerialPort` provides a `WriteToPort` function to send data after initialization. To avoid delays caused by buffering, you can enable the flush mechanism. **3. Data Reception and Protocol Parsing on the Lower Device** Receiving data on the lower device can be done in two ways: polling or using interrupts. While polling is straightforward, it consumes more CPU resources. Interrupt-based reception is generally more efficient and is recommended for better performance. Once data is received, the parsing process can be integrated into the interrupt handler or executed in the main program. For simpler protocols, parsing directly in the interrupt is acceptable. For more complex ones, storing the data in a buffer and parsing it later in the main loop is more efficient. For example, in a simple system, the entire protocol is processed in the serial interrupt. The packet format might look like this: ``` 0x55, 0xAA, 0x7E, 0x12, 0xF0, 0x02, 0x23, 0x45, SUM, XOR, 0x0D ``` Where: - `0x55, 0xAA, 0x7E` is the frame header. - `0x0D` is the frame end. - `0x12` is the destination address. - `0xF0` is the source address. - `0x02` is the data length. - `0x23, 0x45` are the data bytes. - `SUM` and `XOR` are the check codes. The protocol parser checks the integrity of the data and extracts relevant information. Once a complete frame is received and verified, the data is stored for further processing. **4. Data Reception and Command Processing on the Host Computer** On the host side, data reception follows a similar process, though the implementation varies based on the serial port interface. Using a dedicated thread for monitoring incoming data is recommended, especially for blocking read functions. The `CSerialPort` class sends a message (`WM_COMM_RXCHAR`) whenever a character is received. The parent process can then handle the data and perform protocol parsing. A sample response function could look like this: ```cpp LONG CWellInfoView::OnCommunication(WPARAM ch, LPARAM port) { BYTE rcvdat = (BYTE)ch; // Protocol parsing logic here } ``` This approach ensures real-time processing and efficient data handling. **5. Conclusion** The above outlines the basic structure of a communication system, which is simple yet functional. Real-world systems are more complex, involving features like packet responses, error handling, and delay compensation. With the right foundation, these challenges can be addressed, leading to a more stable and reliable system.

HP Probook 440 445 G8

HP Probook 440 445 G8,M23770-B31,M23769-001,M23770-001

S-yuan Electronic Technology Limited , https://www.syuanelectronic.com

This entry was posted in on