Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand input buffer size of pyserial

I want to communicate with the phone via serial port. After writing some command to phone, I used ser.read(ser.inWaiting()) to get its return value, but I always got total 1020 bytes of characters, and actually, the desired returns is supposed to be over 50KB.

I have tried to set ser.read(50000), but the interpreter will hang on.

How would I expand the input buffer to get all of the returns at once?

like image 227
Ired Avatar asked Sep 06 '12 14:09

Ired


2 Answers

I have had exactly the same problem, including the 1020 byte buffer size and haven't found a way to change this. My solution has been to implement a loop like:

in_buff=''
while mbed.inWaiting():
    in_buff+=mbed.read(mbed.inWaiting())    #read the contents of the buffer
    time.sleep(0.11)     #depending on your hardware, it can take time to refill the buffer

I would be very pleased if someone can come up with a buffer-resize solution!

like image 183
Andy_K Avatar answered Sep 29 '22 23:09

Andy_K


If you run your code on Windows platform, you simply need to add a line in your code.

ser.set_buffer_size(rx_size = 12800, tx_size = 12800)

Where 12800 is an arbitrary number I chose. You can make receiving(rx) and transmitting(tx) buffer as big as 2147483647 (equal to 2^31 - 1)

this will allow you to expand the input buffer to get all of the returns at once.

Be aware that this will work only with some drivers since it is a recommendation. The driver might not take your recommendation and will stick with its' original buffer size.

like image 30
Valentyn Avatar answered Sep 29 '22 22:09

Valentyn