In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Linux serial port IO mode what are the experience, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
As we all know, in the Linux system, all devices exist in the form of files, and so is the serial port.
Generally speaking, there are blocking and non-blocking modes in Icano operation.
The concept of "timeout" is actually a means of dealing with blocking, which in essence belongs to the blocking Imap O mode.
The IO operation of serial port in Linux this paper divides it into three states:
Blocking state
Timeout state
Non-blocking state
There are several transition combinations of these three states:
Blocking-- > timeout
Blocking-- > non-blocking
Timeout-- > blocking
Timeout-- > non-blocking
Non-blocking-> blocking
Let's analyze one by one.
First, specify whether its mode is blocked or blocked when the descriptor of a serial port is turned on.
Fd = open ("/ dev/tttyS0", O_RDWR | O_NOCTTY); / / opening serial port in blocking mode fd = open ("/ dev/tttyS0", O_RDWR | O_NOCTTY | O_NDELAY); / / opening serial port in non-blocking mode / / O_NDELAY is equivalent to O_NOBLOCK
When a serial port is blocked, it can be set to timeout.
Using cc_t cc [NCCs] members of struct termios
Timeout of cc [VTIME] irregular mode read (in 100 milliseconds)
C _ cc [VMIN] minimum number of characters when reading in irregular mode
If you need to set a timeout, cc[ VMIN] must be equal to 0. This means that the minimum number of characters that can be read is 0, even if the read returns 0 when reading data with read timeout
There is one thing to pay attention to!
When cc[ VTIME] is set to 0 and cc[ VMIN] = = 0, it represents a timeout of 0 seconds (let's call it that!). At this time, reading data using read will return immediately (bytes are returned when data is read, 0 is returned when no data is like a general timeout), but although it looks the same as non-blocking mode at this time (read does not block), the return value is different.
Non-blocking mode: read returns immediately without reading the data-1
Timeout 0 seconds: read immediately returns 0 when no data is read (the blocking mode of timeout is set)
Ret = read (fd,recvbuf,BUF_SIZE); if (ret = =-1) / / "No data return" {/ / do something} ret = read (fd,recvbuf,BUF_SIZE) in non-blocking mode; if (ret = = 0) / / blocking mode sets timeout 0 seconds "timeout returns" {/ / do something}
Although the form is the same, but when programming, you must know which mode you are using and the current state of the serial port in order to better analyze and deal with the problem.
Let's talk about a problem I've encountered before:
I opened the serial port using blocking mode, but did not set the value of ccVMIN, and it is 0 after initialization, so I found that the serial port is not blocked. In fact, the reason is that the serial port mode or blocking mode is right, but it is 0 seconds timeout, so read returns when no data arrives.
Switching between blocking state and non-blocking state
Use when in a non-blocking state
Fcntl (fd,F_SETFL,0)
You can switch to a blocking state, and you can also set a timeout
When the timeout has been set in the non-blocking state, the timeout takes effect after the transition to the blocking state.
Use when blocking statu
Fcntl (fd,F_SETFL,FNDELAY); / / FNDELAY is equivalent to FNONBLOCK
It can be converted to a non-blocking state, and the timeout fails.
Here are some definitions of macros in fcntl.h.
/ * Define some more compatibility macros to be backward compatible with BSD systems which did not managed to hide these kernel macros. * / # ifdef _ USE_BSD # define FAPPEND O_APPEND # define FFSYNC O_FSYNC # define FASYNC O_ASYNC # define FNONBLOCK O_NONBLOCK # define FNDELAY O_NDELAY # endif / * Use BSD. , /
Is it clear now? O_NDELAY or O_NONBLOCK options for non-blocking mode when opening a serial port
Fcntl sets the third parameter of non-blocking mode, FNDELAY or FNONBLOCK, is actually O_NONBLOCK, mainly for compatibility.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.