How to increase the serial transmit buffer size in the Kernel.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to increase the serial transmit buffer size in the Kernel.
I am writing an application in C++ that sends large packets out the /dev/ttyS0 serial port of a PC using the write function.
The problem is for writing packets larger than 4096 bytes.
I would like my program to be able to transmit packets larger than 4096 bytes without being blocked.
I've looked in the kernel code (linux\drivers\char\serial.c) and there is a #define called SERIAL_XMIT_SIZE. At first I tought maybe I could change that but it seems that the trasmit buffer is actially fixed to be a memory page (4k).
Any ideas on how I can change this?
I could create a seperate serial transmit thread or create a secondary buffer that gets polled but this seems like a lot of bother when the kernel already provides the mechanism.
Thanks
Last edited by pwlodarczyk; 01-17-2005 at 07:16 PM.
I suppose you could change the size to two pages by editing include/kernel/serial.h and setting SERIAL_XMIT_SIZE to page*2. You'd certainly have to go through and see where it is used and make sure they don't do anything that depends on the size being exactly one page. Is there a particular reason why blocking is hurting you?
then its impossible to block. You see no matter what buffer size the kernel has, it won't be big enough for someone somewhere in their application... so the answer is you just accept the fact that the write() function may returns with less bytes written than you requested.
This is the normal behavior and the way you deal with it is you call select() and wait for write access to be available again, just like any other fd.
I always open the serial port like this: open(serial->port, O_RDWR | O_NOCTTY | O_NONBLOCK)
I realise that this won't block.
If want to transmit a 3k block of data, I can open the port as you suggest, and just write it out using write().
It will add the data to the kernel serial transmit buffer, return straight away so I can continue to do important things.
The problem is if I want to write a block greater than 4k. If I want to return stright away, I need to have a secondary buffer that other transmitters also access.
I then need to make sure that something somewhere in the code (a thread or polled task) checks my secondary buffer to see if more data should be written.
This isn't that hard to do but I didn't want to bother with it. I guess I will have to.
how to increase serial read buffer size from 4096 bytes?
hello everyone!!!
i have a similar kind of problem.
i am writing an application in C that receives large packets from the /dev/ttyS0 serial port of a PC using the read function.
The problem is for reading packets larger than 4096 bytes.
i have created a buffer in which i save the incoming packets but i cant seem to increase its size more than 4096 bytes.
please help me in this regard, i would really appreciate an urgent reply
thanks in advance.
I think its better not to think of the data coming from a serial port as packets. Its really not packets, but a byte stream. You application layer software decides where a boundary exist if there are any, but its purely arbitrary.
I'm not exactly sure what problem you're describing, but if you're trying to call read() with a buffer size larger than 4096 and you're never receiving more than 4096, then that is normal behavior for a serial port. Keep calling read() until you receive all the data you expect, it may take several calls.
here is my part of the code where m reading data from serial port into a buffer n thn from buffer m writing the data into a file.
this pgm is workin fine, it reads data from serial port, n creates and writes that data in to a file also, but the problem is jus that i want to recieve files greater than 4096bytes, even if i increase the BUFFSIZE, and sends a file greater than 4096bytes from the other pc , it does not receive more than 4096bytes.
please help me that where i am going wrong, n how can i do this.
thanks
# define BUFFSIZE 4096
char buf[BUFFSIZE];
char *bufptr;
bufptr=buf;
//fp is a pointer to a file
while( (nbytes = read(fd,bufptr,512)) > 0 )
{
I'm still not understanding your code, so here is my example
for reading forever, or at least until the buffer is completely full.
You can add your own test in the loop to break out when the
entire file has been transferred, however big that may be.
Code:
char *buf;
int n, used, bufsize;
bufsize=1000000;
buf=malloc(bufsize);
used=0;
while (used<bufsize) {
n=read(fd,buf+used,bufsize-used);
if (0==n) break; //unknown serial port error
used+=n;
}
thnx for ur reply !!
well yah now my program is working fine, and the serial port is able to recieve a file greater than 4K.
but i have observed another problem that the serial port is not reading the complete file being sent to it. that is if a file of size 22K is sent to it ,thn it only reieves till 20K. n the size of the datanot being read is not constant, it varies.
am not being able to figure out that what isthe problem, maybe it hasto do with flushing.
here is my code...
while( used<bufsize)
{
nbytes=read ( fd, buf, bufsize-used ); //data being read into a buffer 'buf' from serial port
while( (*buf!='\0') && fputc( *(buf++),fp ) !=EOF); // this saves data being read from the buffer into a file.
used+=nbytes;
hello!!!
am having the same problem, even after makin an amendment tht u suggested.
there's no problem of the file size anymore, but last 1 to 2 Kbytes are not saved.
pls reply soon
thanks in advance
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.