当前位置:网站首页>[embedded foundation] serial port communication
[embedded foundation] serial port communication
2022-06-28 01:39:00 【White embedded】
Catalog :
1. Preface
Serial communication is very commonly used in actual embedded development , It is usually used for data interaction with sensors .
2. Basic concepts
The concept of serial communication is very simple , Serial port by bit (bit) Send and receive bytes . Although by byte (byte) Parallel communication is slow , But the serial port can use one wire to send data and another wire to receive data at the same time .
Understand the principle of serial communication , You need to understand the basic concepts first : Baud rate 、 Start bit 、 Data bits 、 Check bit 、 Stop bit 、 Free bit .

2.1 Baud rate
This is a parameter to measure the communication speed , Indicates the number of... Transmitted per second bit Number . such as 100 Indicates that the message is sent every second 100bit.
2.2 Start bit
The start bit is a logic that lasts one bit 0 level , Indicates the beginning of transmission of a character . The receiver can use the start bit to synchronize its receiving clock with the sender's data .
2.3 Data bits
The data bit follows the start bit , Is the really valid information in communication .
The number of data bits can be mutually agreed by both communication parties , It can generally be 5 position 、7 Bit or 8 position .
When transferring characters , Pass low first , Then pass to the high position . The standard ASCII Code is 0~127; Extended ASCII Code is 0 ~ 255(8 position ).
2.4 Check bit
The parity bit occupies only one bit , Used for odd check or even check , Parity bits are not required .
If it's odd , It is necessary to ensure that the transmitted data has an odd number of high levels in total ; If it's even parity , It is necessary to ensure that the transmitted data has an even number of high levels in total .
Suppose the transmitted data bits are 01010101, If it's odd , Then the odd check bit is 1( Make sure there are an odd number 1). If it's even parity , Then the parity bit is 0( Make sure there are even numbers 1).
2.5 Stop bit
The stop bit must be logical 1 level , Indicates the end of a character . The stop bit can be 1 position 、1.5 Bit or 2 position , It can be set by software .
2.6 Free bit
The free bit refers to the end of the stop bit of one character and the beginning of the start bit of the next character , Indicates that the line is idle , Must be filled by high level .
3. Working mode
Serial port communication is divided into... According to the data transmission direction 3 Working mode : Simplex mode 、 Half duplex mode and full duplex mode .
3.1 Simplex mode
Simplex mode data transmission is unidirectional . Between the two sides of the communication , One party is fixed as the sender , One side is fixed as the receiver . Data can only be transmitted in one direction using one line .
3.2 Half duplex mode
Half duplex mode can send and receive data , But you cannot send and receive at the same time . Data transmission allows data to be transmitted in both directions , But only one of them can send data at any time , The other party accepts the data .
Half duplex mode can use one data line , You can also use two data cables .
3.3 Full duplex mode
Full duplex mode communication allows data to be transmitted in both directions at the same time , It is the combination of two simplex communication modes , It is required that both the sending equipment and the receiving equipment have independent receiving and sending capabilities .
In full duplex mode , Transmitter and receiver at each end , There are two transmission lines , Information transmission is higher .
4. Synchronous communication and asynchronous communication
The most obvious difference between synchronous communication and asynchronous communication is whether there is a clock signal .
4.1 Synchronous communication
Synchronous communication (SYNC:synchronous data communication) It means that at the agreed communication rate , The frequency and phase of the clock signal at the transmitter and receiver are always consistent ( Sync ), This ensures that both sides of the communication have a completely consistent timing relationship when sending and receiving data .
Synchronous communication combines many characters into an information group ( Information frame ), The beginning of each frame is indicated by synchronization characters , Only one frame of information is transmitted at a time . While transmitting data, it also needs to transmit clock signal , So that the receiver can use the clock signal to determine each information bit .
The advantage of synchronous communication is that the number of bits of information transmitted is almost unlimited , There are tens to thousands of bytes of data transmitted in a communication , High communication efficiency . The disadvantage of synchronous communication is that it is required to always maintain an accurate synchronous clock in communication , That is, the sending clock and receiving clock should be strictly synchronized ( The common practice is that two devices use the same clock source ).
4.2 asynchronous communication
asynchronous communication (ASYNC:asynchronous data communication), Also known as start stop asynchronous communication , Is transmitted in character units , There is no fixed time interval between characters , The bits in each character are transmitted at a fixed time .
In asynchronous communication , Synchronization is achieved by setting the start bit and stop bit in the character format . To be specific , Before a valid character is officially sent , The transmitter sends a start bit first , Then send valid character bits , Send another stop bit at the end of the character , The start bit to the stop bit form a frame . Between the stop bit and the next start bit is a free bit of variable length , And the start bit is specified as low level ( The logical value is 0), The stop bit and the free bit are high ( The logical value is 1), This ensures that there must be a jump edge at the beginning of the start bit , Thus, the beginning of a character transmission can be marked . According to the start bit and stop bit, it is easy to define and synchronize characters .
obviously , When asynchronous communication is adopted , The sender and receiver can control the sending and receiving of data by their own clocks , The two clock sources are independent of each other , Can be out of sync with each other .
5. Flow control
When the two devices communicate normally , Due to different processing speeds , There is such a problem , Some are fast , Some are slow , In some cases , It may lead to the loss of data .
Such as the communication between desktop computer and MCU , The receiving data buffer is full , At this time, the data that continues to be sent will be lost .
Flow control can solve this problem , When the receiving end can't process the data , Just send out “ No longer receive ” The signal of , The sender stops sending , Until receipt “ You can continue sending ” And then send the data . So flow control can control the process of data transmission , Realize the speed matching between the sender and the receiver , Prevent data loss .
PC Two kinds of flow control commonly used in computer are hardware flow control ( Include RTS/CTS、DTR/CTS etc. ) And software flow control XON/XOFF( continue / stop it ).
5.1 RTS
Require ToSend, Send a request , For the output signal , Used to indicate that the device is ready to receive data , Low level active .
Low level indicates that the device can receive data .
5.2 CTS
Clear ToSend, Send permission , Is the input signal , Used to determine whether data can be sent to the other party , Low level active .
Low level indicates that the device can send data to the other party .
5.3 Example of hardware flow control
RTS (Require ToSend, Send a request ) For the output signal , Used to indicate that the device is ready to receive ;CTS (Clear ToSend, Send clear ) Is the input signal , Used to indicate whether the peer device is ready to receive data .
such as A、B Two devices communicate , Two devices RTS、CTS Cross connect . Yes B Device send (A Equipment reception ) Come on , If A When the receiving buffer of the device is almost full , Put your own RTS pull up ( notice B The device stops sending ), After a while A The device receive buffer has space , Put your own RTS Pull it down ( notice B The device can start sending ).

5.4 Software flow control
Software flow control is the same as hardware flow control , It's just that there's a different way of doing it . In the process of communication , Software flow control by inserting Xoff( Special characters ) and Xon( Another special character ) Signal .A Once the device receives B From the device Xoff, Stop sending now ; conversely , If received B From the device Xon, Then resume sending data to B equipment . Empathy ,B The device is similar , So as to realize the speed matching between the sender and the receiver .
6. At the end
I have worked for many years , I don't think the serial communication has been thoroughly understood , be ashamed .
Even the simplest things , If there is no personal practice or no actual use of , Maybe I can't say I understand .
In practice , Debugging serial communication is generally to call the encapsulated interface of the platform , even so , Be familiar with the basic principles , When encountering an exception, you can check the cause more quickly .
边栏推荐
- DeepMind | 通过去噪来进行分子性质预测的预训练
- Set集合用法
- Deepmind | pre training of molecular property prediction through noise removal
- Li Kou today's question -522 Longest special sequence
- Chapitre 4: redis
- Is it safe to open an account online now? Novice is just on the road, ask for the answer
- Deploy a mongodb single node server locally, enable auth authentication and enable oplog
- 【嵌入式基础】内存(Cache,RAM,ROM,Flash)
- Implementation of timed tasks in laravel framework
- Overview and construction of redis master-slave replication, sentinel mode and cluster
猜你喜欢

MapReduce elementary programming practice
![[Niuke discussion area] Chapter 4: redis](/img/53/f8628c65890f1c68cedab9008c1b84.png)
[Niuke discussion area] Chapter 4: redis

电商转化率这么抽象,到底是个啥?
![The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]](/img/56/768f8be9f70bf751f176e40cbb1df2.png)
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]

Meituan dynamic thread pool practice idea has been open source

MySQL十种锁,一篇文章带你全解析

Solve storage problems? WMS warehouse management system solution

Taro--- day2--- compile and run

Form forms and form elements (input, select, textarea, etc.)

How to build dual channel memory for Lenovo Savior r720
随机推荐
Neural network of zero basis multi map detailed map
Some problems in awk
Deepmind | pre training of molecular property prediction through noise removal
攻击队攻击方式复盘总结
Deploy a mongodb single node server locally, enable auth authentication and enable oplog
PV操作原语
AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
Implementation of timed tasks in laravel framework
去哪儿网(Qunar) DevOps 实践分享
Solon 1.8.3 发布,云原生微服务开发框架
. Mp4 video test address
什么是数字化?什么是数字化转型?为什么企业选择数字化转型?
[untitled]
The practice of dual process guard and keeping alive in IM instant messaging development
Interviewer asked: Inheritance of JS
药物发现综述-03-分子设计与优化
Set collection usage
Is it safe for Xiaobai in the stock market to open an account on the Internet?
Proe/creo product structure design - continuous research
Electron window background transparent borderless (can be used to start the page)