I'm a rookie when it comes to programming but I'm learning. I'm trying to program my mini-pc (Rock 4 SE, with a connected 4G phat and GPS modem) to send GPS location with a python script through minicom. When running the script I can tell in minicom (GPS program) that the python script runs minciom (when sending AT+CGPSINFO it answers +CGPSINFO: 1234.56789 etc) and sends commands and gives the GPS location, but the script can't extract the information from the program.
#!/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()
I did some debugging and noticed that the command ser.inWaiting returns 0 when it should actually return the GPS location. Thanks for any help. NOTE: this code was originally made for a raspberry pi, but I have modified it and hope it will work for the Rock 4 SE. Sorry if I formulated this wrong or anything, this is the first time I post on stack overflow.