Analysis of Serial Communication Technology

1 Introduction

The most familiar stranger! Using this sentence to describe the "serial port" is the most appropriate.

For embedded engineers, "serial port" is a very familiar module, familiar enough to be as natural as drinking water. At the same time, many details about the serial port are gradually blurred and ignored, such as:

What are the concepts such as serial, UART, and RS232 that we often talk about? What is the connection between them? What's the difference?

How is the baud rate of a serial port defined and calculated? What about bit rate? What is the relationship between the two?

Why is there an error in the baud rate? What is the allowable error range during communication?

This article will briefly summarize and summarize the above problems in order to guide the related development work.

2. Concept Explanation

2.1 Serial communication

In communication and computer science, Serial communication is a general concept that refers to all serial communication protocols, such as RS232, USB, I2C, SPI, 1-Wire, Ethernet, etc. Serial (serial) here is relative to parallel communication (parallel communication), as shown below:

Parallel_and_Serial_Transmission

Figure 1 Parallel communication and serial communication

The above picture 1 is very easy to understand. I won't explain it too much here. For the advantages and disadvantages of serial communication, please refer to the explanation in "Serial communication [1] ".

2.2 Synchronous serial communication and Asynchronous serial communication

After understanding the concept of serial communication, you may have questions: after the receiver receives a long series of signals representing 0/1 level transitions, how to restore valid information? There are two ways:

1) The sender provides a clock signal while sending serial data, and sends the data according to certain conventions (for example, when the clock signal is on the rising edge, the data is sent out), and the receiver sends the data according to the clock signal provided by the sender. , and everyone's agreement to receive data. This is often referred to as synchronous serial communication. Protocols with clock signals such as I2C and SPI belong to this communication method.

2) Before and after the data is sent, the sender tells the receiver that it can start (or stop) receiving data through a specific form of signal (such as a START signal and a STOP signal). At the same time, the sending and receiving parties will agree on a data sending speed (that is, the famous baud rate). After the sending end sends the START signal, it will send serial data according to a fixed rhythm. At the same time, the receiving end is receiving After the START signal, serial data is also received at a fixed rhythm. This is often referred to as Asynchronous serial communication. The main character of this section, serial communication, is this communication method.

Note 1: Strictly speaking, the asynchronous serial communication in the form of START/STOP is called "asynchronous start-stop communication", but because this form is too widely used in asynchronous serial communication, the two are also confused and not the same. Added distinction.

Note 2: According to the different synchronization methods, serial ports also include asynchronous serial ports and synchronous serial ports. The serial ports referred to in this article are all asynchronous serial ports.

2.3 Serial port and RS-232

In the computer world, "serial port" is an interface for data transmission using serial, which is relative to the parallel interface of the computer. Although serial communication can be realized through various technologies (such as USB, Ethernet, etc.), due to historical reasons, the serial port we call refers to the interface that conforms to the RS-232 specification. The following figure is one of our more common ones:

Serial_port

Picture 2 male DE-9 connector

Serial port is a general term for the physical interface used to realize serial communication, but only the name is not enough. Both parties of the communication need some conventions and specifications to correctly transmit data, such as which signal lines and signal levels are required. How, what is the shape of the interface, the encoding format of the data, the rate at which the data is transferred, etc.

In serial communication, these contents are mainly defined by two parts:

1) The RS-232 (sometimes called RS-232-C, C is the version) specification, which defines "hardware-related" features, including:

Electrical signal characteristics, such as signal level (-3V~-15V means logic 1, +3~+15V means logic 0), etc.;

Interface characteristics, such as the definition of connectors (9-pin DE-9 Male/Female, 25-pin DB-25 Male/Female, etc.), definition of pin signals, etc.;

The characteristics of the cable, such as the length of the cable (RS-232 does not explicitly limit the length of the cable, but limits the capacitance, the effect is the same), etc.;

etc.

2) Serial port hardware, which defines "software-related" features, including:

The data encoding format (character encoding) is the "asynchronous start-stop" format mentioned above, which is also known as "start bit, data bit, check bit, stop bit";

The bit rate of data transmission, that is, the baud rate we are familiar with.

Note 3: Strange to say, the RS-232 specification specifies all aspects of serial communication, but does not specify the encoding format and bit rates related to data transmission. It may be that the differences in hardware are too great for this specification to fully cover.

Note 4: Although the RS-232 specification does not specify bit rates, it does make a requirement that it cannot exceed 20Kbps, although from the current point of view, this is nonsense, hehe.

Note 5: RS-232 defines various forms of serial ports, such as DE-9, DB-25, etc., but most of these are related to communication-related scenarios in ancient times. With the popularization of digital communication, and protocols such as USB The cannibalization of these behemoths has become less and less common. On the contrary, in embedded scenarios, some simplified forms, such as 4-wire (VCC/RX/TX/GND) serial ports, are used more.

2.4 UART(Universal Asynchronous Receiver/Transmitter)

As we mentioned in the previous section (2.3), RS-232 does not specify the encoding format and bit rates of serial communication due to the large differences in hardware. Therefore, the realization of this piece is entirely by the specific hardware (or the corresponding software is responsible).

In the early products, most of them use software to generate a bit stream that conforms to the encoding format at a certain rate (bit rates). But later in order to improve performance, many products (such as IBM's PC) use special hardware modules to complete this thing, which is the UART we often talk about.

So far, the terms related to serial communication have all appeared. Let’s briefly summarize:

Serial communication generally refers to the method of data communication in the form of bit stream.

Synchronous serial communication (Synchronous serial communication) and asynchronous serial communication (Asynchronous serial communication), different implementation methods of serial communication, the main difference lies in the way of synchronization.

Serial port, specifically refers to the interface for data communication using the method of "asynchronous serial communication", mainly described from the perspective of hardware.

RS-232, a specification that defines the communication behavior between two serial ports, is biased towards hardware characteristics such as levels, connectors, and cables.

UART (Universal Asynchronous Receiver/Transmitter), a hardware module, generates the bit stream required for communication according to the encoding format and bit rate specified by the serial port hardware.

2.5 TTL level conversion and USB to serial port

Finally, let's talk about the concept of TTL level conversion. At present, the most widely used scenario of serial port is some data interaction between embedded products and PC, such as debug output, firmware update, etc. But here's a bit of a nuisance:

The PC serial port conforms to the RS-232 specification, and its level is "-3V~-15V/+3~+15V". And most embedded products use TTL level (such as 0v/3.3v). Therefore, these two levels cannot be directly connected. How to do it? Just add a level conversion circuit, so the connection between the embedded product and the PC becomes the following form:
       PC (serial port)<---------->Level conversion circuit<-- -------->Embedded products (serial port)

There are more troublesome things. With the advancement of technology, the big killer such as the serial port is gradually abandoned by the PC. What should I do? The USB to serial port came into being, and the connection method became:

PC(USB interface)<---------->USB to serial port<---------->Embedded product(serial port)

3. Baud rate and bit rate

After understanding these familiar and unfamiliar terms in the serial port, let's look at the baud rate.

To be honest, in data communication, the bit rate is easier to understand, that is, how many bits can be transmitted within a certain period of time. For example, bps is an abbreviation for bit per second. What about the baud rate?

In communications, the baud rate, also called the symbol rate, refers to the rate at which "data changes". It's hard to say, let's give an example:

In the Department of Computer Science, Xiao Yang and Xiao Li are good friends, but Xiao Yang is a scholar and Xiao Li is a scumbag. So, when the final exam came, Xiao Yang decided to "help". How to do it?

The two agreed that during the exam, Xiao Yang would carry two pens, black and white. According to the appearance of the two pens, it would indicate the four answers of A, B, C, and D, namely:
    the white pen did not appear, the black pen did not appear A
    White pens do not appear Black pens appear B
    White pens appear Black pens do not appear C
    White pens appear Black pens appear D

At the same time, it was agreed that 1 hour after the start of the test, Xiao Yang would start from the first multiple-choice question and change the answer at a rate of one change per minute. According to this speed and everyone's agreement, Xiao Li obtained the answer by observing the appearance of the two pens.

It's a good idea, but if you think about it, this is actually a typical asynchronous communication process. In the process of communication, the speed of answer update (1 time per minute) is what we call baud rate (or symbol rate), which is 1 bd per minute (bd can be viewed as the unit of baud).

At the same time, with each update, how much information is passed along? On the surface, they are A, B, C, and D, but they are essentially two bits represented by white and black, 00, 01, 10 or 11. Therefore, each update transmits 2 bits of information, so the bit rate is 2 bits per minute.

In the above example, the baud rate and bit rate of the communication are different, 1 and 2 (per minute) respectively, but some communication systems, such as the well-known serial communication, they are the same, for example, we say 115200 The baud rate, the actual bit rate is also 115200. Because only 1 bit (0 or 1) is transmitted at a time.

4. Frame format and baud rate error

We know that the data format of serial communication includes start bit, data bits, parity bit and stop bits, among which:

The start bit is fixed to 1bit;

The data bits can be 5, 6, 7, 8 or 9 bits, but 8 bits are commonly used;

The parity bit is optional, generally 0 bit;

The stop bit can be either 1bit or 2bits, generally 1bit.

The diagram is as follows:

Rs232_serial_port

Figure 2 Serial frame format

So, in essence, the baud rate defines the duration of each bit of the signal in the frame format, which is 1/bard rate. And a frame of data transmission is completed, the time required is (1 / bard rate) * 10 (take the 8N1 frame format as an example).

This method of asynchronous communication has bad news and good news:

The bad news is that the baud rate may be inaccurate and have errors due to the system clock accuracy, frequency division factor, etc. During the transmission of a frame of data, errors will accumulate.

The good news is that if the error does not accumulate much, the accumulated error can be cleared to zero in the next frame of data due to the correction of the start/stop signal.

Regarding the error, let's analyze it in a little more detail.

According to our common sense, the signal is sampled at intermediate points, so in a frame of data, if the error accumulates for more than (1 / bard rate / 2) time, the data will be sampled wrongly. Assuming that the ideal baud rate is BDi and the actual baud rate is BDr, then:

The error time of each bit is: 1 / BDi - 1 / BDr.

The error time of 10 bits is: 10 * (1 / BDi – 1 / BDr).

Therefore, the safe margin of error is: abs(10 * (1 / BDi – 1 / BDr)) < 1 / BDi / 2.

Taking the baud rate of 115200 as an example, bring in the above formula:

abs(10 * (1 / 115200– 1 / BDr)) < 1 / 115200 / 2

-1 / 115200 / 2 < 10 * (1 / 115200– 1 / BDr) < 1 / 115200 / 2

(20 / 21) * 115200 < BDr < (20 / 19) * 115200

Therefore, the allowable error range for 115200 baud rate is 20 / 21 ~ 20 / 19, or +/- 5%. Other baud rates can also be brought into the formula to calculate. Also, what if there are errors on both sides? Hey, you know, except 2.

5. Reference Documentation

[1] Serial communication,https://en.wikipedia.org/wiki/Serial_communication

[2] Serial port,https://en.wikipedia.org/wiki/Serial_port

[3] RS-232,https://en.wikipedia.org/wiki/RS-232

[4] Asynchronous serial communication,https://en.wikipedia.org/wiki/Asynchronous_serial_communication

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765981&siteId=291194637