Hi Everyone,
Currently I’m working in a GPS project which is using latitude and longitude. I have getting both coordinates from GPS module and I need to store them into Array to further calculations. both coordinates with some decimal points. I hope to store them in arrays as LatitudeData and LongitudeData which can access globally anywhere in main code.
import serial
global Degrees, NMEA1, NMEA2, NMEA3, DDD, MMM, Value
LatitudeData = []
LongitudeData = []
global index
def Latitude(NMEA1, NMEA2, NMEA3,):
if NMEA1 == 'A': # Check Validity
Degrees = NMEA2[:2]
if NMEA3 == 's':
Degrees = int(Degrees)*-1
else:
Degrees = int(Degrees)
Degrees = str(Degrees).strip('.0')
DDD = NMEA2[2:10]
MMM = float(DDD)/60
MMM = str(MMM).strip('0.')[:6]
Value = Degrees + "." + MMM
return Value
else:
return 0.0
print("Location Invalid")
def Longitude(NMEA1, NMEA2, NMEA3,):
if NMEA1 == 'A':
Degrees = NMEA2[1:3]
if NMEA3 == 'w':
Degrees = int(Degrees)* -1
else:
Degrees = int(Degrees)
Degrees = str(Degrees)
DDD = NMEA2[3:10]
MMM = float(DDD)/60
MMM = str(MMM).strip('0.')[:6]
Value = Degrees + "." + MMM
return Value
else:
return 0.0
print("Location Invalid")
try:
gps = serial.Serial('com5', 115200)
while True:
packet = gps.readline()
decode = packet.decode('unicode_escape')
data = decode.split(",")
# print(data)
if data[0] =='$GNRMC':
# print(data)
Output1 = Latitude(data[2], data[3], data[4])
print(Output1)
Output2 = Longitude(data[2], data[5], data[6])
print(Output2)
index = 0
LocationData1[index] = Output1
LocationData2[index] = Output2
index = index + 1
if index == 9:
index = 1
except serial.SerialException:
print("Error in Communication")
I hope to store 10 values to each array and get mean value. So how to do this. I’m new to python coding and I tried several ways but couldn’t find some solution. So I need your comments.
Thank You.