Hi everyone
I’m working on a GPS project and i get GPS coordinates using GPS module through serial communication. I want to plot in graph real time. So I tried to matplotlib but its not working properly. My Sketch as follows.
import statistics
import serial
import matplotlib.pyplot as plt
LatitudeData = []
LongitudeData = []
def Latitude(NMEA1, NMEA2, NMEA3,):
if NMEA1 == 'A':
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 float(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 float(Value)
else:
return 0.0
print("Location Invalid")
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("Current Latitude : " +str(Output1))
Output2 = Longitude(data[2], data[5], data[6])
print("Current Longititude : " + str(Output2))
if (Output1 != 0 and Output2 != 0):
LatitudeData.insert(0, Output1)
LongitudeData.insert(0, Output2)
if len(LongitudeData) and len(LatitudeData) == 1001:
LatitudeData.pop()
LongitudeData.pop()
for x in range(len(LatitudeData)):
plt.scatter(LatitudeData[x],LongitudeData[x])
plt.show()
its show only one time and I need to close plot window to run loop again. GPS continually sending location coordinates so, LatitudeData and LongitudeData lists are updating. I want to plot those all points in single scatter plot window. That means one by one point adding into same plotting window. How can I fix it ? I need your comments.
Thank You.