Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pySerial buffer won't flush

Tags:

pyserial

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 subsequent 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

like image 677
Robert Jordan Avatar asked Sep 01 '11 05:09

Robert Jordan


People also ask

What does flush do in pySerial?

"Flush input buffer, discarding all its contents." Typically only used after changing the serial port parameters (e.g. port initialization) or for error recovery. "Clear output buffer, aborting the current output and discarding all that is in the buffer.

What is timeout in python serial?

timeout = x : set timeout to x seconds (float allowed) returns immediately when the requested number of bytes are available, otherwise wait until the timeout expires and return all bytes that were received until then.

How do you exit a serial in Python?

You can use the with statement. The with statement will automatically close ser and text_file when execution leaves the with statement. This can be an exception in your case.

What is pySerial API?

pySerial is a Python API module to access the serial port. pySerial provides a uniform API across multiple operating systems, including Windows, Linux, and BSD.

Why doesn't flushinput () clear the input buffer when pyserial is called?

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 ().

What is pyserial and how does it work?

The PySerial and functions like python serial read make communication with Serial Ports easier. Python on a computer with the PySerial package installed can communicate with external hardware.

What does 'flush' mean in a serial monitor?

'flush' does not mean 'clear'; it will just block while sending all the data that was not transmitted yet. And after that you are printing your 'LED is On'; why would that not show in the serial monitor? okay, just to make shure that I understood it right (I'm not a native english): "flush" does not mean to empty (clear) the serial input buffer.

What is the difference between pyserial and parallel ports?

Parallel ports, on the other hand, transmit multiple bits simultaneously. The PySerial and functions like python serial read make communication with Serial Ports easier. Python on a computer with the PySerial package installed can communicate with external hardware.


2 Answers

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()
like image 190
beebek Avatar answered Sep 16 '22 17:09

beebek


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.

like image 33
Tammi Avatar answered Sep 18 '22 17:09

Tammi