In embedded system, many devices can be controlled by serial port, such as WiFi, Bluetooth, RFID, etc., so it is particularly important to use serial port in embedded devices.

First, a few concepts to clarify briefly.

RS232, RS485 and TTL refer to electrical level standards. In general, TTL uses 0V for low and +5V for high. RS232 uses negative logic level, that is, +3V ~ +15V indicates low level, and -3V ~ -15V indicates high level. RS485 and RS232 are based on the same serial port communication interface, data transceiver operation is consistent, RS485 uses differential signal negative logic, +2V ~ +6V indicates low level, -6V ~ -2V indicates high level.

The above three protocols (RS232, RS485, and TTL) are electrical characteristics and specify the physical layer interface requirements. The construction of the physical standard, like all kinds of highway construction, highway with we can run on a variety of cars, and make all kinds of automobile driving rules, this is the communication protocol, UART is one kind of communication protocol, full name is the general asynchronous transceiver, regulated in serial communication, the most common form of asynchronous transfer mode of communication, the following steps.

Commonly said serial port, COM port refers to the physical interface, they usually have 9 pin and 4 pin two styles of standard. The four pins of the 4-pin serial port standard are VCC, TX, RX, and GND. TX and RX are Transmit and Receive respectively. Generally speaking, when two serial ports send and receive information to each other, the two pins need to be interactively connected, that is, TX of port A is connected to RX of port B, and RX of port A is connected to TX of port B.

  

VCC, TX, RX and GND pins are generally used in the left side of the D 9-pin interface.

Let’s see how the serial port works.

Serial port usually refers to a serial interface, as opposed to a parallel interface. For example, if there is an 8-bit data sent from A to B, the difference between parallel and serial is shown below.

As you can see, serial transmission requires only one cable, while parallel transmission requires eight cables. Their respective characteristics are as follows:

  1. Serial transmission cable is less, so the signal attenuation is slow, electromagnetic compatibility is better, transmission distance is longer; There are many parallel transmission cables, so the signal attenuation is fast, electromagnetic compatibility is poor, and transmission distance is short.
  2. At first glance, the serial transmission speed appears to be 1/N of the parallel transmission, but in fact, due to the limitation of the physical characteristics of the electromagnetic interference of the parallel cable, the highest frequency of the parallel transmission (such as tens of MHz) is much lower than the highest frequency of the serial transmission (up to tens of GHz), so the speed of the serial transmission is not slower than the parallel transmission.
  3. The serial transmission hardware interface is simple, occupies little space, and the cost is low. The hardware interface of parallel transmission is complex, occupies large space, and costs a little higher.
  4. Serial transmission due to fewer cables, so the normal transmission of data needs various specific algorithms, complicated software design; Each data bit is independent in parallel transmission, and the algorithm and software design are relatively simple.

In simple terms, serial transmission has more advantages than parallel transmission. In fact, with the increasing frequency of various devices, serial communication is becoming more and more popular in the field of modern computers.

So how does the serial communication of this wire actually transmit data? There are two main methods.

1. Synchronous transmission

The so-called synchronous transmission, the most core place is to use a unified clock to control the sender and receiver, and then specify a synchronization start character, at the same time specify the transmission of each character contains bits (generally 5-8 bits) it is ok.

Because synchronous transmission must require the clock of both transceivers and transceivers to be consistent, and even require the use of a strict clock source, and this is impossible to meet the requirements in most cases, so although synchronous transmission is higher than asynchronous transmission speed, but more widely used or asynchronous transmission.

2. Asynchronous transmission

As the name implies, the nature of asynchronous transmission does not require synchronization between the clocks of the sender and the receiver, which means that they can have their own clocks (clocks do not need to be synchronized, but the frequency must be the same, otherwise the baud rate must be the same), which greatly reduces the complexity of engineering implementation. An important concern is therefore asynchronous transmission.

Since the transmission is asynchronous, meaning that the sender can send data at any time without any prior consultation with the receiver, it is natural for the receiver to have a question: when will your data come? The general approach is as follows: Provisions of a free state of potential (such as high level), and then prescribed a start bit (such as a low level of clock interval), and then followed by the normal data bits (of course, can also be specified length, such as eight bits), then it is optional parity bit (used to check whether the process of data transmission is affected by the electromagnetic environment in digital roll), Then there is the stop bit (such as a high level clock interval).

So with that said, let’s do a couple of visuals. Here are some pictures of Zhihu to illustrate with examples.

Original image website:www.zhihu.com/question/29…

A typical example of serial port transfer is the keyboard key. When we press ‘k’, we are actually transferring the ASCII value 01101011 of the character to the host.

But most of the time, the keyboard may not be generating any data at all, so we need to have a level that expresses the idle state, like this.

Keep at high level when no data is available. Well, when we press the letter ‘k’, we immediately send out a starting bit, followed by our data ‘k’, like this

The host accepts a number of bits (pre-arranged with the keyboard) and knows that the keyboard has sent a ‘k’. The optional check bit and stop bit are followed, and the cable level is restored to idle

In this way, the communication between devices is asynchronous, as long as the consistent baud rate is set, depending on the start bit, data bit, check bit, stop bit to coordinate.

The above asynchronous serial communication is called UART. Believe smart you all get to it ^_^