I have got a GPS module connected to my Raspberry Pi and I want the program to end if the device gets disconnected at any point. If I disconnect it now, the program just hangs. Any way to solve this?
This is my code:
import serial
import time
import string
import pynmea2
while True:
port="/dev/ttyAMA0"
ser=serial.Serial(port, baudrate=9600, timeout=1)
dataout = pynmea2.NMEAStreamReader()
newdata=ser.readline()
if newdata[0:6] == "$GPRMC":
newmsg=pynmea2.parse(newdata)
lat=newmsg.latitude
lng=newmsg.longitude
gps = str(lat) + ", " + str(lng)
print(gps)