Page 1 of 1

Serial port use with pyserial

Posted: Tue Feb 21, 2017 9:30 am
by nabili
Hi everyone!

I'm a begginer with the raspberry pi (I own version 3, running Jessie) and I try to comminucate with a LoRa module taking AT commands that we write on the USB serial port.

To do so, I've wrote a small script in python (v 2.7.9) using the pyserial library (v 3.2.1) :

Code: Select all

import serial

# Set up the connection to the dongle
dongle = serial.Serial(port="/dev/ttyUSB1",baudrate=38400,timeout=0,rtscts=0,xonxoff=0)

# get help 
dongle.write('help')

# Close the connection
dongle.close()
But when I execute it, nothing is happening, for now I'm just trying to write something on the serial port, even if it's gargage. Also I've noticed that on minicom or putty, I can read data from the USB module (It send a start message at each reset) but I can't write to it.

Also, I've tried to activate/desactivate the shell login from serial, it's not fixing the problem

Any idea on what I've possibly did wrong?

Thanks ;)

Re: Serial port use with pyserial

Posted: Tue Feb 21, 2017 9:51 am
by nabili
Also, the configuration of the device :
Baud Rate : 38400
Data : 8 bits
Parity : None
Stop : 1 bit
Flow control : None
End line character : LF

Re: Serial port use with pyserial

Posted: Tue Feb 21, 2017 1:51 pm
by ghp
Hello,
if minicom can receive from the module, but no writes are possible, then check the docs if there is a mismatch in line ending (crlf, lf). Or perhaps the data stream needs some special chars to switch into at command mode (in old hayes modems, this has been '+++' or alike).
For the python program, check whether /dev/ttyUSB1 or /dev/ttyUSB0 is the correct connection. Should be the same as you use in minicom (and close minicom when trying to run the python code).
Regards,
Gerhard

Re: Serial port use with pyserial

Posted: Wed Feb 22, 2017 10:51 am
by Davies
does python return the response from an AT write command without additional code?
im unsure, but i think even when you have a successful write through python you dont see unless you catch the response and print it to console.

what happens if you try,

Code: Select all

import serial

dongle = serial.Serial(port="/dev/ttyUSB1",baudrate=38400,timeout=0,rtscts=0,xonxoff=0)

dongle.write('help')
response = dongle.readline()
dongle.close()
print response()
this works for my sim800 device to detect if its switched on but i write AT which should return OK if its on but returns AT instead, im unsure if it yeilds the response or just the input that was wrote . also i have to call this multiple times on first boot to get a response.
perhaps you may need to use PIPE or something as such.. maybe dongle.readlines() to see more than 1 line