Python - 非线程池上的GPSD用法

时间:2016-05-22 11:22:11

标签: python-2.7 gps polling gpsd

我正在使用python实现GPSD轮询: 遵循这里的python示例: http://www.catb.org/gpsd/client-howto.html#_python_examples

我无法在此处使用代码: https://gist.github.com/wolfg1969/4653340 因为我必须在我的系统中守护大约10个进程,所以我会选择catb,以便于实现。

我对以下代码有疑问,为什么它在两个循环后停止?我怎么能解决这个问题?感谢。

def GpsDetection():

global gpsd
gpsd = gps(mode=WATCH_ENABLE)

try:
    while 1:
        # Do stuff
        report = gpsd.next()
        # Check report class for 'DEVICE' messages from gpsd.  If we're expecting messages from multiple devices we should
        # inspect the message to determine which device has just become available.  But if we're just listening
        # to a single device, this may do.
        print report
        if report['class'] == 'DEVICE':
            # Clean up our current connection.
            gpsd.close()
            # Tell gpsd we're ready to receive messages.
            gpsd = gps(mode=WATCH_ENABLE)
        # Do more stuff
        print "GPSD Data is showing now!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        print datetime.datetime.now()
        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(1)
except StopIteration:
    print "GPSD has terminated"

return

1 个答案:

答案 0 :(得分:1)

由于这个问题,我很好奇如何从gpsd中为慢速或慢速gps位置要求线程化JSON数据。

如果客户端与gpsd通信的套接字填满,未读取或转储,则gpsd将关闭该套接字。

我告诉很多事情可以在4800波特一秒钟内发生。毫不奇怪,守护进程在大约8到15秒内放弃一个完整的缓冲区,或者在它超时时放弃。

我写了a threading shim for a Python 2-3 gpsd client

那,the python client,需要导入机制并睡眠以减慢其速度。

from time import sleep
from agps3threaded import AGPS3mechanism

agps_thread = AGPS3mechanism()  # Instantiate AGPS3 Mechanisms
agps_thread.stream_data(host='192.168.0.4')  # From localhost (), or other hosts, by example, (host='gps.ddns.net')
agps_thread.run_thread(usnap=.2)  # Throttle the time to sleep after an empty lookup, default 0.2 two tenths of a second

while True:  # All data is available via instantiated thread data stream attribute.
# line #140-ff of /usr/local/lib/python3.5/dist-packages/gps3/agps.py
    print('-----')
    print(agps_thread.data_stream.time)
    print('Lat:{}'.format(agps_thread.data_stream.lat))
    print('Lon:{}'.format(agps_thread.data_stream.lon))
    print('Speed:{}'.format(agps_thread.data_stream.speed))
    print('Course:{}'.format(agps_thread.data_stream.track))
    print('-----')
    sleep(30)

这将解决您的线程gpsd数据问题。

如果你想知道为什么关于代码没有工作,它在哪里中断或停止,以及错误信息是什么?