Page 1 of 1

Making the serial input buffer larger

Posted: Tue Feb 12, 2013 7:00 pm
by Grumpy Mike
I am trying to draw some data sent to the Pi through the USB serial connection of an Arduino.
How big is the serial buffer? It seems to get to about 3900 bytes waiting when it chokes.
Can the buffer be made bigger?

I am using a baud rate of 115200, would I get faster transfer through the serial port on the GPIO pins?

I am transferring the data as bytes rather than strings as I assume it is the quickest way. It has less bytes to transfer it is true but on receiving the bytes I have to do a bit of going through hoops to get it into a number. They are two byte numbers and I do:-

Code: Select all

a = ser.read(2)
value = ( ord(a[0]) << 8) | ( ord(a[1]) )
Is there a quicker way?
Thanks

Re: Making the serial input buffer larger

Posted: Tue Feb 12, 2013 9:12 pm
by techpaul
Are you sure it is serial buffer choking it could be the USB buffers are too small, as your transmission is buffered by USB stack and driver before being passed to serial buffers. Not seen standard serial buffers choke that low before.

Are you sure the Arduino is not choking on sending ?

I would try the direct serial connection to remove different USB stack implementations that may have assumed small buffers at either or both ends of the USB as it is a serial device.

The only faster way I know off top of my head is in C as union of byte array and reading as a short IF the byte order matches array index order. Dont know any other way in Python.

Re: Making the serial input buffer larger

Posted: Wed Feb 13, 2013 7:39 am
by stevech
Something wrong in the app design.
I use Python on a much slower CPU and keep up with a high rate.
But don't try for 100% constant input data at, say, 115,200 baud.
If the hardware loses frame sync, it cannot recover, with 8N1, unless the sender's data has a few character times of idleness.

Also, the serial.Serial library has lots of overhead. You may need to use os.read()

Re: Making the serial input buffer larger

Posted: Wed Feb 13, 2013 8:38 am
by cae2100
do you mind posting the full code for your program? it may help. also I know a little about errors that would cause trouble like your having, with buffer overruns, and try to flush the serial buffer after each read to keep it from overflowing the buffers. you can also try to set up a really small timeout on the serial init commands, that is what worked for me.

Re: Making the serial input buffer larger

Posted: Wed Feb 13, 2013 8:59 am
by rurwin
It would probably be faster to read more characters at one time if you can, and then parse them as a separate step.

In my experience a serial connection never recovers sync while it is full of data, no matter what the stop-bits are set to.

Re: Making the serial input buffer larger

Posted: Wed Feb 13, 2013 11:56 am
by Grumpy Mike
Thanks for all your replies, good to know I am not doing anything silly.
try to flush the serial buffer after each read to keep it from overflowing the buffers.
That would rather defeat the point in having a buffer, which is not to loose any data.
But don't try for 100% constant input data at, say, 115,200 baud.
I wasn't, ... thanks.
Are you sure the Arduino is not choking on sending ?
Yes, the Arduino checked out fine.
Are you sure it is serial buffer choking it could be the USB buffers are too small, as your transmission is buffered by USB stack and driver before being passed to serial buffers.
Good point, I will have to try the direct connection.

However for the moment I have elevated the problem by making the Python drawing part of the program very much faster. Instead of updating the screen with a pygame.display.update on each set of data received, I have used a timer to only call pygame.display.update every 0.2 of a second. This ensures the buffer is empty most of the time and only has 80 to 400 bytes or so in it when python goes walkabout for the odd few seconds.

Thanks for your input