From 4454747453e9a550affc9a3f3b350409e258ba6c Mon Sep 17 00:00:00 2001 From: Alan Johnston Date: Sun, 14 Dec 2025 08:08:01 -0500 Subject: [PATCH] Update gps_client.py return -1 if no socket --- gps_client.py | 93 +++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/gps_client.py b/gps_client.py index 74826a60..1c4712fe 100644 --- a/gps_client.py +++ b/gps_client.py @@ -11,55 +11,54 @@ run this way: python3 example1.py.txt import gps # the gpsd interface module import time -session = gps.gps(mode=gps.WATCH_ENABLE) - -start = time.perf_counter() - -mode = -1 - -lat = 0 - -lon = 0 - +mode = -1 +lat = 0 +lon = 0 alt = 0 try: - while (session.read() == 0) and ((time.perf_counter() - start) < 2) and (mode < 2): -# print(gps.MODE_SET) - # print(session.valid) - if (session.valid): - # not useful, probably not a TPV message -# continue - -# print('Mode: %s(%d) Time: ' % -# (("Invalid", "NO_FIX", "2D", "3D")[session.fix.mode], -# session.fix.mode), end="") - # print time, if we have it - print("%d " % session.fix.mode, end="") - if (session.fix.mode > mode): - mode = session.fix.mode - # if gps.TIME_SET & session.valid: - # print(session.fix.time, end="") - # else: - # print('n/a', end="") - - if gps.isfinite(session.fix.latitude): - lat = session.fix.latitude - if gps.isfinite(session.fix.longitude): - lon = session.fix.longitude - if gps.isfinite(session.fix.altitude): - alt = session.fix.altitude - print("%.6f %.6f %.6f" % - (session.fix.latitude, session.fix.longitude, session.fix.altitude)) - -# else: -# print(" 0 0 0") - -except KeyboardInterrupt: - # got a ^C. Say bye, bye - print('') - -# Got ^C, or fell out of the loop. Cleanup, and leave. -session.close() + + session = gps.gps(mode=gps.WATCH_ENABLE) + + start = time.perf_counter() + + try: + while (session.read() == 0) and ((time.perf_counter() - start) < 2) and (mode < 2): + # print(gps.MODE_SET) + # print(session.valid) + if (session.valid): + # not useful, probably not a TPV message + # continue + + # print('Mode: %s(%d) Time: ' % + # (("Invalid", "NO_FIX", "2D", "3D")[session.fix.mode], + # session.fix.mode), end="") + # print time, if we have it + print("%d " % session.fix.mode, end="") + if (session.fix.mode > mode): + mode = session.fix.mode + # if gps.TIME_SET & session.valid: + # print(session.fix.time, end="") + # else: + # print('n/a', end="") + + if gps.isfinite(session.fix.latitude): + lat = session.fix.latitude + if gps.isfinite(session.fix.longitude): + lon = session.fix.longitude + if gps.isfinite(session.fix.altitude): + alt = session.fix.altitude + print("%.6f %.6f %.6f" % + (session.fix.latitude, session.fix.longitude, session.fix.altitude)) + + # else: + # print(" 0 0 0") + + except KeyboardInterrupt: + # got a ^C. Say bye, bye + print('') + + # Got ^C, or fell out of the loop. Cleanup, and leave. + session.close() print("%d %.6f %.6f %.1f" % (mode, lat, lon, alt)) exit(0)