我正在使用以下python脚本在串行端口ttyUSB1上写入AT + CSQ。 但我什么都看不懂。从串行端口写入和读取
但是,当我在minicom上激活AT + CSQ时,我得到了所需的结果。
这个脚本有什么问题?
日志: 手动脚本
[email protected]:~# python se.py
Serial is open
Serial is open in try block also
write data: AT+CSQ
read data:
read data:
read data:
read data:
日志:
小型机控制台
1. ate
OK
2. at+csq
+CSQ: 20,99
3. at+csq=?
OKSQ: (0-31,99),(99)
我怎样才能得到这些结果在下面的python脚本?
import serial, time
#initialization and open the port
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.timeout = None #block read
#ser.timeout = 0 #non-block read
ser.timeout = 3 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 #timeout for write
try:
ser.open()
print("Serial is open")
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
print("Serial is open in try block also")
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
#write data
ser.write("AT+CSQ")
time.sleep(1)
# ser.write("AT+CSQ=?x0D")
print("write data: AT+CSQ")
# print("write data: AT+CSQ=?x0D")
time.sleep(2) #give the serial port sometime to receive the data
numOfLines = 1
while True:
response = ser.readline()
print("read data: " + response)
numOfLines = numOfLines + 1
if (numOfLines >= 5):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
1.在minicom中,您似乎首先发送了消息,但没有看到您使用可格式化的Python代码发送该消息。 2.当你在minicom中发送一个字符串时,你还可以按回车键作为“\ r”或“\ n”(不知道哪一个可能没关系),并且当你用python发送字符串时您必须明确地将Enter键的击键放入您发送的字符串中,因为它不会自动发送。因此,可能需要确保您发送“ate \ n”(或者它可能是\ r,不确定),然后检查确定响应,然后同样将“\ r”(或“\ n”)添加到您发送的其他字符串。 – barny
请正确格式化问题中的代码。 – hlovdal
嗨, hlovdal,我能够使用现有的代码来获取所需的输出, 抱歉,格式不正确。 :( 这里是我的输出: 根@ imx6slzbha:〜/ python_exercises#蟒蛇serial_tty_working.py 端口打开 写入数据:AT + CSQ = X0D 读取数据:? 读取数据: 读取数据:^ RSSI:19 读取数据: 读取数据:^ HCSQ: “LTE”,46,53,161,30 我可以使用带方便抓取LTE RSSI() 感谢 你有任何想法如何。使用单个python脚本编写多个命令并读取输出l一行一行。 谢谢 –