How to expand input buffer size of pyserial

a 夏天 提交于 2019-12-04 11:13:03

问题


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:


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!




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/12302155/how-to-expand-input-buffer-size-of-pyserial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!