I need to communicate with some device using serial port. I know this device parameters (boudrate etc.). I also know that this device waits untill it receives a new line character (I don't know unix or windows type) and then sends back something in ASCII.
The problem is that when i want to write anything the script hangs. No error occurs, just nothing happens later. It's impossible to print anything or even to close the port.
How can I fix that?
It doesn't work for really basic script. Of course this is no a whole code, just an example to present what is not working.
Sadly, I didn't find any explanation or an answer which works.
I'm working on Ubuntu 16.04
Thanks for your help!
import serial
ser = serial.Serial(
port="/dev/NameOfDevice",
baudrate=115200,
bytesize=8,
parity='N',
stopbits=1,
timeout = 0)
ser.open()
print('Port was opened')
ser.write(b'\r\n') #this is a line where it stops working. Different inputs were tried, nothing works.
print('Does it works?')
edit: I was a fool before, but now I am certain I am correct.
I ran into the same problem and solved it by adding a "write_timeout":
ser = serial.Serial(
port="/dev/NameOfDevice",
baudrate=115200,
bytesize=8,
parity='N',
stopbits=1,
write_timeout = 1,
timeout = 0)
Note that this will raise a timeout exception. In my case this is the expected behavior, because I'm checking to see if I can properly connect on the user specified port.
So I suspect you are connecting to something, but it isn't acknowledging your write, and since the default behavior for 'write' is to block until acknowledgement, this is what you see.
See also the api description for the write function: https://pyserial.readthedocs.io/en/latest/pyserial_api.html
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