Serial communication basic wiring method (transfer)

The commonly used serial ports are 9-pin serial port (DB9) and 25-pin serial port (DB25). When the communication distance is short (<12m), the cable can be directly connected to the standard RS232 port (RS422, RS485 is farther). Far, an additional modem (MODEM) is required. The simplest and most commonly used method is the three-wire system connection method. That is, the ground, the received data, and the transmitted data are connected in three feet. This paper only involves the most basic connection method and is directly connected with RS232.
1. Common signal pins for DB9 and DB25 Description 9-pin serial port (DB9) 25-pin serial port (DB25)
PIN No. Function Description Abbreviation PIN No. Function Description Abbreviation
1 Data carrier detection DCD 8 Data carrier detection DCD
2 Receive data RXD 3 Receive data RXD
3 Send data TXD 2 Send data TXD
4 Data Terminal Preparation DTR 20 Data Terminal Preparation DTR
5 signal ground GND 7 signal ground GND
6 Data Equipment Ready for DSR 6 Data Ready for DSR
7 Request to send RTS 4 Request to send RTS
8 Clear Send CTS 5 Clear Send CTS
9 Ring indicator DELL 22 Ring indicator DELL
2. RS232C serial communication connection method (three-wire system)
First of all, the serial data transmission can be realized only by receiving data pins and sending pins: the receiving and sending legs of the same serial port are directly connected by wires, two serial ports are connected, or one serial port is connected to multiple serial ports. The receiving terminal of the same serial port Connect directly to the sending foot with a 9-pin serial port and a 25-pin serial port, both of which are directly connected to 2 and 3;
· Two different serial ports (either two serial ports on the same computer or serial ports on different computers)
The above table is for the microcomputer's standard serial port, there are many non-standard devices, such as receiving GPS data or electronic compass data, as long as one principle is remembered: receive data pin (or line) and send data pin (or line) Connected, cross each other, the corresponding signal ground, can be victorious.
3. Several points to note in serial debugging:
When debugging the serial port, prepare a debugging tool that is easy to use, such as serial debugging assistant, serial port wizard, etc., which has a multiplier effect; it is strongly recommended not to plug the serial port with power, and at least one end of the insertion is power-off, otherwise the serial port is easily damaged.
The definitions of simplex, half duplex, and full duplex are called simplex if the information can only be transmitted from one party A to the other party B at any time in the communication process.
If at any time, the information can be transmitted from A to B, and from B to A, but can only exist in one direction of transmission, called half-duplex transmission.
If there is an A to B and B to A bidirectional signal transmission on the line at any time, it is called full duplex.
The telephone line is a two-wire full-duplex channel. Due to the use of echo cancellation techniques, bidirectional transmission signals are not confused. Duplex channels sometimes also separate the transmit and receive channels and use separate lines or bands to transmit signals in opposite directions, such as backhaul.
During the transmission of the parity serial data, errors may be caused due to interference. For example, if the character 'E' is transmitted, the bits are:
0100,0101=45H
D7 D0
Due to interference, the bit may be changed to 1. In this case, we call it a "error." How do we find errors in the transmission, called "error detection." After finding an error, how to eliminate the error is called "correction."
The simplest error detection method is "parity", which is to send 1 odd/even parity bit in addition to the bits of the transmitted character. Odd or even parity can be used.
Odd check: In all transmitted digits (including digits and check digits of characters), the number of "1"s is odd, such as:
1 0110,0101
0 0110,0001
Even parity: In all transmitted digits (including digits and check digits of characters), the number of "1"s is even, such as:
1 0100,0101
0 0100,0001

Parity can detect partial errors in the information transmission process (1 bit error can be detected, 2 bit error and 2 or more bit errors cannot be detected), and at the same time, it cannot correct errors. After finding an error, you can only request retransmission. However, due to its simple implementation, it has been widely used.
Some error detection methods have automatic error correction capabilities. Such as cyclic redundancy code (CRC) error detection.
Serial communication flow control We often see RTS/CTS and XON/XOFF options in serial communication processing. This is the two flow control options. Current flow control is mainly used in modem data communication, but Ordinary RS232 programming, to understand a bit of this knowledge is good. So, what is the role of flow control in serial communications and how do you program serial communication applications? Here we talk about this issue.

1. The role of flow control in serial communication The "stream" mentioned here is, of course, referring to the data flow. When data is transmitted between two serial ports, data loss often occurs, or the processing speeds of the two computers are different, such as communication between the desktop and the microcontroller, and the data buffer at the receiving end is full. The data will be lost. Now that we are using MODEM for data transmission on the network, this problem is particularly acute. Flow control can solve this problem. When the data processing at the receiving end does not come, a "no longer received" signal is sent and the sending end stops sending until it receives the "can continue sending" signal and then sends the data. Therefore, flow control can control the process of data transmission and prevent the loss of data. The two types of flow control commonly used in PCs are hardware flow control (including RTS/CTS, DTR/CTS, etc.) and software flow control XON/XOFF (continued/stopped), as described below.

2. Hardware flow control Hardware flow control is commonly used RTS/CTS flow control and DTR/DSR (data terminal ready/data set ready) flow control.
Hardware flow control must be connected to the corresponding cable. When using RTS/CTS (Request to Send/Clear to Send) flow control, the RTS and CTS lines at both ends of the communication should be connected to each other. The data terminal equipment (such as a computer) should use RTS to start The data stream from the modem or other data communication device, while the data communication device (such as a modem) uses the CTS to start and pause the data stream from the computer. The process of this hardware handshake mode is: we set a high bit flag (which can be 75% of the buffer size) and a low bit flag (which can be 25% of the buffer size) according to the buffer size of the receive buffer when programming. When the amount of data in the area reaches a high level, we set the CTS line to low level (to send logic 0) at the receiving end. When the program at the sending end detects that CTS is low, it stops sending data until the amount of data in the buffer at the receiving end is low. At low level, set CTS high. RTS is used to indicate whether the receiving device is ready to receive data.
Commonly used flow control also has DTR/DSR (Data Terminal Ready/Data Set Ready). We will not elaborate here. Due to the diversity of flow control, I personally believe that when using flow control in software, a detailed explanation should be given, how to connect, and how to apply.

3. Software flow control Because of the limitation of the cable line, we generally do not use the hardware flow control in the ordinary control communication, but use the software flow to control. Software flow control is generally achieved through XON/XOFF. Commonly used method is: When the amount of data in the input buffer of the receiving end exceeds the set high level, the XOFF character is sent to the data sending end (Decimal 19 or Control-S, device programming instructions should be elaborated), the sending end receives Immediately after the XOFF character stops sending data; when the data volume in the input buffer of the receiving end is lower than the set low level, it sends an XON character (decimal 17 or Control-Q) to the data sending end, and the sending end receives the XON character. Immediately after the start of data transmission. You can usually find out what character is sent from the device supporting source program.
It should be noted that if binary data is transmitted, flag characters may also appear in the data stream and cause erroneous operations. This is a defect in software flow control, and hardware flow control does not have this problem.