Serial communication protocol--UART, RS-232, RS-485, RS-422

Recently, I have learned about microcontrollers and digital electronics, so I decided to systematically learn some communication protocols. The book at hand only introduces the relevant protocols of the serial port low-speed interface.

Serial communication
refers to transmitting data one bit at a time by using a data line (requiring a ground line or a control line), and each bit of data occupies a fixed length of time.
Features : Anti-interference, saving data lines, but slow speed

During serial communication: the data is arranged bit by bit, and the sending and receiving are controlled according to the timing signal. The sending end always determines the beginning and end of the data bits through sending, and the receiving end needs to sample and decode the data stream at appropriate time intervals.
Divided into: synchronous serial communication and asynchronous serial communication

The information frame of synchronous serial communication is composed of synchronous signal character (SYNC), data character and check character (CRC). When transmitting data, it is necessary to maintain strict synchronization between the sending clock and the receiving clock. However, due to the inconsistency of synchronization characters between different manufacturers, the scope of use is small.
Asynchronous serial communication has two important indicators: character frame format and baud rate. Data is usually transmitted in character frames in units of characters or bytes, and character frames are sent and received frame by frame. The transmit and receive clock sources are independent of each other.
character frame formatIt is: one start bit, eight data bits, one parity bit, and one stop bit.
Data bits and parity bits exist in different categories in different protocols.


Serial communication can be divided into simplex, half-duplex, and duplex according to the working mode.
Simplex - The roles of the sender and receiver are fixed;
Half-duplex (RS-485) - Data can be sent or received. There is an electronic switch for transmitting and receiving on each port, which generally cannot occur at the same time; Full-
duplex (PS-232) )—communication by two transmission lines that can send and receive at the same time.


Initialization of the communication port:
clarify the encoding and decoding methods, set the baud rate and other operations.
Baud rate : The number of bits sent per second. The baud rate of the sending device and the receiving device are different, which will cause decoding errors. The time to send each bit is about 0.1ms when the baud rate is 9600bps.
Data bits : The number of actual data bits in communication. How to choose the data bits depends on the number of bits of data you want to transmit.
Stop bit : used to indicate the end of data frame transmission and device correction clock synchronization, the typical value is 1 bit. The greater the number of stop bits, the greater the tolerance for clock synchronization of different devices.
Parity bit : There are three check modes, odd check, even check and no check. The idea of ​​odd and even parity is not to count the number of 0\1 in the data bits, butmingleOdd and even rules. Nowadays, most of them choose no verification.


RS – (Recommend Standard)
RS-232 standard (DB-9)
Insert image description here
generally has 9 pins. The communication range is within 15m. It is a single-ended signal transmission. There are problems such as common ground noise and the inability to suppress common mode interference. It is required during work. Ensure that the level is within ±(3-15)V. The meanings of each signal line are:
CD (Carried Detect): Carrier sense. Indicates whether the modem has detected the carrier signal from the communication device.
RD (Receive Data):Receive data. Represents a signal line that receives data from an external device.
TD (Transmit Data):send data. Indicates a signal line that sends data to an external device.
DTR (Data Terminal Ready): The data terminal is ready. Indicates that the computer is ready to receive or send data.
GND (Ground): ground wire. A common ground point for connecting circuits.
DSR(Data Set Ready): The dataset is ready. Indicates that the modem is up and ready for communication.
RTS(Ready to Send): ready to send. Indicates that the computer is ready to send data and asks the external device to be ready to receive.
CTS(Clear to Send): Clear to send. Indicates that the external device is ready to receive data from the computer
RI(Ring Indicator): Ring indication. A signal that indicates when there is an incoming call on the telephone line.

The main disadvantages of RS-232 are:
① The signal level of the interface is high, which easily damages the chip of the interface circuit, which is different from the TTL circuit; ② The
transmission rate is low, the baud rate is <115kbps;
③ Single-ended drive, easy to cause coherence If there is no mode interference, a filter needs to be added;
④The transmission distance is limited.


RS-485 standard
has a maximum transmission rate of 10Mb/s. It uses a combination of balanced driver and differential receiver. It has good anti-noise interference and has multi-machine communication capabilities. The transmission distance is up to 1200m (differential transmission). The difference between single-ended driver and balanced
driver See Du Niang for details


The RS-422 standard
is almost the same as RS-485. The difference is that:
RS-422 has four signal lines and can achieve full-duplex mode; the host-slave ratio can reach 32, and slaves cannot communicate with each other, so it is point-to-many. Two-way communication; the receiving end needs to be connected to a 120Ω terminal resistor, and the sending end can not be connected.


The difference between RS-232 and RS-485
The only difference is that RS-485 has a transmitter control signal DE (Drive Enable), while RS-232 does not need this signal.

RS-485 cannot receive and send at the same time and needs to switch between sending and receiving. The transmit signal TXD and the receive signal RXD of RS-232 each use one line, so there will be no conflict.
When converting RS-232 to RS-485, you can choose to transform the handshake signal RTS into a DE control signal and perform level conversion at the same time. Every time you write a serial communication program, you must first set RTS to be valid. After receiving the signal, set RTS to be invalid to avoid conflicts when multiple hosts are in the sending state at the same time on the same bus.
Insert image description here


UART serial port
Universal Asynchronous Receiver\Transmitter,Universal asynchronous sending and receiving equipment, used to support multi-machine communication, can be realized by setting the parameters of the SCON register.

Multi-machine communication needs to transmit an additional address code (ID). Before the master transmits data, each slave is in a state of standby to receive. The data stream sent by the host contains an address code. The serial port of each slave will receive the address code sent by the host. The program will determine whether it is the same as its own address code. If it is the same, it will start the program.

On a serial transmission line, each data stream contains 11 bits. The tenth bit is used to differentiate between address and data. When the microcontroller needs to perform multi-machine transmission, the transmission contains TB8 bits, and the reception contains RB8 bits. When TB8=1, it means that an address code is being transmitted, containing eight bits, and can connect 256 slaves. When TB8 =0, it means transmitting data. The receiving end can determine whether the transmitted address or data is transmitted through the format.

In the slave machine, usually withserial interruptway to treat incoming data. If the SM2 bit of the SCON register is set to 1, the serial interface is allowed to perform address judgment on multiple slaves. In this mode, the slave is only receiving address values ​​(its RB8=1 ), an interrupt request is generated.

In the serial interrupt service routine, as long as theSBUFRead back the address value, and make a judgment whether it is called. If it is YES, it will jump to execute the operation set by the program. If it is NO, it will not do any operation, then end the interrupt service routine and return to the main program. The specific slave is first set to SM2, and then starts sending or receiving data. The other slaves did not initiate transmission.

At this moment, it is still a one-to-one communication, but it is a one-to-many communication when the host sends the address at the initial stage.

The serial port of the single-chip microcomputer is TTL level, that is, 5 V, and the transmission distance is only 5 m at the farthest. If long-distance transmission is desired, the TTL level should also be converted to RS-485 level.


Learn the ModBus protocol another day.

Guess you like

Origin blog.csdn.net/Wangwenshuaicsdn/article/details/129938280