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

19,410

Solution 1

You want to use the serial IOCTL TIOCSSERIAL which 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.

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.html The new one will have to do until I write another or find a better example.

Solution 2

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.

Share:
19,410
mathematician1975
Author by

mathematician1975

I am a former postdoctoral research mathematician, and now a C++ software developer on Linux and Windows.

Updated on July 20, 2022

Comments

  • mathematician1975
    mathematician1975 almost 2 years

    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().

    My question is this: Every time select returns 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.

    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?

  • mathematician1975
    mathematician1975 almost 12 years
    This is useful to me as I was unaware of TIOCSSERIAL but unfortunately the only item in the serial_struct that appears to be of use is the xmit_fifo_size integer which I have set to 1 but unfortunately still only alerts me after 8 bytes are received. Oh well....
  • mathematician1975
    mathematician1975 almost 12 years
    Apologies to you JimR - it DOES work provided that i disable the hardware control setting CRTSCTS. Linux dev is a new world to me and it is hard going!! Many thanks for your help.
  • ForceMagic
    ForceMagic over 11 years
    I don't see how this is related to the question that is being asked. I had a look to section 3.2 and it's not obvious how it could help him. Maybe you could explain more your thoughts. Also, it's never a bad idea to copy a part of the code you're talking about in case the link broke.
  • Exectron
    Exectron almost 11 years
    I'd never heard of TIOCSSERIAL before. Does it apply to Linux serial support in general, or just particular drivers? How widely implemented is it in various Linux serial drivers? Where is a good location for documentation of TIOCSSERIAL and other ioctls?
  • JimR
    JimR almost 11 years
    @CraigMcQueen: I've not used it in years since serial port connected modems (dialup ISPs, dialup credit card/check terminals etc) were going out of fashion, so my knowledge isn't up to the task. man ioctl and man ioctl_list plus digging through header files and asking questions is how I got things to work. There is a text based comm terminal called minicom that has source available. I learned a bit from that.
  • John U
    John U about 7 years
    I think the link has gone bad.
  • Ubdus Samad
    Ubdus Samad over 3 years
    See Section 9 on this page.