ser.inwaiting 返回 0 (b'') 但不应该

问题描述 投票:0回答:0

在编程方面我是菜鸟,但我正在学习。我正在尝试对我的迷你电脑(Rock 4 SE,带有连接的 4G phat 和 GPS 调制解调器)进行编程,以通过 minicom 使用 python 脚本发送 GPS 位置。运行脚本时,我可以在 minicom(GPS 程序)中告诉 python 脚本运行 minciom(发送 AT+CGPSINFO 时它回答 +CGPSINFO: 1234.56789 等)并发送命令并给出 GPS 位置,但脚本无法提取来自程序的信息。

#!/usr/bin/python
# -*- coding:utf-8 -*-
import serial
import time

ser = serial.Serial('/dev/ttyUSB2',115200)
ser.flushInput()

power_key = 6
rec_buff = ''
rec_buff2 = ''
time_count = 0

def send_at(command,back,timeout):
    rec_buff = ''
    ser.write((command+'\r\n').encode())
    time.sleep(timeout)
    if ser.inWaiting():
        time.sleep(0.01 )
        rec_buff = ser.read(ser.inWaiting())
    if rec_buff != '':
        if back not in rec_buff.decode():
            print(command + ' ERROR')
            print(command + ' back:\t' + rec_buff.decode())
            return 0
        else:
            
            #print(rec_buff.decode())
            
            #Additions to Demo Code Written by Tim!
            global GPSDATA
            #print(GPSDATA)
            GPSDATA = str(rec_buff.decode())
            Cleaned = GPSDATA[13:]
            
            #print(Cleaned)
            
            Lat = Cleaned[:2]
            SmallLat = Cleaned[2:11]
            NorthOrSouth = Cleaned[12]
            
            #print(Lat, SmallLat, NorthOrSouth)
            
            Long = Cleaned[14:17]
            SmallLong = Cleaned[17:26]
            EastOrWest = Cleaned[27]
            
            #print(Long, SmallLong, EastOrWest)   
            FinalLat = float(Lat) + (float(SmallLat)/60)
            FinalLong = float(Long) + (float(SmallLong)/60)
            
            if NorthOrSouth == 'S': FinalLat = -FinalLat
            if EastOrWest == 'W': FinalLong = -FinalLong
            
            print(FinalLat, FinalLong)
            
            #print(FinalLat, FinalLong)
            #print(rec_buff.decode())
            
            return 1
    else:
        print('GPS is not ready')
        return 0

def get_gps_position():
    rec_null = True
    answer = 0
    print('Start GPS session...')
    rec_buff = ''
    send_at('AT+CGPS=1,1','OK',1)
    time.sleep(2)
    while rec_null:
        answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1)
        if 1 == answer:
            answer = 0
            if ',,,,,,' in rec_buff:
                print('GPS is not ready')
                rec_null = False
                time.sleep(1)
        else:
            print('error %d'%answer)
            rec_buff = ''
            send_at('AT+CGPS=0','OK',1)
            return False
        time.sleep(1.5)

#Additions to Demo GPS.py Code Added by Tim // Simplfing the GPS Start up process
power_on(power_key)
while True:
    
    get_gps_position()

我做了一些调试并注意到命令 ser.inWaiting 在实际返回 GPS 位置时返回 0。谢谢你的帮助。注意:此代码最初是为树莓派编写的,但我对其进行了修改,希望它适用于 Rock 4 SE。抱歉,如果我表述有误或其他任何内容,这是我第一次在堆栈溢出上发帖。

raspberry-pi serial-port bit pyserial minicom
© www.soinside.com 2019 - 2024. All rights reserved.