zoalsay
Posts: 3
Joined: Thu Sep 06, 2018 12:38 pm

How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 1:01 pm

Hi All,
I am attaching a sensor( recognized as: /dev/ttyUSB0) that continuously send data to raspberry pi 3 B.
I want to increase the UART (serial) USB receive input buffer to be able to store data more than the current (4095); is there anyway to do this in C under Linux?
I used to do it in C under windows using:
Status = SetupComm(hComm, 51200000,4096);
Is there any equivalent to this command in Linux?
Best Regards,

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 1:25 pm

Do you know what the size defaults to on Linux? I always assumed it was a 16k bytes buffer but that may just be a faulty memory.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20494
Joined: Sat Jul 30, 2011 7:41 pm

Re: How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 1:29 pm

Not found one. Which is odd! Seems to, on the whole, default to PAGE_SIZE which is 4096.

A better way of dealing with this is to have your own buffer, and transfer data from the serial buffer to that using another thread. That way you can have whatever buffer size you need and can leave the serial driver to do it's job.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

zoalsay
Posts: 3
Joined: Thu Sep 06, 2018 12:38 pm

Re: How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 1:40 pm

joan wrote:
Thu Sep 06, 2018 1:25 pm
Do you know what the size defaults to on Linux? I always assumed it was a 16k bytes buffer but that may just be a faulty memory.
I am checking the available bytes in buffer using:
ioctl(hCom, FIONREAD, &avaialble_bytes);
It never exceeds 4095, I understood it is 4095 bytes.

zoalsay
Posts: 3
Joined: Thu Sep 06, 2018 12:38 pm

Re: How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 1:43 pm

jamesh wrote:
Thu Sep 06, 2018 1:29 pm
Not found one. Which is odd! Seems to, on the whole, default to PAGE_SIZE which is 4096.

A better way of dealing with this is to have your own buffer, and transfer data from the serial buffer to that using another thread. That way you can have whatever buffer size you need and can leave the serial driver to do it's job.
Is there anyway change the driver default setting in Linux?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 20494
Joined: Sat Jul 30, 2011 7:41 pm

Re: How to increase the serial recieve buffer size in C linux

Thu Sep 06, 2018 2:03 pm

zoalsay wrote:
Thu Sep 06, 2018 1:43 pm
jamesh wrote:
Thu Sep 06, 2018 1:29 pm
Not found one. Which is odd! Seems to, on the whole, default to PAGE_SIZE which is 4096.

A better way of dealing with this is to have your own buffer, and transfer data from the serial buffer to that using another thread. That way you can have whatever buffer size you need and can leave the serial driver to do it's job.
Is there anyway change the driver default setting in Linux?
As I said, not that I have found. You could perhaps rebuild the kernel with a larger buffer size. Not sure which particular uart drivers we use though.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Please direct all questions to the forum, I do not do support via PM.

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 3 guests