Serial Port is short for serial port, also known as serial communication interface or COM interface.
Serial communication refers to the communication mode that uses the serial communication protocol (Serial Communication) to transmit data from one bit to one bit on a signal line.
Serial Port according to electrical standards and protocols, including RS-232-C, RS-422, RS485 and so on.
1. Serial Communication
In serial communication, data is transmitted on a single line with a 1-Bit Width. Data in one byte is transmitted eight times in sequence from low to high.
Data in serial communication is transmitted by bit. Each bit sent by the sender has a fixed interval, this requires the receiver to receive each bit at the same time interval of the sender. In addition, the recipient must be able to determine the start and end of an Information Group.
Two common basic serial communication methods are synchronous communication and asynchronous communication.
1.1 Synchronous Serial Communication
Synchronous communication (Sync: synchronous data communication) means that at the agreed communication rate, the clock signal frequency and phase of the sending end and the receiving end are always consistent (synchronous ), this ensures that both parties have a completely consistent timing relationship when sending and receiving data.
Synchronous communication groups many characters into an information group (Information Frame). The start of each frame is indicated by synchronous characters. One communication only transmits one frame of information. When transmitting data, you also need to transmit clock signals so that the receiver can use the hour-hand signal to determine each information bit.
The advantage of synchronous communication is that the number of digits for transmitting information is almost unlimited. The data transmitted in a single communication involves dozens to thousands of bytes, which leads to high communication efficiency. The disadvantage of synchronous communication is that the precise synchronization clock is always required in the communication, that is, the sending clock and the receiving clock must be strictly synchronized (the common practice is that two devices use the same clock source ).
In subsequent serial communication and programming, we will only discuss asynchronous communication methods, so we will not repeat the synchronous communication too much here.
1.2 Serial Asynchronous Communication
Async: Asynchronous Data Communication (async), also known as start-stop asynchronous communication, is transmitted in characters. There is no fixed interval between characters, each character is transmitted at a fixed time.
In asynchronous communication, the receiving and receiving sides can achieve synchronization by setting the Start and Stop bits in the character format. Specifically, before a valid character is officially sent, the sender first sends a start bit, then sends a valid character bit, and then sends a stop bit at the end of the character. The start bit to the stop bit constitutes a frame. The interval between the stop bit and the next start bit is an indefinite idle bit, and the specified start bit is low (the logical value is 0). The stop bit and idle bit are both high (the logical value is 1 ), in this way, the start position must have a lower hop edge, which can mark the start of a character transmission. The definition and synchronization of characters are easily implemented based on the Start and Stop bits.
Obviously, when asynchronous communication is adopted, the sender and receiver can control data sending and receiving by their respective clocks. These two clock sources are independent of each other and can be synchronized with each other.
The following describes the data sending and receiving processes of asynchronous communication.
1.2.1 Asynchronous Communication Data Format
Before introducing the data sending and receiving processes of asynchronous communication, it is necessary to clarify the data format of asynchronous communication.
Asynchronous Communication requires that the data format transmitted is composed of start bit, data bit, parity bit, and stop bit, 1 (the parity bit is not shown in this figure, because the parity bit is not mandatory. If there is a parity bit, the parity bit should be after the data bit and before the stop bit ).
Figure 1 Asynchronous Communication Data Format
(1) Start bit: the start bit must be a logical 0 level lasting a bit time, marking the start of a character transmission. the receiver can use the start bit to synchronize its receiving clock with the data of the sender.
(2) Data bit: The data bit follows the start bit, which is truly valid information in communication. The number of digits of a Data bit can be agreed by both parties. Generally, it can be 5, 7, or 8 bits, and the standard ASCII code is 0 ~ 127 (7 digits), the extended ASCII code is 0 ~ 255 (8 digits ). When data is transmitted, the system first transmits the low-level characters and then the High-Level characters.
(3) parity bit: Only one parity bit is used for odd or even checks. The parity bit is not mandatory. For odd parity check, ensure that the transmitted data has an odd number of logical highs. For even parity check, ensure that the transmitted data has an even number of logical highs.
For example, assume that the transmitted data bit is 01001100. For odd verification, the odd verification bit is 0 (Ensure that there are an odd number of 1 in total). For even verification, the even check bit is 1 (make sure there are an even number of 1 in total ).
It can be seen that the parity bit is only a simple logic high or a logic low level for data, and does not actually judge the data. The advantage of this is that the receiving device can know the status of a single bit, it is possible to determine whether noise interferes with communication and data synchronization.
(4) Stop bit: The stop bit can be 1, 1.5, or 2, which can be set by the software. It must be a logical level 1, marking the end of a character transmission.
(5) idle bit: the idle bit starts from the stop bit of a character to the Start bit of the next character, indicating that the line is idle and must be filled with a high level.
1.2.2 data sending process of Asynchronous Communication
After clarifying the data format of asynchronous communication, you can send data according to the specified data format. The specific steps for sending data are as follows:
(1) After initialization or when no data needs to be sent, the sender outputs logic 1, which can have any number of idle spaces.
(2) When data needs to be sent, the sender first outputs logic 0 as the start position.
(3) Then you can start to output the data bit. The sender first outputs the data bit D0, then D1, and finally the highest bit of the data.
(4) If there is a parity check bit, the sender outputs the check bit.
(5) Finally, the sending end outputs the stop bit (logic 1 ).
(6) If no information needs to be sent, the sending end outputs logic 1 (idle bit). If there is information to be sent, the transfer is performed in step (2 ).
1.2.3 Asynchronous Communication Data receiving process
In asynchronous communication, the receiver determines the time length of each bit based on the receipt clock and baud rate factor. The following example uses the baud rate factor equal to 16 (the receiving clock shifts the receiving shift register once every 16 clock cycles.
(1) Start communication. The signal line is idle (logic 1). When a hop from 1 to 0 is detected, the receiving clock is counted.
(2) When 8 clocks are counted, the system checks the input signal. If the input signal is still low, it is confirmed that this is the start position rather than the interference signal.
(3) After the receiving end detects the start bit, it detects the input signal once every 16 receiving clocks and uses the corresponding value as the D0 BIT data.
(4) Check the input signal once every 16 receiving clocks, and use the corresponding value as the D1 data until all data bits are input.
(5) Check the parity test bit.
(6) After receiving the specified number of data bits and check bits, the communication interface circuit wishes to receive the Stop bits (logical 1). If logic 1 is not received at this time, an error occurs, set the "frame error" flag in the Status Register. If there is no error, perform parity check on all data bits. If there is no verification error, extract the data bits from the shift register and send them to the data input register, if the verification is incorrect, set the "parity error" flag in the Status Register.
(7) after all the frame information is received, the High Level on the line is used as the idle bit.
(8) When the signal becomes low again, the next frame is detected.
The above is the whole process of data sending and receiving in asynchronous communication.
1.3 concepts
To better understand the serial communication, we also need to understand several basic concepts in the serial communication.
(1) Sending clock: when sending data, the data to be sent is first sent to the shift register, and then the parallel data is output in a position by position under the control of the sending clock.
(2) receiving clock: When receiving serial data, the rising edge of the receiving clock samples the received data, detects the data bit, and moves it into the shift register of the receiver, finally, it forms parallel data output.
(3) baud rate factor: the number of clock pulses required to send or receive one data bit.
2. serial port connection
There are two kinds of commonly used serial port, one is 9-pin serial port (DB-9), one is 25-pin serial port (DB-25 ). Each connector has a public header and a mother header, wherein a needle-shaped joint is a public header, and a hole-shaped joint is a mother. The appearance of the 9-pin serial port is shown in 2.
Figure 2 DB-9 exterior Diagram
From figure 2, we can see that in the 9-pin serial port connector, the pin definitions of the public and mother heads are different, which requires special attention. So what are the functions of these pins? The functions of the 9-pin serial port and the 25-pin serial port are described in 3.
Figure 3 9 common pin functions of needle serial port and 25-needle serial port
3. RS-232C standards
Commonly used serial communication interface standards are RS-232C, RS-422, RS-423 and RS-485. The RS-232C, as the electrical standard of the serial communication interface, defines the data terminal device (DTE: data terminal equipment) and data communication equipment (DCE: data communication equipment) the interface information transmitted by bit is reasonably arranged for the electrical signal and mechanical requirements of the interface, which has been widely used in the world.
3.1 Electrical Properties
RS-232C of electrical properties, logical power and various signal functions are defined, as follows:
On the txd and rxd data lines:
(1) logic 1 is-3 ~ -15 V voltage
(2) logic 0 is 3 ~ 15 V voltage
On the control line such as RTS, CTS, DSR, DTR, and DCD:
(1) The signal is valid (on status) 3 ~ 15 V voltage
(2) The signal is invalid (off status)-3 ~ -15 V voltage
It can be seen that the RS-232C is to use positive and negative voltage to represent the logic state, and the transistor-transistor logical Integrated Circuit (TTL) in high and low level to express the logic state of the provision is exactly the opposite.
3.2 signal line distribution
The RS-232C standard interface has 25 lines, of which 4 lines, 11 lines of control, 3 scheduled lines and 7 standby and undefined lines. How are these signal lines allocated on the pins of the 9-pin and 25-pin serial ports? 9-pin serial port and 25-pin serial port signal line distribution 4.
Figure 4 Distribution of needle serial port and 25-needle serial port signal lines
Next we will give a brief introduction to these signal lines.
(1) When the data device is ready (DSR), the effective State (on) indicates that the data communication device is in usable state.
(2) When the data terminal is ready (DTR), the valid state (on) indicates that the data terminal device is in usable state.
The status signals of the two devices are valid. They only indicate that the device is available. They do not indicate that the communication link can start to communicate. Whether the communication can start depends on the following control signals.
(3) request sending (RTS) is used to indicate that the data terminal device (DTE) requests the data communication device (DCE) to send data.
(4) allowed sending (CTS) indicates that the data communication device (DCE) has prepared data and can send data to the data terminal device (DTE, is the response to the request to send a signal to the RTS.
Request sending (RTS) and allowed sending (CTS) are used in half-duplex communication systems. In full-duplex systems, requests are not required to send (RTS) and requests are allowed to send (CTS) signals, set it to on directly.
(5) data carrier detection (DCD) indicates that the data communication device (DCE) is connected to the communication link and informs the data terminal device (DTE) to prepare for receiving data.
(6) ringing indication (RI): When the data communication device receives the ringing call signal sent from the exchange station, the signal is valid (on) and the notification terminal has been called.
(7) send data (txd). The data terminal device (DTE) sends serial data to the data communication device (DCE) through this signal line ).
(8) the receiving signal (rxd), through which the data terminal device (DTE) receives serial data from the data communication device (DCE.
(9) ground line (SG and PG), indicating the signal line of the ground and protected area respectively.