10

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?

1

4 Answers 4

7

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

from serial import Serial
ser = Serial(port='COM1', baudrate=115200, timeout=1, writeTimeout=1)
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.

3
  • Keep in mind that this command is a recommendation to the driver. It will work only with certain drivers. in principle, this should be platform independent. I have worked with Windows drivers which do not take this recommendation.
    – Valentyn
    Jun 5, 2018 at 14:06
  • 2
    What is this ser object that you are calling the method from? I tried both the port and the serial from import serial, but both times I get AttributeError: 'Serial' object has no attribute 'set_buffer_size' and AttributeError: module 'serial' has no attribute 'set_buffer_size' respectively
    – LukasFun
    Jan 14, 2022 at 15:54
  • @LukasFun I have added clarification. "ser" is Serial object you create for the device you want to connect. You only must have port specified, the rest is optional. Which OS are you using? It is not guaranteed that you can change the buffer size. I do not know exactly when you can and when you cannot. On Di-245 by DATAQ you can and DI-4108 by DATAQ you cannot. I do not know what is the difference.
    – Valentyn
    Jun 7, 2022 at 0:34
4

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!

1

I'm guessing that you are reading 1020 bytes because that is all there is in the buffer, which is what ser.inWaiting() is returning. Depending on the baud rate 50 KB may take a while to transfer, or the phone is expecting something different from you. Handshaking?

Inspect the value of ser.inWaiting, and then the contents of what you are receiving for hints.

0

pySerial uses the native OS drivers for serial receiving. In the case of Windows, the size of the input driver is based on the device driver.

You may be able to increase the size in your Device Manager settings if it is possible, but ultimately you just need to read the data in fast enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.