Advertisement
Guest User

Untitled

a guest
May 9th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from gpsdclient import GPSDClient  # pip3 install gpsdclient
  4.  
  5. client = GPSDClient()
  6. try:
  7.  
  8.     # using a with-statement will automatically close file when leaving the scope
  9.     with open('GPS/gps.txt', 'w') as f:
  10.  
  11.         # get stream of messages from gpsd
  12.         for msg in client.dict_stream():
  13.             if msg['class'] == 'TPV':  # time-position-velocity report
  14.                 # for a list of fields that may be available, see:
  15.                 # https://gpsd.gitlab.io/gpsd/gpsd_json.html#_tpv
  16.                 t = msg.get('time', None)
  17.                 let = msg.get('lat', None)
  18.                 lon = msg.get('lon', None)
  19.                 f.write( f"time={t}, lat={lat}, lon={lon}\n" )
  20.  
  21. except KeyboardInterrupt:
  22.     # exit gracefully on control-C
  23.     pass
  24.  
  25. finally:
  26.     # cleanup
  27.     client.close()
  28.     print("Done and now exiting!")
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement