How can I determine the amount of write/output buffer space left on a linux serial port?

我是研究僧i 提交于 2019-12-10 17:15:40

问题


You can determine how much data is available to read from a serial port under linux using an ioctl. Is it possible to determine how much buffer space remains for the serial port when writing to it? Effectively I want to write a block of data to a serial port, succeeding only if it can all be offloaded in one go, or failing if it would have to be chunked. The writes and reads to the ports are non-blocking. I'm not expecting this to be the UARTs buffer, but the kernels memory buffer ahead of the UARTs buffer (I guess).


回答1:


You can determine the amount of write/output.

For read:

ioctl(device_handler, TIOCINQ, &bytes);

For write:

ioctl(device_handler, TIOCOUTQ, &bytes);

Size of FIFO buffer:

serial_struct serinfo;
memset(&serinfo, 0, sizeof(serinfo));
ioctl(device_handler, TIOCGSERIAL, &serinfo);
serinfo.xmit_fifo_size;

Regards, VA.




回答2:


The serial port is a character device not a block device. It has no buffer. Character devices ( such as Serial port, keyboard, mouse) only writes and reads a character not a word. For exame if you listen to a serial someone writes " have a nice day ", if you do not listen from the time he starts typing, you would not see the entire phrase. You would see only the characters typed when you listen




回答3:


If you access serial port using file descriptor, you can use select for checking whenever the descriptor is ready for non-blocking write. I don't know whenever this works for serial ports. I used this for TCP sockets and it worked.



来源:https://stackoverflow.com/questions/13181040/how-can-i-determine-the-amount-of-write-output-buffer-space-left-on-a-linux-seri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!