当前位置:网站首页>[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 .
边栏推荐
- Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
- Proe/Creo产品结构设计-钻研不断
- Taro--- day2--- compile and run
- Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
- AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
- What is digitalization? What is digital transformation? Why do enterprises choose digital transformation?
- Is it safe for Xiaobai in the stock market to open an account on the Internet?
- Want to open an account to buy stock, is it safe to open an account on the Internet?
- Adobe Premiere基础-常用的视频特效(边角定位,马赛克,模糊,锐化,手写工具,效果控件层级顺序)(十六)
- What is a through-hole conductive slip ring?
猜你喜欢

无人机专用滑环定制要求是什么

基于可学习尺寸自适应分子亚结构的药物相互作用预测

The practice of dual process guard and keeping alive in IM instant messaging development

Huawei partners and Developers Conference 2022 | Kirin software cooperates with Huawei to jointly build the computing industry and create a digital intelligence future

Overview of drug discovery-01 overview of drug discovery

Taro--- day2--- compile and run

Qu'est - ce que la numérisation? Qu'est - ce que la transformation numérique? Pourquoi les entreprises choisissent - elles la transformation numérique?

N methods of data De duplication using SQL

Flutter SliverAppBar全解析,你要的效果都在这了!

自监督学习与药物发现
随机推荐
Google Earth engine (GEE) -- an error caused by the imagecollection (error) traversing the image collection
[description] solution to JMeter garbled code
Import the data table in MySQL into Excel
[Niuke discussion area] Chapter 4: redis
Ai+ clinical trial patient recruitment | massive bio completed round a financing of $9million
手机股票开户安全吗,买股票在哪开户?
如何阅读一篇论文
lefse分析本地实现方法带全部安装文件和所有细节,保证成功。
Modular development
Electron window background transparent borderless (can be used to start the page)
药物发现综述-02-分子性质预测
Arrays.asList()坑
How to add live chat in your Shopify store?
模块化开发
MySQL - function
Form forms and form elements (input, select, textarea, etc.)
golang 猴子吃桃子,求第一天桃子的数量
Li Kou today's question -522 Longest special sequence
同花顺股票开户是会有什么风险吗?同花顺开户安全吗
PV操作原语