RS232, RS485, RS422, serial port
1, RS232 basics
The data transfer between the computer and the computer or between the computer and the terminal can be performed by serial communication and parallel communication. Since the serial communication method has less use lines and low cost, especially in remote transmission, it is widely used because it avoids inconsistencies in multiple line characteristics.
In serial communication, both parties are required to use a standard interface so that different devices can be easily connected for communication. The RS-232-C interface (also known as EIA RS-232-C) is currently the most commonly used serial communication interface.
RS-232-C is a serial physical interface standard developed by the Electronic Industry Association (EIA). RS is the abbreviation of English “Recommended Standard”, 232 is the identification number, C is the number of revisions, which represents the latest revision of RS232 (1969). Before that, there are RS232B, RS232A.
It was developed in 1970 by the Electronic Industries Association (EIA) in conjunction with Bell Systems, modem manufacturers, and computer terminal manufacturers for serial communications. Its full name is “the serial binary data exchange interface technology standard between data terminal equipment (DTE) and data communication equipment (DCE)”.
1.1, electrical characteristics
The EIA-RS-232C specifies electrical characteristics, logic levels, and various signal line functions.
On TxD and RxD: Logic 1 (MARK) = -3V ~ -15V, Logic 0 (SPACE) = +3 ~ +15V On RTS, CTS, DSR, DTR and DCD control lines:
Signal valid (on, ON state, positive voltage) = +3V ~ +15V
Invalid signal (open, OFF state, negative voltage) = -3V to -15V
Levels such as +-5, +-10, +-12, and +-15 are possible depending on the power supply of the device.
1.2, mechanical characteristics of the connector
Since RS-232C does not define the physical characteristics of the connector, various types of connectors such as DB-25, DB-15, and DB-9 have appeared, and their pin definitions are also different. Recently, 8-pin RJ-45 connectors have become more common, although their pin assignments vary widely.
The EIA/TIA 561 standard specifies a method for pin assignment, but the Yost Serial Device Wiring Standard (“Yost Serial Device Wiring Standard”), which is widely used on Unix computers, invented by Dave Yost, and many other devices. None of the above connection standards were adopted. The signal and pin assignments in the more used RS-232 are listed in the table below:
The signal is labeled from the perspective of the DTE device, the TD, DTR and RTS signals are generated by the DTE, and the RD, DSR, CTS, DCD and RI signals are generated by the DCE.
The RS-232 port of the PC is a 9-pin socket. Some devices are connected to the PC’s RS-232 interface. Because they do not use the other’s transmission control signals, only three interface lines are required, namely “transmit data TXD”, “receive data RXD” and “signal ground GND”.
The bidirectional interface can only be made with 3 wires because all signals of RS-232 share a common ground. The unbalanced circuit makes RS-232 very susceptible to the base point voltage offset between the two devices.
For the rising and falling periods of the signal, RS-232 has only relatively poor control ability, which is prone to crosstalk. RS-232 is recommended to communicate between short distances (within 15m). Due to the relationship of asymmetric circuits, RS-232 interface cables are usually not made of twisted pairs.
1.3, transmission cable
The data transmission rate specified by the RS-232-C standard is 50, 75, 100, 150, 300, 600, 1200, 2400, 4800, 9600, 19200 baud per second. The driver is allowed to have a capacitive load of 2500pF, and the communication distance will be affected. This capacitance is limited.
For example, when a 150pF/m communication cable is used, the maximum communication distance is 15m; if the capacitance per meter of cable is reduced, the communication distance can be increased. Another reason for the short transmission distance is that RS-232 is a single-ended signal transmission, which has common ground noise and cannot suppress common mode interference. Therefore, it is generally used for communication within 20m.
According to the RS-232C standard, the transmission cable length should be 50 feet when the symbol distortion is less than 4%. In fact, this 4% symbol distortion is very conservative. In practical applications, about 99% of users are It works in the range of 10-20% of symbol distortion, so the maximum distance in actual use will be much more than 50 feet. DEC Company of the United States has specified that the allowable symbol distortion is 10% and the following experimental results are obtained.
The No. 1 cable is a shielded cable, and the model number is DECP.NO.9107723. There are three pairs of twisted pairs. Each pair consists of 22# AWG, which is covered with a shielded net. Cable #2 is an unshielded cable. The model number is DECP.NO.9105856-04 is a 22#AWG four-core cable.
1.4, link layer
In the RS-232 standard, characters are transmitted one after another in a series of bits. The longest encoding format is the asynchronous start-stop asynchronous start-stop format, which uses a start bit followed by 7 or 8 data bits, which may be a parity bit, followed by two stop bits. So sending a character requires 10 bits, and a good result is to make the entire transmission rate, the rate of the transmitted signal, with 10 divisions.
Serial communication requires multiple settings in the software settings. The most common settings include baud rate, parity, and stop bits. The baud rate is the baud rate from one device to another, that is, how many bits per second per second (bit/s). Typical baud rates are 300, 1200, 2400, 9600, 19200, etc. bit/s. Generally, the devices at both ends of the communication must be set to the same baud rate, but some devices can also be set to automatically detect the baud rate.
Parity Parity is used to verify the correctness of the data. Parity is generally not used. If it is used, it can be either odd or even. Parity works by modifying each transmit byte (and also limiting the bytes sent).
If you do not make parity, the data will not be changed. In even parity, because the parity bit is set to 1 or 0 (usually the most significant or least significant bit), the data is changed so that all transmitted digits (including digits and check digits of the character) The number of “1” in the ) is an even number;
In odd parity, the number of “1”s in all transmitted digits (including digits and parity bits of characters) is odd. Parity can be used by the receiver to check if the transmission is sending a fault – if there is an error in the number of “1”s in a byte, then this byte must have an error in the transmission. If the parity is correct, then either no error has occurred or an even number of errors have occurred.
The stop bit is sent after each byte transfer and is used to help accept the signal side hardware resynchronization.
D/P/S is a regular symbolic representation in the serial communication software setup. 8/N/1 (very common) indicates 8 bit data, no parity, 1 bit stop bit. The data bits can be set to 7, 8, or 9, and the parity bits can be set to none (N), odd (O), or even (E), and the parity bits can use the bits in the data.
So 8/E/1 means a total of 8 data bits, one of which is used for parity. The stop bit can be 1, 1.5 or 2 bits (1.5 is used on a teletypewriter with a baud rate of 60 wpm).
1.5, transmission control
Additional settings are required when a handshake or data integrity check needs to be sent. The common combination is RTS/CTS, DTR/DSR or XON/XOFF (actually no special pins are inserted in the data stream without using the connector pins).
The receiver sends the XON/XOFF signal to the sender to control when the sender sends data, which is opposite to the direction in which the transmitted data is transmitted. The XON signal tells the sender that the receiver is ready to accept more data, and the XOFF signal tells the sender to stop sending data until it knows that the receiver is ready again. XON/XOFF is generally deprecated, and RTS/CTS control streams are recommended instead.
XON/XOFF is an in-band method that works between terminals, but it must be supported at both ends, and there may be confusion when starting up suddenly.
XON/XOFF can work on a 3-wire interface. The RTS/CTS was originally designed for teletypewriter and modem half-duplex cooperative communication, and it can only send data to one modem at a time. The terminal must send a request to send a signal and wait until the modem responds to clear the send signal. Although RTS/CTS achieves handshake through hardware, it has its own advantages.
1.6, the shortage of RS-232 standard
After many years of improvements in RS-232 devices and communication technologies, the communication distance of RS-232 has greatly increased. Due to the early appearance of the RS-232 interface standard, it is inevitable that there are deficiencies, mainly the following four points:
(1) The signal level of the interface is high, which is easy to damage the chip of the interface circuit. Because it is incompatible with the TTL level, it needs to use the level conversion circuit to connect with the TTL circuit.
(2) The transmission rate is low. When transmitting asynchronously, the baud rate is 20Kbps. Now with the new UART chip 16C550, etc., the baud rate is 115.2Kbps.
(3) The interface uses a signal line and a signal return line to form a common ground transmission form. This common ground transmission is prone to common mode interference, so the noise immunity is weak.
(4) The transmission distance is limited. The maximum transmission distance is 50 meters. In fact, it can only be used at about 15 meters.
2, RS485 foundation
In response to the limitations of the RS-232 serial port standard, RS-422 and RS-485 interface standards have been proposed. RS-485/422 adopts balanced transmission and differential reception to realize communication: the transmitting end converts the TTL level signal of the serial port into two outputs of differential signals A and B, and then reduces the differential signal at the receiving end after transmission through the cable. TTL level signal.
Since the transmission line usually uses twisted pair and differential transmission, it is extremely resistant to common mode interference. The bus transceiver is highly sensitive and can detect voltages as low as 200mV. Therefore, the transmission signal can be recovered beyond the kilometer.
2.1, electrical characteristics of RS-485
The driver can output a common mode voltage of ±7V
Receiver input resistance RIN ≥ 12kΩ
Input capacitance ≤ 50pF
When the number of nodes is 32 and the terminal resistance of 120Ω is configured, the driver can output at least 1.5V (the size of the terminating resistor is related to the parameters of the twisted pair used)
Transmitter: The logic “1” is represented by the voltage difference between the two lines + (2 to 6) V; the logic “0” is represented by the voltage difference between the two lines – (2 to 6) V.
The input sensitivity of the receiver is 200mV (ie (V+)-(V-)≥0.2V, indicating the signal “0”; (V+)-(V-)≤-0.2V, indicating the signal “1”)
2.2, transmission rate and transmission distance
The maximum transmission rate of RS-485 data is 10Mbps, and the maximum communication distance is about 1219M. The transmission rate is inversely proportional to the transmission distance. At 10Kb/S transmission rate, the maximum communication distance can be achieved.
However, since RS-485 often communicates with the RS-232 port of a PC, it is generally up to 115.2 Kbps. Because the too high rate will reduce the RS-485 transmission distance, it is often around 9600 bps or less.
2.3, network topology
The RS-485 interface is a combination of balanced driver and differential receiver. It has enhanced anti-common-mode dry capability, that is, good anti-noise interference. RS-485 uses half-duplex mode to support multi-point data communication.
The RS-485 bus network topology generally adopts a terminal-matched bus type structure. That is to say, each bus is connected in series by a bus, and a ring or star network is not supported. If you need to use a star structure, you must use a 485 repeater or a 485 hub. The RS-485/422 bus generally supports up to 32 nodes. If a special 485 chip is used, it can reach 128 or 256 nodes, and the maximum can support up to 400 nodes.
The international standard of RS-485 does not specify the interface connector standard of RS485, so it is possible to use terminal blocks or connectors such as DB-9 and DB-25.
3, RS422 basics
The electrical performance of RS-422 is similar to that of RS-485. The main differences are:
(1) RS-485 has 2 signal lines: both transmit and receive are A and B. Since the RS-485 receives and distributes two lines, it cannot be received and sent at the same time (half duplex).
(2) RS-422 has 4 signal lines: two transmissions (Y, Z) and two receptions (A, B). Since the RS-422 is sent and sent separately, it can be received and sent simultaneously (full duplex).
(3) RS-422 supporting multi-machine communication Short-circuit YA as RS-485, short-circuit RS-422 ZB as RS-485 B can be easily converted to RS-485.
Many people often mistake the RS-422 serial interface for the full-duplex version of the RS-485 serial interface. In fact, they have many differences in electrical characteristics, and the common-mode voltage range and receiver input resistance are different. These two standards apply to different application areas.
The RS-485 serial interface driver can be used in RS-422 serial interface applications because the RS-485 serial interface satisfies all RS-422 serial interface performance parameters, and vice versa. For RS-485 serial interface drivers, the common-mode voltage output range is between -7V and +12V; for RS-422 serial interface drivers, this performance is only ±7V. The minimum input resistance of the RS-422 serial interface receiver is 4KΩ, while the minimum input resistance of the RS-485 serial interface receiver is 12KΩ.
4, serial port and handshake basics
4.1, serial port basics
The serial port is a very common device communication protocol on the computer (not to be confused with the Universal Serial Bus or USB). Most computers contain two RS232-based serial ports. The serial port is also a common communication protocol for instrumentation equipment; many GPIB-compatible devices also have an RS-232 port. At the same time, the serial communication protocol can also be used to obtain data of the remote acquisition device.
The concept of serial communication is very simple. The serial port sends and receives bytes in bits. Although slower than parallel communication in bytes, the serial port can receive data with one line while using one line. It is simple and capable of long distance communication. For example, when IEEE488 defines the parallel traffic state, the specified device line must not exceed 20 meters, and the length between any two devices must not exceed 2 meters; for the serial port, the length can reach 1200 meters.
Typically, the serial port is used for the transmission of ASCII characters. Communication is done using 3 lines: (1) ground, (2) transmit, and (3) receive. Since serial communication is asynchronous, a port can send data on one line while receiving data on another line. Other lines are used for handshaking, but are not required. The most important parameters for serial communication are baud rate, data bits, stop bits, and parity. For two ports that are passing, these parameters must match:
4.1.1, baud rate
This is a parameter that measures the speed of communication. It represents the number of bits transmitted per second. For example, 300 baud means that 300 bits are transmitted per second. When we refer to the clock cycle, we mean the baud rate. For example, if the protocol requires 4800 baud, then the clock is 4800 Hz. This means that the serial communication has a sampling rate of 4800 Hz on the data line. Usually the telephone line has baud rates of 14400, 28800 and 36600. The baud rate can be much larger than these values, but the baud rate is inversely proportional to the distance. High baud rates are often used for communication between closely placed instruments, a typical example being communication with GPIB devices.
4.1.2, data bits
This is a parameter that measures the actual data bits in the communication. When a computer sends a packet, the actual data is not 8 bits, and the standard values are 5, 7, and 8. How to set it depends on the information you want to transfer. For example, the standard ASCII code is 0 to 127 (7 bits). The extended ASCII code is 0 to 255 (8 bits). If the data uses simple text (standard ASCII), then each packet uses 7-bit data. Each packet refers to one byte, including start/stop bits, data bits, and parity bits. Since the actual data bits depend on the choice of communication protocol, the term “package” refers to the case of any communication.
4.1.3, stop bit
Used to represent the last digit of a single package. Typical values are 1, 1.5 and 2 bits. Since the data is timed on the transmission line and each device has its own clock, it is likely that there is a small out-of-synchronization between the two devices in the communication. The stop bit therefore not only indicates the end of the transmission, but also provides the opportunity for the computer to correct the clock synchronization. The more bits that apply to the stop bit, the greater the tolerance of different clock synchronizations, but the slower the data transfer rate.
4.1.4, parity bit
A simple error detection method in serial communication. There are four ways to detect errors: even, odd, high, and low. Of course, there is no check digit. For the case of even and odd parity, the serial port will set the parity bit (one bit after the data bit), and use a value to ensure that the transmitted data has an even or odd logic high. For example, if the data is 011, then for the even parity, the parity bit is 0, and the number of bits that guarantee the logic high is an even number. If it is an odd parity, check bit 1 so that there are 3 logic highs. The high and low bits do not really check the data, simply set the logic high or the logic low check. This allows the receiving device to know the status of a bit and has the opportunity to determine if there is noise interfering with the communication or whether the transmitted and received data is out of sync.
4.2, basic knowledge of handshake
The RS-232 pass mode allows simple connection of three wires: Tx, Rx and ground. However, for data transmission, both parties must use the same baud rate for data timing. Although this method is sufficient for most applications, this use is limited for situations where the receiver is overloaded. At this time, the serial port handshake function is required. In this section, we discuss three of the most common forms of RS-232 handshake: software handshake, hardware handshake, and Xmodem.
4.2.1, software handshake
The first handshake we discussed was a software handshake. Usually used when the actual data is a control character, similar to how GPIB uses command strings. The necessary lines are still three: Tx, Rx, and ground. Because the control characters are no different from normal characters on the transmission line, the function SetXModem allows the user to enable or disable the user to use the two control characters XON and OXFF. These characters are sent by the receiver during communication, causing the sender to pause.
For example: Suppose the sender sends data at a high baud rate. During the transmission, the receiver finds that the input buffer is full because the CPU is busy with other work. In order to temporarily stop the transmission, the receiver sends XOFF. The typical value is decimal 19, which is hexadecimal 13, until the input buffer is empty.
Once the receiver is ready to receive, it sends XON. The typical value is decimal 17, which is hexadecimal 11, and communication continues. When the input buffer is half full, LabWindows sends XOFF. In addition, if the XOFF transfer is interrupted, LabWindows will send XOFF when the buffer reaches 75% and 90%. Obviously, the sender must follow this code to ensure that the transmission continues.
4.2.2, hardware handshake
The second is to use a hardware line handshake. Like the Tx and Rx lines, RTS/CTS works with DTR/DSR, one as an output and the other as an input. The first set of lines are RTS (Request to Send) and CTS (Clear to Send). When the receiver is ready to receive data, it sets the RTS line high to indicate that it is ready. If the sender is also ready, it sets CTS high, indicating that it is about to send data. Another set of lines is DTR (Data Terminal Ready) and DSR (Data Set Ready).
These are now mainly used for modem communication. Enable the serial port and modem to communicate their status. For example, when the modem is ready to receive data from the PC, it sets the DTR line high, indicating that the connection to the phone line has been established. The read DSR line is set high and the PC starts transmitting data. A simple rule is that DTR/DSR is used to indicate that the system is ready for communication, while RTS/CTS is used for the transmission of a single packet.
In LabWindows, the function SetCTSMode enables or disables the use of hardware handshaking. If CTS mode is enabled, LabWindows uses the following rules: When the PC sends data: The RS-232 library must detect the CTS line high before sending data.
When the PC receives data:
If the port is open and the input queue is available to receive data, the library function sets RTS and DTR high.
If the input queue is 90% full, the library function deasserts RTS but keeps DTR high.
If the port queue is nearly empty, the library function sets RTS high, but keeps DRT high.
If the port is closed, the library function deasserts RTS and DTR.
4.2.3, XModem handshake
The last discussed handshake is called the XModem file transfer protocol. This protocol is very common in modem communication. Although it is commonly used in modem communication, the XModem protocol can be used directly in other device communications that follow this protocol. In LabWindows, the actual XModem application is hidden from the user. As long as the PC and other devices use the XModem protocol, the LabMode XModem function is used in file transfer. The functions are XModemConfig, XModemSend and XModemReceive.
XModem uses protocols with the following parameters: start_of_data, end_of_data, neg_ack, wait_delay, start_delay, max_tries, packet_size. These parameters need to be confirmed by both parties. The standard XModem has a standard definition: however, it can be modified by the XModemConfig function to meet specific needs.
The use of these parameters is determined by the character neg_ack sent by the receiver. This informs the sender that it is ready to receive data. It starts to try to send, there is a timeout parameter start_delay; when the timeout attempt exceeds the max_ties number, or the start_of_data sent by the receiver is received, the sender stops trying. If start_of_data is received from the sender, the receiver will read the subsequent information packet.
The packet contains the number of packets, the complement of the number of packets as the error check, the actual packet of the packet_size byte size, and the sum check value for further error checking. After reading the data, the receiver will call wait_delay and then want the sender to send a response. If the sender does not receive a response, it resends the packet until it receives a response or exceeds the maximum number of retransmissions max_tries. If the response has not been received, the sender notifies the user that the transmission of the data has failed.
Since the data must be sent in packets of pack_size bytes, when the last packet is sent, if the data is not enough to fill a packet, the ASCII code NULL (0) bytes will be filled later. This results in more data being received than the original data. Do not use XON/XOFF in the case of XModem, because the number of packets sent by the XModem sender is likely to increase to the value of the XON/OFF control character, resulting in communication failure.