gpsd python client

z3a picture z3a · Jul 21, 2010 · Viewed 20.7k times · Source

I'm trying to write a very simple python client for Gpsd, but I have this error after some time of execute the script:

Traceback (most recent call last):
 File "gps_cap.py", line 13, in <module>
   g.stream()
 File "/usr/lib/python2.6/site-packages/gps/gps.py", line 348, in stream
   gpsjson.stream(self, flags)
 File "/usr/lib/python2.6/site-packages/gps/client.py", line 176, in stream
   return self.send(arg + "}")
 File "/usr/lib/python2.6/site-packages/gps/client.py", line 111, in send
   self.sock.send(commands)
socket.error: [Errno 104] Connection reset by peer

and this is my python code:

import os
from gps import *
from time import *

g = gps(mode=WATCH_ENABLE)
while 1:
      os.system('clear')
       g.poll()
       if PACKET_SET:
               g.stream()

       print
       print ' GPS reading'
       print '----------------------------------------'
       print 'latitude    ' , g.fix.latitude
       print 'longitude   ' , g.fix.longitude
       print 'time utc    ' , g.utc,' + ', g.fix.time
       print 'altitude    ' , g.fix.altitude
       print 'epc         ' , g.fix.epc
       print 'epd         ' , g.fix.epd
       print 'eps         ' , g.fix.eps
       print 'epx         ' , g.fix.epx
       print 'epv         ' , g.fix.epv
       print 'ept         ' , g.fix.ept
       print 'speed       ' , g.fix.speed
       print 'climb       ' , g.fix.climb
       print 'track       ' , g.fix.track
       print 'mode        ' , g.fix.mode
       print
       print 'sats        ' , g.satellites

       sleep(1)

Maybe anyone can help with this issue? I'm runnig Gpsd 2.95 in a ArchLinux box.

Thanks!

Answer

codenameLxL picture codenameLxL · Jul 27, 2014

I know this question is pretty old but i still drop my answer here in case someone needs it in the future:

#! /usr/bin/python
# Written by Dan Mandle http://dan.mandle.me September 2012
# License: GPL 2.0 
import os
from gps import *
from time import *
import time
import threading

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    global gpsd #bring it in scope
    gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
    self.current_value = None
    self.running = True #setting the thread running to true

  def run(self):
    global gpsd
    while gpsp.running:
      gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer

if __name__ == '__main__':
  gpsp = GpsPoller() # create the thread
  try:
    gpsp.start() # start it up
    while True:
      #It may take a second or two to get good data
      #print gpsd.fix.latitude,', ',gpsd.fix.longitude,'  Time: ',gpsd.utc

      os.system('clear')

      print
      print ' GPS reading'
      print '----------------------------------------'
      print 'latitude    ' , gpsd.fix.latitude
      print 'longitude   ' , gpsd.fix.longitude
      print 'time utc    ' , gpsd.utc,' + ', gpsd.fix.time
      print 'altitude (m)' , gpsd.fix.altitude
      print 'eps         ' , gpsd.fix.eps
      print 'epx         ' , gpsd.fix.epx
      print 'epv         ' , gpsd.fix.epv
      print 'ept         ' , gpsd.fix.ept
      print 'speed (m/s) ' , gpsd.fix.speed
      print 'climb       ' , gpsd.fix.climb
      print 'track       ' , gpsd.fix.track
      print 'mode        ' , gpsd.fix.mode
      print
      print 'sats        ' , gpsd.satellites

      time.sleep(5) #set to whatever

  except (KeyboardInterrupt, SystemExit): #when you press ctrl+c
    print "\nKilling Thread..."
    gpsp.running = False
    gpsp.join() # wait for the thread to finish what it's doing
  print "Done.\nExiting."

This code work with thread and will give out a nice output of gpsd data to the screen. It can be terminated with Ctrl + C.

All credits go to http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/