Linux 串口读取 - 我可以更改输入缓冲区的大小吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10815811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:34:37  来源:igfitidea点击:

Linux serial port reading - can I change size of input buffer?

c++linuxserial-port

提问by mathematician1975

I am writing an application on Ubuntu Linux in C++ to read data from a serial port. It is working successfully by my code calling select()and then ioctl(fd,FIONREAD,&bytes_avail)to find out how many bytes are available before finally obtaining the data using read().

我正在用 C++ 在 Ubuntu Linux 上编写一个应用程序来从串行端口读取数据。它通过我的代码调用成功运行select(),然后ioctl(fd,FIONREAD,&bytes_avail)在最终使用read().

My question is this: Every time selectreturns with data, the number of bytes available is reported as 8. I am guessing that this is a buffer size set somewhere and that select returns notification to the user when this buffer is full.

我的问题是:每次select返回数据时,可用字节数报告为 8。我猜这是在某处设置的缓冲区大小,当该缓冲区已满时,select 会向用户返回通知。

I am new to Linux as a developer (but not new to C++) and I have tried to research (without success) if it is possible to change the size of this buffer, or indeed if my assumptions are even true. In my application timing is critical and I need to be alerted whenever there is a new byte on the read buffer. Is this possible, without delving into kernel code?

作为一名开发人员,我是 Linux 的新手(但对 C++ 并不陌生),我曾尝试研究(但没有成功)是否可以更改此缓冲区的大小,或者我的假设是否确实如此。在我的应用程序中,时序至关重要,每当读取缓冲区中有新字节时,我都需要收到警报。如果不深入研究内核代码,这可能吗?

采纳答案by JimR

You want to use the serial IOCTL TIOCSSERIALwhich allows changing both receive buffer depth and send buffer depth (among other things). The maximums depend on your hardware, but if a 16550A is in play, the max buffer depth is 14.

您想使用TIOCSSERIAL允许更改接收缓冲区深度和发送缓冲区深度(除其他外)的串行 IOCTL 。最大值取决于您的硬件,但如果使用 16550A,则最大缓冲区深度为 14。

You can find code that does something similar to what you want to do here

您可以在此处找到与您想要执行的操作类似的代码

The original link went bad: http://www.groupsrv.com/linux/about57282.htmlThe new one will have to do until I write another or find a better example.

原来的链接坏了:http: //www.groupsrv.com/linux/about57282.html新的将不得不做,直到我写另一个或找到更好的例子。

回答by Sbirro

You can try to play with the VMIN and VTIME values of the c_cc member of the termios struct. Some info here, especially in the section 3.2.

您可以尝试使用 termios 结构的 c_cc 成员的 VMIN 和 VTIME 值。这里有一些信息,特别是在第 3.2 节中。