Serial port flow control software flow control and hardware flow control

This article is mainly reproduced from Baidu Library http://wenku.baidu.com/view/1fddeb262f60ddccda38a097.html

In serial communication processing, we often see two options, rts / cts and xon / xoff, which are two options for flow control. Currently, flow control is mainly used in data communication of modems. So, what is the role of flow control in serial communication, and how is it applied in the preparation of serial communication programs? Let's talk about this issue below.

1. The role of flow control in serial communication

The "stream" mentioned here, of course, refers to the data stream. When the data is transferred between the two serial ports, the phenomenon of data loss often occurs, or the processing speed of the two computers is different, such as the communication between the desktop computer and the single chip computer, the data buffer at the receiving end is full, then continue to send at this time Incoming data will be lost. Now we are transmitting data via modem on the network, this problem is particularly prominent. Flow control can solve this problem. When the data at the receiving end cannot be processed, it sends a "no longer receive" signal, and the sending end stops sending until it receives the "can continue to send" signal before sending data. Therefore, flow control can control the process of data transmission and prevent data loss. The two types of flow control commonly used in PCs are hardware flow control (including rts / cts, dtr / cts, etc.) and software flow control xon / xoff (continue / stop), which are explained separately below.

 

2. Hardware flow control

The hardware flow control commonly used are rts / cts flow control and dtr / dsr (data terminal ready / data set ready) flow control.

Hardware flow control must connect the corresponding cable line. When using rts / cts (request to send / clear to send) flow control, the rts and cts lines at both ends of the communication should be connected accordingly. Data terminal equipment (such as computers) use rts to start Start the data flow of the modem or other data communication equipment, and the data communication equipment (such as modem) uses cts to start and pause the data flow from the computer. The process of this hardware handshake method is:

When programming, we set a high bit flag (can be 75% of the buffer size) and a low bit flag (can be 25% of the buffer size) according to the buffer size of the receiving end. When the amount of data in the buffer reaches the high bit, we Set the cts line low (send logic 0) at the receiving end. When the program at the sending end detects that cts is low, it stops sending data until the amount of data in the receiving buffer is lower than the low bit and sets cts to high . rts is used to indicate whether the receiving device is ready to receive data. Commonly used flow control is also dtr / dsr (data terminal ready / data set ready).

3. Software flow control

Due to the limitation of the cable, we generally do not use hardware flow control but software flow control in ordinary control communication. Software flow control is generally achieved through xon / xoff. The common method is: when the amount of data in the input buffer of the receiving end exceeds the set high bit, the xoff character is sent to the data sending end (19 decimal or control-s, the device programming manual should be elaborated in detail) After the xoff character, it immediately stops sending data; when the amount of data in the input buffer of the receiving end is lower than the set low bit, the xon character (decimal 17 or control-q) is sent to the data sending end. The data will be sent immediately afterwards. Generally, you can find out what characters are sent from the source program of the device.

It should be noted that if binary data is transmitted, logo characters may also appear in the data stream and cause misoperation. This is a defect of software flow control, and hardware flow control does not have this problem.

 

 

The following is an understanding of serial port flow control from another blog. It is also reproduced, but I did not find the original text, so I posted his address.

http://blog.csdn.net/bianhonglei/article/details/8525971

Serial flow control:

 

A and B are used to indicate two devices that communicate with each other through a serial port. A sends data to B, and uses hardware RTS / CTS as the hardware flow control mechanism.

A wants to send data, that is, Request To Send (data), B sees that RTS is valid, and decides that if he wants to do preparation work, he sets CTS to be invalid. If he is ready, he sets CTS, Clear To Send means that for your Send (data), I have cleared it. So A can send data after seeing that CTS is valid. Then every subsequent byte data sent from A to B is such a process. In the middle, it may be said that B's buffer full buffer is full, so CTS must be set to be invalid. After A finds it, it stops sending data and continues to detect CTS until it is valid before continuing to send data. After the normal data transmission is completed, A clears the initial RTS flag, which means that the RTS is invalid, indicating that the data has been transmitted. Thus, the entire process of A sending data to B is over.

 

Published 314 original articles · Like 93 · Visit 210,000+

Guess you like

Origin blog.csdn.net/qq_18671205/article/details/105118514