当前位置:网站首页>Detailed explanation of serial port communication principle 232, 422, 485

Detailed explanation of serial port communication principle 232, 422, 485

2022-06-26 05:51:00 self85

This paper introduces the basic concept of serial communication 、 data format 、 Communication mode 、 Typical serial communication standard, etc .

A serial port communication ,RS232,RS485, Stop bit , Odd check , Even check

1 A serial port communication
A serial port communication (Serial Communication), Between peripherals and computers , Through the data signal line 、 Ground wire, etc , A mode of communication in which data is transmitted bit by bit .
Serial port is an interface standard , It specifies the electrical standard of the interface , The interface plug-in cable and the protocol used are not specified .
Serial ports are divided according to electrical standards and protocols , Include RS-232-CRS-422RS485 etc. .

2 Data format of serial communication

 Insert picture description here
Character by character , Each character is transmitted bit by bit , And when transmitting a character , Always with “ Start bit ” Start , With “ Stop bit ” end , There is no fixed time interval between characters .
Each character is preceded by a starting bit ( Low level ), The character itself consists of 7 Bits consist of bits , Then the character is followed by a check digit ( The check bit can be odd parity 、 Even check or no check bit ), Finally, one or one and a half or two stop bits , The stop bit is followed by an indefinitely long free bit , Both the stop bit and the idle bit are specified as high . In actual transmission, the signal width of each bit is related to the baud rate , The higher the baud rate , The smaller the width , Before transmission , Both sides must use the same baud rate setting .

3 Communication mode
Simplex mode (Simplex Communication) The data transmission is unidirectional . Between the two sides of the communication , One party is fixed as the sender , One side is fixed as the receiver . Information can only be transmitted in one direction , Use a transmission line .
Half duplex mode (Half Duplex) Communication uses the same transmission line , It can send and receive data , But it cannot send and receive at the same time . Data transmission allows data to be transmitted in both directions , however , Only one of them can send data at any time , The other party receives data . Therefore, half duplex mode can use one data line , You can also use two data lines . In half duplex communication, each end shall have a transceiver switch , Determine the direction of data transmission through switching . Because there is a switch , So there will be a time delay , Information transmission efficiency is lower .
Full duplex mode (Full Duplex) Communication allows data to be transmitted in both directions at the same time . therefore , Full duplex communication is a combination of two simplex communication modes , It requires that both the transmitting device and the receiving device have independent receiving and transmitting capabilities . In full duplex mode , There are transmitters and receivers at each end , There are two transmission lines , High information transmission efficiency .
obviously , When other parameters are the same , Full duplex is faster than half duplex , Be efficient .

4 Even check and odd check
In standard ASCII In code , Its highest position (b7) Used as parity bit . So called parity , It refers to a method used to check whether there is an error during code transmission , Generally, it can be divided into odd check and even check . Odd check regulations : Correct code in a byte 1 The number of must be odd , If it's not an odd number , At the top b7 add 1; Even check regulations : Correct code in a byte 1 The number of must be even , If not even , At the top b7 add 1.

5 Stop bit
The stop bit is calculated by length . Serial asynchronous communication starts with timing , At intervals of unit time ( A unit time is the reciprocal of the baud rate ), Accept the specified data bits and parity bits in sequence , And assemble parallel bytes of one character ; After that, a stop bit of the specified length shall be received “1”. So , Stop bits are “1”,1.5 Is its length , That is, the high level of the stop bit remains 1.5 Unit length of time . In general , The stop bit has 1,1.5,2 Three lengths per unit of time .

6 Baud rate
Baud rate is the number of bits of data transmitted per second .
The baud rate is measured in bits per second (bps), Common units are : Kilobits per second Kbps, Megabits per second Mbps. Typical transmission baud rate of serial port 600bps,1200bps,2400bps,4800bps,9600bps,19200bps,38400bps.
PLC/PC When communicating with weighing instrument , The most commonly used baud rate is 9600bps,19200bps.PLC/PC Or when the instrument communicates with the large screen , The most commonly used baud rate is 600bps.

7 Typical serial communication standard
EIA RS232( Usually abbreviated to “RS232”): 1962 By the electronics industry association of America (EIA) To develop .
EIA RS485( Usually abbreviated to “RS485”): 1983 By the electronics industry association of America (EIA) To develop .

8 RS232 A serial port
RS232 It is the most widely used serial interface in computer and communication industry . It works in full duplex , Ground wire is required 、 The transmitting line and the receiving line .RS232 Only point-to-point communication can be realized .
8.1 RS232 Serial port disadvantages
● The interface signal level value is high , The interface circuit chip is easy to be damaged .
● Low transmission rate , Maximum baud rate 19200bps.
● The anti-jamming ability is poor .
● The transmission distance is limited , Generally in 15m within .
● Only point-to-point communication can be realized .

8.2 RS232 Serial interface definition
RXD: receive data ,TXD: send data ,GND/SG: Signally .
8.3 The computer DB9 Pin interface definition
The computer DB9 Pin connectors are common RS232 A serial port , The pin is defined as follows :
2 No feet :RXD( receive data )
3 No feet :TXD( send data )
5 No feet :SG or GND( Signally )
Other feet : We don't have to

 The computer RS232 Serial port and instrument serial port connection diagram :

 Insert picture description here
stay 9 Pin serial connector , The pin definition order of male and female heads is different , This needs special attention . that , What are the functions of these pins ?9 Pin serial port and 25 The function description of common pins of pin serial port is shown in the following figure :
 Insert picture description here

9 RS485 A serial port
9.1 RS485 Serial port features
●RS485 Balanced transmission and differential reception are adopted , Good anti-interference ability , The signal can be transmitted for thousands of kilometers .
●RS485 There are two wire system and four wire system . When the four wire system is adopted , Only point to many communication can be realized ( That is, there can only be one master device , The rest are slave devices ). The four wire system is rarely used nowadays , Now, the two-wire wiring mode is adopted .
● Two wire system RS485 It can only work in semi dual mode , Sending and receiving cannot be done at the same time .
●RS485 At most... Can be connected on the same bus 32 Nodes , Real multipoint communication can be realized , However, the master-slave communication mode is generally adopted , That is, one master with multiple slaves .
● because RS485 The interface has good anti-interference ability , Long transmission distance and multi station capability make it the preferred serial interface .
9.2 485 Schematic diagram of common mode interference suppression

9.3 RS485 Serial interface definition
A or Data+(D+) or +: The signal is positive ;
B or Data-(D-) or -: The signal is negative .
9.4 Computers and RS485 Instrument communication
The serial port of the computer is only RS232, No, RS485, If the computer wants to communicate with RS485 Serial port instrument for communication , You must use a serial port converter or install RS485 Communication can only be carried out after serial port conversion card .
9.5 RS485 Terminal resistance of serial port
● Generally, there is no need to increase the terminal resistance , Only in RS485 The communication distance exceeds 100 In the case of meters , To be in RS485 Increase the terminal resistance at the beginning and end of the communication ,RS485 Typical termination resistance is 120 o .
● The terminal resistance is to eliminate the signal reflection in the communication cable during the communication process , There are two kinds of signals that cause reflection : Impedance discontinuity and impedance mismatch .
The impedance is discontinuous , The signal suddenly meets the cable at the end of the transmission line with little or no impedance , The signal will cause reflection in this place . The way to eliminate this reflection , A terminal resistor of the same size as the characteristic impedance of the cable must be bridged at the end of the cable , Make the impedance of the cable continuous . Because the transmission of signal on the cable is bidirectional , therefore , A terminal resistor of the same size can be bridged at the other end of the communication cable .
Another cause of signal reflection is the impedance mismatch between the data transceiver and the transmission cable . The reflection caused by this reason , The main performance is when the communication line is in idle mode , The whole network is a mess of data . To reduce the impact of the reflected signal on the communication line , Noise suppression and bias resistance are usually used . in application , For smaller reflections , For simplicity and convenience , Bias resistance is often used .

10 Common precautions for serial communication hardware
● The communication cable terminals must be firmly connected , There must be no looseness , otherwise , It may burn the instrument or the communication board of the upper computer .
● Do not plug or unplug the communication terminal , otherwise , It may burn the instrument or the communication board of the upper computer , Be sure to turn off the power supply of the instrument before unplugging the communication terminal or connecting the communication line .

● Do not plug or unplug the communication terminal , otherwise , It may burn the instrument or the communication board of the upper computer , Be sure to turn off the power supply of the instrument before unplugging the communication terminal or connecting the communication line .
● The shielded cable for communication shall preferably be double-layer isolated shielded cable , Secondly, select single-layer shielded cable , It is better not to choose cables without shielding layer , And the cable shielding layer must be completely shielded , Some poor quality cables , The shielding layer is very loose , It has no shielding effect at all . One end of the single-layer shielded cable shield shall be grounded , The outer layer of the double-layer shielded cable shielding layer ( Including armor ) Both ends shall be grounded , The inner shield should be grounded at one end .
● Instrument use RS232 Communication time , The length of communication cable shall not exceed 15 rice .
● commonly RS485 There is no fixed standard for the connector of the protocol , The pin sequence and pin function may be different according to different manufacturers , Users can view related products RS485 Pin diagram of .
●RS485 It is better to select resistance matching for communication cable 、 Low attenuation RS485 Special communication cable ( Twisted pair ), Do not use ordinary twisted pair cables or communication cables of poor quality . Because of ordinary cable or poor quality communication cable , Possible impedance mismatch 、 Large attenuation 、 Insufficient stranding 、 The shielding layer is too loose , This will lead to very large interference , It will cause poor communication , Can't even communicate .
● Instrument use RS485 Communication time , Each instrument must be connected hand in hand , No star connections or forks , If there is a star connection or fork , The interference will be very large , It will cause poor communication , Can't even communicate .
●485 In theory, the transmission distance of the bus structure reaches 1200 rice , Generally, it refers to the high-quality and up to standard communication wires , Baud rate 9600, Only one 485 Equipment can make the communication distance reach 1200 rice , And being able to communicate doesn't mean that every communication is normal , So usually 485 The actual stable communication distance of the bus is far from 1200 rice . load 485 Many devices , Wire impedance is different , Shorter communication distance .
● Instrument use RS485 Communication time , When necessary, , Please connect the terminal resistor , To enhance the anti-interference of the system , Typical terminal resistance values are 120 o .

11 Key points for setting serial port communication software
11.1 Some basic concepts about communication
● Master and slave : Play a major role in the communication system 、 The person who issues the main command is called the host , The person who accepts the command is called a slave .
● Continuous mode : It means that the host does not need to issue commands , The slave can automatically send data to the host .
● Command mode : It refers to the command issued by the master to the slave , The slave executes actions according to instructions , And the result “ The reply ” Mode for the host .
● Output data type : When communicating in a continuous mode , The type of data output from the host to the host .
● Communication protocol : It refers to the communication between the master and the slave , According to which coding rule to communicate .
● Baud rate : The speed of communication between master and slave computers .
● Data bits : Every time you transmit data , The data consists of several digits .
● Check bit : Data transmission error detection , It can be odd check 、 Even check or no check .
● Address : The number of each slave .
11.2 Key points of communication setup between master and slave computers
● Key points : Lord / from RS232/485 Whether the hardware is set correctly , Whether the communication line is connected correctly . Some communication boards are RS422 And RS485 Shared , It depends on the patch cord on the board , Some instruments RS232/485 Communication jumper is also required to realize .
● Main points two : Whether the communication port on the host is set correctly ; Overtime ( Generally set as 2s)、 Communication delay ( Generally set as 5~20ms)、ACK signal delay ( Generally set as 0ms) Whether the setting is correct .
● Main points three : Lord / Whether the slave communication protocol is selected correctly .
● Point four : Lord / Whether the baud rate of the slave is selected correctly .
● Point five : Lord / Whether the slave data bit is correctly selected . Data bits can be selected 7 position ,8 position .
● Point six : Lord / Whether the slave check bit is correctly selected . Generally, even parity can be selected 、 Odd check 、 No verification .
● Point seven : Lord / Whether the slave stop position is selected correctly . The stop bit can be selected 1 position 、1.5 A still 2 position .
● Point eight : Whether the slave address is selected correctly .
● Point nine : Lord / Whether the communication mode of the slave is selected correctly .

When conducting communication test, the line test is often conducted , Test whether the serial port cable used is available , There are two methods as follows :

1 Connect the serial port cable to different serial ports , Use the serial port debugging tool to send data from a serial port , Another serial port line that can receive instructions normally is OK Of .

2 Short circuit one end of the serial port cable ( Use a metal handle 2,3 Pin connected ), Use a multimeter to measure the... At the other end 2,3 If No. 1 is normal, there will be a short-circuit alarm sound .

原网站

版权声明
本文为[self85]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180505047439.html