I'm making a Python program for automatic setup of a modem using AT-commands (not important to know what that is)
I use the serial module for COM port communication.
The way it works is that i send a command, and then I want to "save" the respons until a string saying "OK" comes in the end. I tried this:
import serial
ser = serial.Serial(port='COM15', baudrate=115200, timeout=3, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE)
intline =''
ser.write('AT+COPS?'+'\r\n') '''This specific commands asks for network status'''
while ser.inWaiting() >0:
intline += ser.read(1)
if 'Greentel' in intline:
print "Internet access"
else:
print "No internet access"
My trouble is that sometimes I get multiple lines in respons. As an example, for the command used here I get the respons:
"+UMWI: 0,4
+UMWI: 0,5
AT+COPS?
+COPS: 0,0,"Greentel",7
OK"
What I really want is to make a function who I can call to read until it meets OK, put the text in a string/list, and let me search for specific words in it.
Figured I'd post my solution for the slim chance of others working with AT commands in Python. What I did was make a function who reads the input and stores it in a string until a chosen stopsign is encountered. I made it like this:
def read2sign(command, stopSign):
ser.flushInput()
temp = ''
ser.write(command +'\r\n')
while stopSign not in temp:
temp += ser.read(1)
print "---" '''For debugging purpose'''
print temp
print temp
return temp
I also kinda want to say sorry for posting prematurely. I guess I just had to try longer :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.