我正在使用以下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一行一行。 謝謝 –