Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read response AT command with pySerial

I have the following sample code:

import serial

ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=5)
ser.write("AT\r")
response =  ser.readline()
ser.write(chr(26)) 
ser.close()

print response

My goal is to send the AT command and get your answer OK.

The documentation of PySerial readline() says reads the data received until it finds a line break, the problem is that my print is returning nothing.

I'm sure that after the AT command, the response that the 3G modem sends me is OK. Anyone know the reason why you can not retrieve the answer?

PS: using programs like CuteCom, I got confirmation that the device works and that it responds to AT commands.

like image 821
Renato Avatar asked May 08 '14 03:05

Renato


2 Answers

Received problem with code:

import serial
ser = serial.Serial(port='COM1', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonoff=False, rtscts=False, dsrdtr=False)
cmd="AT\r"
ser.write(cmd.encode())
msg=ser.read(64)
print(msg)

output is OK :)

like image 186
Qwerty Avatar answered Sep 16 '22 11:09

Qwerty


In order to complement the question comments, please try this and see if anything changes:

import serial

ser = serial.Serial('/dev/ttyUSB1', 115200, timeout=5)
ser.write("AT\r")
response =  ser.read(2)
print response
ser.close()

If everything works, then add the "\r" to your write() and replace the ser.read(2) with ser.readline() and set the timeout value to zero again.

like image 37
pah Avatar answered Sep 17 '22 11:09

pah