开发者

pySerial buffer won't flush

开发者 https://www.devze.com 2023-04-01 20:56 出处:网络
I\'m having a problem with serial IO under both Windows and Linux using pySerial.With this code the device never receives the command and the read times out:

I'm having a problem with serial IO under both Windows and Linux using pySerial. With this code the device never receives the command and the read times out:

import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
ser.write("get")
ser.flush()
print ser.read()

This code times out the first time through, but s开发者_JS百科ubsequent iterations succeed:

import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
while True:
    ser.write("get")
    ser.flush()
    print ser.read()

Can anyone tell what's going on? I tried to add a call to sync() but it wouldn't take a serial object as it's argument.

Thanks, Robert


Put some delay in between write and read e.g.

import serial
ser = serial.Serial('/dev/ttyUSB0',9600,timeout=5)
ser.flushInput()
ser.flushOutput()
ser.write("get") 

# sleep(1) for 100 millisecond delay
# 100ms dely
sleep(.1)
print ser.read()


Question is really old, but I feel this might be relevant addition.

Some devices (such as Agilent E3631, for example) rely on DTR. Some ultra-cheap adapters do not have DTR line (or do not have it broken out), and using those, such devices may never behave in expected manner (delays between reads and writes get ridiculously long).

If you find yourself wrestling with such a device, my recommendation is to get an adapter with DTR.


This is because pyserial returns from opening the port before it is actually ready. I've noticed that things like flushInput() don't actually clear the input buffer, for example, if called immediately after the open(). Following is code to demonstrate:

import unittest
import serial
import time
"""
1) create a virtual or real connection between COM12 and COM13
2) in a terminal connected to COM12 (at 9600, N81), enter some junk text (e.g.'sdgfdsgasdg')
3) then execute this unit test
"""

class Test_test1(unittest.TestCase):
    def test_A(self):
        with serial.Serial(port='COM13', baudrate=9600) as s:   # open serial port
            print("Read ASAP:  {}".format(s.read(s.in_waiting)))
            time.sleep(0.1)     # wait for 100 ms for pyserial port to actually be ready
            print("Read after delay:  {}".format(s.read(s.in_waiting)))

if __name__ == '__main__':
    unittest.main()

"""
output will be:
Read ASAP:  b''
Read after delay:  b'sdgfdsgasdg'
.
----------------------------------------------------------------------
Ran 1 test in 0.101s
"""

My workaround has been to implement a 100ms delay after opening before doing anything.


Sorry that this is old and obvious to some, but I didn't see this option mentioned here. I ended up calling a read_all() when flush wasn't doing anything with my hardware.

# Stopped reading for a while on the connection so things build up

# Neither of these were working
conn.flush()
conn.flushInput()

# This did the trick, return value is ignored
conn.read_all()

# Waits for next line
conn.read_line()
0

精彩评论

暂无评论...
验证码 换一张
取 消