[An article to understand RS232 and RS485 communication]

An article to understand RS232 and RS485 communication

Preface :
Before learning RS232 and RS485 communication, in order for us to understand better, we must combine the knowledge of serial communication, so by the way, I will write down the commonly used communication methods.

1. Communication method

1.1. Classification of communication methods

(1) First of all, according to the different bits of data transmission, it can be divided into: serial communication and parallel communication.
Serial communication: The so-called serial communication is the transmission of one data line byte in units of one bit ;
parallel communication: As you can imagine, parallel communication means that multiple data lines can transmit each bit of data simultaneously .
Features :
Serial communication: usually point-to-point communication, with simple communication lines and low cost, suitable for long-distance transmission.
Parallel communication: characterized by fast transmission speed and suitable for short-distance transmission.
(2) According to the clock frequency (bit stream) of the data, it can be divided into: synchronous communication and asynchronous communication.
Synchronous communication: the data is transmitted on a clock line, and the clock frequency of the receiving end is required to be consistent with the clock frequency of the sending end . Often a continuous bit stream is sent;
asynchronous communication: both communication parties agree on the same clock frequency or baud rate, the data is divided into strict start bits, the data is, and the end flag bit; at the same time, the receiving end clock and the sending end clock are no longer required Frequency synchronization allows communication as long as the baud rate is under the same conditions. After the sending end sends a byte, it can send the next byte after an arbitrarily long time interval.
Features :
High efficiency of synchronous communication; low efficiency of asynchronous communication.
Synchronous communication is more complex, and the allowable error of the clocks of both parties is small; asynchronous communication is simple, and a certain error of the clocks of both parties is allowed.
Synchronous communication can be used for point-to-multipoint; asynchronous communication is only suitable for point-to-point.

1.2. Classification of serial communication

We know the general classification of communication methods, so in serial communication, there are also certain division methods: that is, the popular simplex, half-duplex, and full-duplex communication. Simplex Communication (Simplex Communication): refers to the message
only A working method that can transmit in one direction.
In simplex communication, the communication channel is one-way, and the sending end and receiving end are also fixed, that is, the sending end can only send information, but cannot receive information; the receiving end can only receive information, but cannot send information. Based on this situation, the data signal is transmitted from one end to the other end, and the signal flow is unidirectional.
For example: a radio station plays a broadcast and the audience receives information, but the audience cannot feedback information to the radio station through the radio channel.

Half-duplex Communication can achieve two-way communication, but it cannot be carried out in both directions at the same time and must be carried out alternately.
In this working mode, the sender can transform into a receiver; correspondingly, the receiver can also transform into a sender. But at the same time, information can only be transmitted in one direction. Therefore, half-duplex communication can also be understood as a simplex communication that switches directions.
For example: in a typical walkie-talkie, A can only speak to transmit information, and B can receive information; when B transmits information, A can only receive information. Not possible at the same time.

Full duplex communication means that at any time of communication, there is two-way signal transmission from A to B and B to A on the line. Full-duplex communication allows data to be transmitted in both directions at the same time, also known as two-way simultaneous communication, which means both parties to the communication can send and receive data at the same time. **In full-duplex mode, each end of the communication system is equipped with a transmitter and a receiver, so data can be controlled to be transmitted in both directions at the same time. The full-duplex mode does not require direction switching, so there is no time delay caused by switching operations, which is very beneficial for interactive applications that cannot have time delays (such as remote monitoring and control systems). This method requires both communication parties to have a transmitter and a receiver, and at the same time, two data lines are needed to transmit data signals. (Control and status wires may also be required, as well as ground wires).
In theory, full-duplex transmission can improve network efficiency, but in fact it is only useful when it is coordinated with other related equipment.
For example: twisted pair network cables must be used to enable full-duplex transmission, and the hub (HUB) connected in the middle must also be capable of full-duplex transmission; finally, the network operating system used must also support full-duplex transmission. operation, so that the power of full-duplex transmission can be truly unleashed.

2. Serial communication

First, let’s look at basic serial communication, because RS232 and RS485 communication are both variants of serial communication ;
the concept of serial communication (Serial Communications) is very simple, and the serial port sends and receives bytes bit by bit.
Serial communication uses TTL (Transistor-Transistor Logic) level standard logic
communication, which is mainly completed using three wires, namely ground wire, sending and receiving .
Because serial communication is asynchronous , a port can send data on one line while receiving data on another line.
Then we will focus on serial communication for easy understanding, as shown in the figure below:
Insert image description here
Then it is very simple for us to establish serial communication. It can be communication between two chips, or communication between a chip and a computer. Those who have studied microcontrollers will know it very well. The typical serial communication connection method is as shown in the figure below:
Insert image description here
Insert image description here
It is not difficult to see that communication is mainly completed using three wires, namely ground wire, sending and receiving . Among them, RX and TX on the hardware ( TX is the abbreviation of Transmission, which means the sending end of the data, that is, the data output from the computer or other data source. RX is the abbreviation of Reception, which means the receiving end of the data, which is the destination of the data. . ) needs to be cross-connected , what other conditions are needed?

Through the understanding of asynchronous communication mentioned in the above communication method , we can know that to implement serial communication, we also need to agree on a baud rate. That is to say, both parties agree on the same clock frequency or baud rate, and the data is divided into strict starting points. Bit, data is, end flag bit .

2.1. Baud rate

Since there is no clock signal in serial port asynchronous communication, the communication parties need to agree on the baud rate, that is, the length of each symbol, in order to decode the signal. Common baud rates are 4800, 9600, 115200, etc.
This is a parameter that measures the rate at which symbols are transmitted. It refers to the change in unit time after the signal is modulated, that is, the number of carrier parameter changes per unit time . For example, 240 characters are transmitted per second, and each character format contains 10 bits (1 start bit, 1 Stop bit, 8 data bits), the baud rate at this time is 240Bd, and the bit rate is 10 bits * 240 bits/second = 2400bps.
The baud rate can be generally understood as: the number of high and low levels that can be transmitted within 1 second.
Now that we know the baud rate, let's learn how the data is divided into start bits, data, and end flag bits?
In fact, these are all restricted by standards defined by scientists such as IEEE (Institute of Electrical and Electronics Engineers). Taking the basic TTL level standard as an example, the data is divided according to the frame format
Insert image description here
standard, as shown in the figure: According to the data bits Different combinations can transmit different data, and of course there are other frame formats, as shown in the figure:
Insert image description here
start bit, stop bit :
the data packet starts from the start bit and ends with the stop bit. The start signal is represented by data bits of logic 0, and the stop signal is represented by data bits of 0.5, 1, 1.5 or 2 logic 1, as long as both parties agree.
Valid data :
After the start bit is the main data content of the transmission, also called valid data, and its length is generally agreed to be 5, 6, 7 or 8 bits long.
Parity bit :
Since the transmission data is susceptible to external interference during the communication process, a parity bit is added after the valid data to solve the problem.
Verification methods include odd parity (odd), even parity (even), 0 parity (space), 1 parity (mark) and no parity (noparity).
Odd parity requires that the number of "1"s in the valid data and check bits is an odd number. For example, an 8-bit long valid data is: 01101001. At this time, there are 4 "1"s. In order to achieve the odd parity effect, the checksum bit is "1", and the final transmission is 8 bits of valid data plus 1 bit of check bit, a total of 9 bits.
Even parity is just the opposite. It requires that the number of "1"s in the valid data and check bits is an even number. In order to achieve the even check effect, the check bit is "0".
For 0 parity, no matter what data content is in the valid data, the check digit is always "0", and for 1 parity, the check digit is always "1".

2.2. High and low level range of serial port communication

When we know the agreed baud rate and frame format, taking the common 9600 baud rate, it means that 9600 high and low level changes can be transmitted within 1 second. The next step is to explore the high and low level range of serial port communication. Serial port The communication uses TTL (Transistor-Transistor Logic) level standard logic , as shown in the figure:
(TTL level signal stipulates that +5V is equivalent to logic "1", 0V is equivalent to logic "0" (using binary system) When representing data). Such data communication and level regulation method is called TTL (transistor-transistor logic level) signal system.) As can be seen from the
Insert image description here
figure, the minimum high-level output of the TTL level standard is 2.4V, and the maximum low-level output is 2.4V. The level is 0.4V; and under normal circumstances, we usually think of serial communication as using the high level as 5V and the low level as 0V for communication work, so this makes the level very susceptible to interference from external signals, for example, At this time, a section of static electricity is suddenly applied from the outside, and a certain bit of the interfering data bit is suddenly pulled high, so the communication distance of this section is very short. This proves that the communication under the TTL level standard logic is susceptible to external interference, and the transmission distance is very short. The distance is generally 1m, such as communication between two chips or between a chip and a computer. As shown in the figure:
Insert image description here
Insert image description here
To this end, scientists have proposed the RS232 standard in order to combine it with actual application scenarios.

2.3. RS232 standard

RS-232 (ANSI/EIA-232 standard) is the serial connection standard on IBM-PC and its compatible machines.
The RS-232 bus specifies 25 lines or 9 lines (commonly used), and includes two signal channels, namely the first channel (called the main channel) and the second channel (called the secondary channel). Full-duplex communication
can be achieved using the RS-232 bus . The main channel is usually used, and the secondary channel is rarely used. In general applications, full-duplex communication can be achieved by using 3 to 9 signal lines, and a simple full-duplex communication process can be achieved by using three signal lines (receiving line, transmitting line and signal line) . DB-9 pin connector, as shown in the picture:
Insert image description here

Data:
TXD (pin 3): Serial port data output (Transmit Data)
RXD (pin 2): Serial port data input (Receive Data)
Handshake:
RTS (pin 7): Send data request (Request to Send)
CTS (pin 8): Clear to Send
DSR (pin 6): Data Send Ready
DCD (pin 1): Data Carrier Detect (Data CarrierDetect)
DTR (pin 4): Data Terminal Ready
Ground :
GND (pin 5): Ground wire
Others:
RI (pin 9): Ring indication

We know before that serial communication is full-duplex communication. For example, the TTL level between microcontrollers is susceptible to external interference, and it can only be point-to-point communication, as shown in the figure: So how do we convert the serial communication TTL signal
Insert image description here
into What about RS232 communication?
(1) First of all, the RS232 standard uses negative logic transmission , which stipulates that the level of logic "1" is -3V~-15V, and the level of logic "0" is +3V~+15V. The purpose of selecting this electrical standard is to improve the anti-interference ability and increase the communication distance .
RS-232 has a noise margin of 2V, and the receiver will be able to recognize signals as high as +3V as a logic "0" and signals as low as -3V as a logic "1".
(The noise margin refers to: when the output of the previous stage is the worst, the maximum noise amplitude allowed to ensure the normal operation of the subsequent stage; in digital electronics: the larger the
noise margin, the greater the allowable noise. The better the circuit’s immunity to interference.)

(2) Then, after knowing the transmission method of RS232, we only need to add a level conversion chip, such as MAX232, on the basis of the original serial communication, as shown in the figure: Combined with the characteristics of RS232 negative logic transmission, when
Insert image description here
we The TTL gives the MAX232 chip a 5V voltage, and the MAX232 outputs a -12V; when the MAX232 receives 0V, it outputs +12V, as shown in the figure:
Insert image description here

Insert image description here

Based on the negative logic characteristics and the understanding of the above figure, we can get the range of high and low levels of RS232 standard communication (the level of logic "1" is specified to be -3V~-15 V, and the level of logic "0" is + 3 V~+15 V), as shown in the figure:
Insert image description here

Comparing the levels of the TTL standard and the RS232 standard, it can be seen that the RS232 transmission distance is longer (generally 15m). Similarly, when it is interfered by external pulses or signals, the redundancy is wider, which infers that the anti-interference ability has also been improved , but the corresponding rate will drop . As long as 20k (that is, the baud rate of 19200) is shown in the figure:
Insert image description hereInsert image description here

Insert image description here

RS232 interface, this interface can realize point-to-point communication, but this method cannot realize networking function. The subsequent emergence of RS485 solved this problem.

2.4, RS485 standard

EIA-485 (formerly called RS-485 or RS485) is a standard for multi-point communication with 2-wire, half-duplex, balanced transmission line electrical characteristics belonging to the physical layer of the OSI model . It can also achieve full-duplex by using four lines. . It is a standard jointly issued by the Telecommunications Industry Association (TIA) and the Electronic Industry Alliance (EIA).
RS-485 is different from RS-232. The 485 data signal uses a differential transmission method, also called balanced transmission.
RS485, like EIA-422, uses twisted pairs for high-voltage differential balanced transmission, and mostly uses shielded twisted pairs for transmission. This wiring method is a bus topology, and up to 32 nodes can be connected to the same bus, which is popular Master-slave mode communication .

So how do we convert serial communication TTL signals into RS485 communication?
Similar to RS232, you only need to add a 485 conversion chip on the basis of serial communication to enable the hardware to establish communication, as shown in the figure: The function of the 485 conversion chip is to convert TTL level signals
Insert image description here
into
differential signals, and the differential signals only need The two wires are represented here as A and B. They do not require a GND ground wire and are usually transmitted in the form of a well-known twisted pair. As shown in the figure:
(Supplement: The above signal conversions are mutual, and can be converted from TTL to 232/485, or from 482/232 to TTL) The
Insert image description here
logical relationship of the differential signal , when A is high level and B is low level , represents logic "0". On the contrary, when A is low level and B is high level, it represents logic "1", as shown in the figure: The
Insert image description here
advantage of differential signals is strong anti-interference ability, because the output signal is generated by A It is determined by the two wires A and B. The difference between A and B represents the logic "0/1", and they are twisted together in a twisted pair. Even if they are interfered, the two wires will be interfered at the same time, so their voltage difference The value can also remain essentially unchanged. (Writing this note, I really want to sigh, this is the power of differential signals, scientist yyds! Just kidding, haha.) As
shown in the figure:
Insert image description here
It is because of this feature that the communication distance of RS485 is also Even further, it can reach 1200m, and the maximum transmission rate can reach 10Mbps , as shown in the figure:
Insert image description hereInsert image description here

In addition, RS485 has another feature , which is mentioned in the concept. This wiring method is a bus topology and can connect up to 32 nodes on the same bus, which is the popular master-slave mode communication .
The serial communication and RS232 interface can only realize point-to-point communication and cannot realize the networking function. **RS485 can realize the networking communication of one master and multiple slaves. **That is, it can communicate with multiple devices.

3. Summary

Insert image description here
Whether it is serial communication, RS232 or RS485 communication, they only define the physical layer and distinguish their respective level standards and application scenarios. The principles are the same for our programming and have almost no impact. As long as you master simple serial communication, you can quickly get started with RS232, RS422, RS485, etc.
Finally, let’s briefly summarize the differences between them :

TTL RS232 RS485
transmission specification A standard technique for communication between parts of a device controlled by a computer processor Asynchronous serial port protocol Half-duplex synchronization protocol
Transmission distance 1 m 15m 1200m
Transmission rate Common baud rate ranges Up to 2Mbps(19200) Up to 10Mbps
Electrical characteristics 0V ~ 5V Logic 1: -15V ~ -3V Logic 0: +3V ~ +15V Logic 0: -6V ~ -2V Logic 1: +2V ~ +6V
Network Topology One-way communication or point-to-point connection One-way communication or point-to-point connection Multiple devices perform master-slave communication on the same bus

4. References

1. Baidu Encyclopedia
2. bilibili understand it in 5 minutes! The most essential difference between serial port RS232 and RS485!

Guess you like

Origin blog.csdn.net/m0_69455439/article/details/132515313