Serial port is the abbreviation of serial interface (serial port), also known as serial communication interface or COM interface. Serial communication refers to a communication mode in which data is transmitted bit by bit on a signal line using a serial communication protocol (serial communication). Serial ports are divided according to electrical standards and protocols, including RS-232-C, RS-422, RS485, etc.

In serial communication, data is transmitted on a single line with a width of 1 bit, and the data of a byte is divided into 8 times, and transmitted bit by bit in order from low bit to high bit. The data of serial communication is transmitted bit by bit, and each bit sent by the sender has a fixed time interval, which requires the receiver to receive each bit according to the same time interval as the sender. Not only that, but the receiver must be able to determine the beginning and end of a message group.

The two basic serial communication methods commonly used include synchronous communication and asynchronous communication.

serial synchronous communication

Synchronous communication (SYNC: synchronous data communication) means that at the agreed communication rate, the frequency and phase of the clock signal at the sending end and the receiving end are always consistent (synchronous), thus ensuring that the two parties in communication are completely consistent when sending and receiving data timing relationship.

Synchronous communication forms many characters into an information group (information frame), and the beginning of each frame is indicated by a synchronous character, and only one frame of information is transmitted in one communication. A clock signal needs to be transmitted along with the data so that the receiver can use the clock signal to determine each bit of information.

The advantage of synchronous communication is that the number of bits of information transmitted is almost unlimited, and the data transmitted in one communication has tens to thousands of bytes, and the communication efficiency is high. The disadvantage of synchronous communication is that it is required to keep an accurate synchronous clock in the communication, that is, the sending clock and the receiving clock must be strictly synchronized. A common practice is to use the same clock source for both devices.

Asynchronous communication is the most widely used in serial communication and programming. Serial port in STM32Example of communication:Learning STM32 single-chip microcomputer, the serial port that cannot be bypassed

Serial asynchronous communication process

Asynchronous communication (ASYNC: asynchronous data communication), also known as start-stop asynchronous communication, is transmitted in units of characters, there is no fixed time interval requirement between characters, and each character in each character is transmitted at a fixed time .

In asynchronous communication, the synchronization between the sender and receiver is achieved by setting the start bit and stop bit in the character format. Specifically, before a valid character is officially sent, the transmitter first sends a start bit, then sends a valid character bit, and then sends a stop bit at the end of the character, and the start bit to stop bit constitutes a frame. Between the stop bit and the next start bit is an idle bit of variable length, and the start bit is specified as low level (logic value 0), the stop bit and idle bit are both high level (logic value 1), This ensures that there must be a lower jump edge at the beginning of the start bit, thus marking the start of a character transmission. According to the start bit and stop bit, the definition and synchronization of characters can be easily realized.

Obviously, when using asynchronous communication, the sending end and the receiving end can control the sending and receiving of data by their respective clocks, and the two clock sources are independent of each other and can be asynchronous with each other.

Let’s briefly talk about the data sending and receiving process of asynchronous communication.

  • Data Format

Before introducing the data sending and receiving process of asynchronous communication, it is necessary to clarify the data format of asynchronous communication.

Asynchronous communication stipulates that the data format transmitted is composed of start bit, data bit, parity bit and stop bit. The asynchronous communication data format is shown in the figure below (the The parity bit is not shown in the figure, because the parity bit is not necessary. If there is a parity bit, the parity bit should be after the data bit and before the stop bit).

(1) Start bit: The start bit must be a logic 0 level that lasts for one bit time, marking the beginning 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 immediately follows the start bit and is the real effective information in communication. The number of data bits can be agreed upon by the communication parties, generally it can be 5, 7 or 8 bits, the standard ASCII code is 0~127 (7 bits), and the extended ASCII code is 0~255 (8 bits). When transmitting data, the low bit of the character is transmitted first, and then the high bit of the character is transmitted.

(3) Parity check bit: The parity check bit occupies only one bit, which is used for odd check or even check, and the parity check bit is not necessary. If it is odd parity, it needs to ensure that the transmitted data has an odd number of logic high bits in total; if it is even parity, it needs to ensure that the transmitted data has a total of even number of logic high bits.

For example, suppose the transmitted data bit is 01001100, if it is odd parity, the odd parity bit is 0 (make sure there are an odd number of 1s in total), and if it is even parity, the even parity bit is 1 (you must Make sure there is an even number of 1’s in total).

It can be seen that the parity bit simply sets the logic high or logic low for the data, and does not make a substantial judgment on the data. The advantage of this is that the receiving device can know the state of a bit, and it is possible to judge whether there is Noise interferes with communication and the synchronization of transmitted data.

(4) Stop bit: The stop bit can be 1, 1.5 or 2 bits, which can be set by software. It must be a logic 1 level, marking the end of the transmission of a character.

(5) Idle bit: The idle bit refers to the start bit from the stop bit of a character to the start bit of the next character, indicating that the line is in an idle state and must be filled with a high level.

  • data sending process

After knowing 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 there is no data to be sent, the sender outputs logic 1, and there can be any number of free bits.

(2) When data needs to be sent, the sender first outputs logic 0 as the start bit.

(3) Then you can start to output the data bits. The sending end first outputs the lowest bit D0 of the data, then D1, and finally the highest bit of the data.

(4) If the parity check bit is set, the sender outputs the check bit.

(5) Finally, the sender outputs a stop bit (logic 1).

(6) If there is no information to be sent, the sender outputs a logic 1 (idle bit), and if there is information to be sent, then go to step (2).

  • Data receiving process

In asynchronous communication, the receiving end determines the time length of each bit by the receiving clock and the baud rate factor. The following takes the baud rate factor equal to 16 (the receiving clock shifts the receiving shift register once every 16 clock cycles) as an example to illustrate.

(1) Start communication, the signal line is idle (logic 1), when a transition from 1 to 0 is detected, start counting the receive clock.

(2) When 8 clocks are counted, detect the input signal, if it is still low, then confirm that this is the start bit, not an interference signal.

(3) After the receiving end detects the start bit, it detects the input signal every 16 receiving clocks, and takes the corresponding value as D0 bit data.

(4) Detect the input signal once every 16 receiving clocks, and use the corresponding value as D1-bit data until all data bits are input.

(5) Check the parity bit.

(6) After receiving the specified number of data bits and parity bits, the communication interface circuit expects to receive a stop bit (logic 1). If logic 1 is not received at this time, it means that an error has occurred, and the status register is set to ” If there is no error, perform a parity check on all data bits. When there is no check error, take the data bit out of the shift register and send it to the data input register. If the check is wrong, set “parity” in the status register. wrong” sign.

(7) After receiving all the information of this frame, the high level appearing on the line is regarded as an idle bit.

(8) When the signal becomes low again, start to enter the detection of the next frame.

The above is the whole process of data sending and receiving in asynchronous communication.

Several concepts of serial communication

In order to better understand serial communication, we also need to understand several basic concepts in serial communication.

(1) Sending clock: When sending data, the data to be sent is first sent to the shift register, and then under the control of the sending clock, the parallel data is shifted bit by bit and output.

(2) Receive clock: When receiving serial data, the rising edge of the receive clock samples the received data, performs data bit detection, and shifts it into the shift register of the receiver, and finally forms a parallel data output.

(3) Baud rate factor: The baud rate factor refers to the number of clock pulses required to send or receive 1 data bit.

serial connector

There are two commonly used serial port connectors, one is 9-pin serial port (DB-9 for short), and the other is 25-pin serial port (DB-25 for short). Each type of connector has a male head and a female head. The connector with a needle is a male head, and the connector with a hole is a female head. The picture below is the appearance of DB-9

As can be seen from the above figure, in the 9-pin serial port connector, the definition order of the pins of the male and female connectors is different, which requires special attention. So what does this pin do? The function description of common pins of 9-pin serial port and 25-pin serial port is shown in the figure below.

RS-232C standard

Commonly used serial communication interface standards are RS-232C, RS-422, RS-423 and RS-485. Among them, RS-232C, as the electrical standard of the serial communication interface, defines the interface information for bit-serial transmission between data terminal equipment (DTE: data terminal equipment) and data communication equipment (DCE: data communication equipment), and reasonably arranges the interface The electrical signal and mechanical requirements have been widely used in the world.

  • electrical characteristics

RS-232C stipulates electrical characteristics, logic levels and various signal functions.

On the TXD and RXD data lines:

  • Logic 1 is a voltage of -3~-15V.

  • Logic 0 is a voltage of 3~15V.

On control lines such as RTS, CTS, DSR, DTR and DCD:

  • The effective signal (ON state) is a voltage of 3~15V.

  • The invalid signal (OFF state) is a voltage of -3~-15V.

It can be seen that RS-232C uses positive and negative voltages to indicate the logic state, which is exactly the opposite of the transistor-transistor logic integrated circuit (TTL) which uses high and low levels to indicate the logic state.

  • Signal Line Assignment

The RS-232C standard interface has 25 lines, including 4 data lines, 11 control lines, 3 timing lines, and 7 spare and undefined lines.

So, how are these signal lines distributed on the pins of the 9-pin serial port and the 25-pin serial port? The distribution of 9-pin serial port and 25-pin serial port signal lines is shown in the figure below.

The following is a brief introduction to these signal lines.

(1) The data device is ready (DSR), and the valid state (ON) indicates that the data communication equipment is in a usable state.

(2) The data terminal is ready (DTR), and the valid state (ON) indicates that the data terminal equipment is in a usable state.

The validity of these two device status signals only means that the device itself is available, and does not mean that the communication link can start to communicate. Whether the communication can start is determined by the following control signals.

(3) Request to send (RTS), used to indicate that the data terminal equipment (DTE) requests the data communication equipment (DCE) to send data.

(4) Allow to send (CTS), which is used to indicate that the data communication equipment (DCE) has prepared data and can send data to the data terminal equipment (DTE), which is a response to the request to send the signal RTS. Request to send (RTS) and clear to send (CTS) are used in half-duplex communication systems. In full-duplex systems, there is no need to use request to send (RTS) and clear to send (CTS) signals, directly set them to ON can be.

(5) Data carrier detection (DCD), which is used to indicate that the data communication equipment (DCE) has connected to the communication link, and informs the data terminal equipment (DTE) that it is ready to receive data.

(6) Ringing indication (RI), when the data communication equipment receives the ringing call signal sent by the switchboard, the signal is enabled (ON), and the terminal is notified that it has been called.

(7) Transmit data (TXD), the data terminal equipment (DTE) sends serial data to the data communication equipment (DCE) through this signal line.

(8) Receive signal (RXD), the data terminal equipment (DTE) receives the serial data sent from the data communication equipment (DCE) through the signal line.

(9) Ground wires (SG, PG), which represent signal ground and protection ground signal lines respectively.

Leave a Reply

Your email address will not be published.