Detailed explanation of the principle of serial port communication

The serial interface is referred to as the serial port, also known as the serial communication interface (commonly referred to as the COM interface), which is an extended interface using serial communication.

The characteristics of serial communication are:

  • The data is transmitted bit by bit, and only one transmission line is needed to complete the one-way transmission and communication;
  • If there is a pair of transmission lines, two-way full-duplex communication can be realized (the telephone line can be directly used as the transmission line), which greatly reduces the cost and is especially suitable for long-distance communication, but the transmission speed is relatively slow;

1. Serial port division standard

Serial interfaces can be divided into synchronous serial interfaces and asynchronous serial interfaces according to different clock synchronization methods. Synchronous Serial Interface (SSI for short) is a commonly used industrial communication interface. The common SPI interface is also a kind of synchronous serial interface.

The asynchronous serial communication interface generally refers to the Universal Asynchronous
Receiver/Transmitter (UART for short). It includes interface standard specifications and bus standard specifications such as RS232, RS499, RS423, RS422 and RS485, that is, UART is the general term for asynchronous serial communication ports. When it comes to serial ports, if synchronous or asynchronous is not specified, it usually refers to an asynchronous serial interface.

UART itself does not specify electrical characteristics, but RS232, RS499, RS423, RS422 and RS485, etc., are interface standards and bus standards corresponding to various asynchronous serial communication ports, which stipulate the electrical characteristics, transmission rate, and connection characteristics of communication ports And the mechanical characteristics of the interface and so on. In fact, it belongs to the concept of the physical layer (lowest layer) in the communication network, and has no direct relationship with the communication protocol. The communication protocol is a concept belonging to the data link layer (upper layer) in the communication network.

(1) UART-TTL level standard: the logic "0" output level of the TXD/RXD/DTR/CTS signal is required to be less than 0.4V; the logic "1" output level is required to be greater than 2.4V, typically 3.4V, and the upper limit does not exceed TTL power supply 5V.

(2) UART-RS232 standard: RS (Recommended Standard) stands for the recommended standard, and 232 is the identification number. The RS232 standard mainly stipulates the definition of the finger level, the definition of the signal pin function, and the response protocol. RS232 is full-duplex, and only needs 3 signal lines at least, one data line is dedicated to sending data, the other data line is dedicated to receiving data, and there is a common ground line. But RS232 can only communicate with a single computer. The level defined by the RS232 interface standard is negative logic and has a large voltage range: "1" (-3V ~ -15V), "0" (+3 ~ +15V), so when the UART in a microcontroller is connected to the PC , it requires an RS232 driver to shift the level. RS232 level is the default standard of UART, in addition to TTL level and CMOS level, attention should be paid to matching in engineering applications. The TTL cable brush tool that DIY people often say is a conversion connector that transfers the TTL_UART port of the device to the USB port of the PC.

(3) UART-RS485 standard: RS485 is a bus-type interface specification that uses differential lines for serial data communication. When the communication distance is required to be tens of meters to thousands of meters, the RS-485 serial bus standard is widely used. RS-485 uses balanced transmission and differential reception, so it has the ability to suppress common mode interference. In addition, the bus transceiver has high sensitivity and can detect the voltage as low as 200mV, so the transmission signal can be recovered beyond a kilometer. RS485 adopts differential signal negative logic, +2V~+6V means "0", -6V~-2V means "1". RS485 has two kinds of wiring, two-wire system and four-wire system. The four-wire system can only realize point-to-point communication mode, which is rarely used now. Now, the two-wire system connection mode is mostly used, which is a bus topology. Up to 32 nodes can be connected on the same bus.

(4) UART-RS485 standard: In fact, it is RS485 with two pairs of differential lines, which can realize full-duplex communication.

2. Serial communication protocol

The UART protocol is a full-duplex asynchronous transceiver protocol. The data transmission signal line TX and the reception signal line RX work independently and in parallel without affecting each other. The transmission speed (baud rate), check bit type, and stop bit number of the communication parties need to be mutually agreed in advance, otherwise they cannot communicate correctly.

For each byte of data transmitted through the UART protocol, its synchronization as the start bit must be added in the front, and the parity bit and stop bit must be added in the back. The start bit is a logic "0" level, which requires that it must start from the falling edge, that is to say, the leading edge of the start bit must be "1" (stop bit or idle bit); and the stop bit has no leading edge requirements, as long as it is " 1" is enough. When not transmitting data, the data line must remain "1" (idle bit).

Note that except for the idle bit, the width of other bits is the same (equal to the baud rate). If the serial port data is sent continuously without delay, no idle bit will be inserted after the stop bit after each byte is sent.

UART data transmission timing:

Explanation on the concept of baud rate: baud rate (BaudRate) indicates the number of bits transmitted per second, and its unit is bps (bitper second). The bit here not only refers to the data bit, but also includes the start bit, checksum bit, stop bit, and the time width of each bit is 1/BaudRate.

Assuming that the serial port configuration is (9600bps, 1 start bit, 8 data bits, 1 odd parity bit, 1 stop bit, 11 bits in total), the number of bytes that can be transmitted per second is 9600/11=872 Bytes. 

3、UART-TTL

UART-TTL is a full-duplex UART interface using TTL level. That is to say, each I/O pin of the UART serial port, including TXD, RXD, DTR, CTS and other signal logic "0" output level requirements is less than 0.4V; logic "1" output level requirements are greater than 2.4V, typically 3.4V V, the upper limit does not exceed 5V of the TTL power supply.

Common physical forms of the UART-TTL interface are: USB to TTL serial cable (flash/download cable), USB to TTL serial adapter (USB Dongle), as shown in the figure below.

UART-TTL adapter:

Since UART-TTL application scenarios usually only need four wires RXD/TXD/GND/VCC, the common USB-to-TTL interface only leads out these four wires. 

4、UATT-RS232

UART-RS232 is a full-duplex UART interface with 232 levels. The level defined by the RS232 interface standard is negative logic and has a large voltage range: "1" (-3V ~ -15V), "0" (+3 ~ +15V). RS232 level is the default standard of UART. Due to the larger amplitude of RS232 level, it has larger noise margin and longer transmission distance than TTL level. The standard maximum transmission distance of the RS232 serial port is 15 meters, and the longest distance should not exceed 20 meters.

The interface form of UATT-RS232 is usually in the form of DB9, that is, a 9-pin D-shaped interface, and it is divided into two forms: male and female.

Male and female heads of UART-RS232-DB9 interface:

UART-RS232 serial port pin definition (DB9):

5、UART-RS485

UART-RS485 belongs to the bus type, and only needs two data lines at the minimum (differential signal: A line and B line). The transmission distance can reach 1200 meters.

The logic 1 (positive) of the RS485 level refers to the situation of voltage B>A, and the logic 0 (negative) refers to the situation of voltage A>B. And for the RS485 level transmitter, the transmitted differential level requires that the absolute value of the voltage difference between the A line and the B line is between 2v~6v; for the RS485 level receiver, it is only required to detect the voltage between the AB line The absolute value of the difference is not less than 200mv to represent the data, otherwise the bus is idle.

RS485 devices with a single set of differential signal lines can only communicate in half-duplex, that is, they can either receive data or send data at the same time (the switching of the input/output mode of the device is controlled by a dedicated pin). Since RS485 does not have a bus arbitration mechanism, only upper-layer software can ensure communication reliability. Therefore, users must follow two principles when programming: (a) "Occupy the bus only when data is to be sent (switch the device to send mode), and release the bus immediately after sending data (switch to input mode)"; ( b) Before occupying the bus, it is better to detect whether the bus is idle (for example, keep a low level or a high level for a certain period of time as an idle state). When RS485 communicates with PC, usually convert RS485 to RS232.

In order to suppress interference, RS-485 needs 2 termination resistors, which are connected to both ends of the transmission bus, and its resistance value is required to be equal to the characteristic impedance of the transmission cable, usually a resistance of 120 ohms. In short-distance transmission, no terminating resistor is required, that is, generally no terminating resistor is required below 300 meters.

6、UART-RS422

Both RS422 and RS485 are serial bus types with exactly the same electrical characteristics. The only difference is that RS422 has two pairs of differential lines, which can realize full-duplex communication; while RS485 has only one pair of differential lines, which can only communicate in half-duplex.

Guess you like

Origin blog.csdn.net/qq_35029061/article/details/130738565