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
"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.
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.
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.
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.
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 ().
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.
'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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With