7

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.

Renato
  • 345
  • 1
  • 5
  • 14

3 Answers3

7

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 :)

Qwerty
  • 83
  • 2
  • 8
5

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.

pah
  • 4,700
  • 6
  • 28
  • 37
  • Realized the changes as you mentioned. I do not have any answers yet. – Renato May 08 '14 at 03:41
  • 1
    Can you try ser.write("AT\n") and ser.write("AT\r\n") ? – pah May 08 '14 at 04:02
  • Yes, I tried both options. My device is a modem ZTE190, and I'm using Python 2.7.6 – Renato May 08 '14 at 04:36
  • Can you successfully send and receive data from your /dev/ttyUSB1 device through minicom? If so, use the full configuration settings when calling the serial.Serial(), eg, set all the following variables accordingly with a working configuration: baudrate, bytesize,parity,stopbits,timeout,xonxoff and rtscts – pah May 08 '14 at 05:05
1

it's a multiple lines output what the modem returns to your application. will need multiple readline calls to collect all the output (the OK is not the first response line and neither it is second one, and if readline calls are too fast will be lost the tail of the message). consider following code instead (the answer var would be complete modem reply placeholder):

`

import serial, time

modem = serial.Serial(port='/dev/ttyHS0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1, xonoff=False, rtscts=False, dsrdtr=False)

cmd = "AT\r"

modem.write(cmd.encode())

answer = ""

read_timeout = 0.1

quantity = modem.in_waiting 

while True:

    if quantity > 0:

           answer += modem.read(quantity)
    else:
                # read_timeout is depends on port speed

                # with following formula it works:

                # 0.1 sec + 1.0 sec / baud rate (bits per second) * 10.0 bits (per character) * 10.0 times

                # example for 115200 baud rate:

                # 0.1 + 1.0 / 115200 * 10.0 * 10.0 ~ 0.1 sec

           time.sleep(read_timeout) 

    quantity = modem.in_waiting

    if quantity == 0:

           break

`

Oleg Kokorin
  • 2,288
  • 2
  • 16
  • 28