问题
I am using python to communicate with GSM modem connected in my ttyUSB port.
import serial
from curses import ascii
ser=serial.Serial('/dev/ttyUSB0', 9600, timeout=3)
command = ser.write('AT+CMGR=3\r\n')
print command
Now to communicate with my modem, I pass AT commands from python, I need to print the output of the complete command string. How do I do it?
回答1:
If you mean how to get output from the modem, use read
, readline
or readlines
methods of ser
. See tutorial.
回答2:
First, an AT command should be terminated with only \r
and not \r\n
(unless you have changed S3, and you should not do that), see V.250 for more details on that and AT commands in general (e.g. if you have not already read that specification it is highly recommended).
Then, for reading from the serial port, this is already covered in Janne Karila's answer.
Thirdly, in order to handle AT commands you must read and parse the responses you get from the modem, see this answer for description of how this should be done.
来源:https://stackoverflow.com/questions/16910998/print-output-of-at-command