在运行代码时等待指定的时间,而不将代码放在另一个线程中

时间:2018-05-23 02:01:58

标签: python gps gpsd

堆叠朋友! 我有一个GPS模块,发现自己在等待GPS模块找到卫星 - 除非GPS模块正在等待寻找卫星,Python无法运行任何其他东西。我不想将GPS模块放在另一个线程上。我必须把它放在另一个线程中吗?是否有GPS功能允许我每隔5秒左右查询一次数据?我遵循了本教程:https://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi/introduction

这是我的代码:

import os
import serial
import time
import gps #GPSD Library

#See wiring diagram here:
#https://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi/using-uart-instead-of-usb

class pa6h():

def __init__(self):
    # GPS is on Serial 0. For Raspberry Pi 3 make sure you also
    # 1. Turn on serial Hardware in raspi-config
    # 2. Put enable_uart=1 in the /boot/config.txt
    # 3. Remove any console=... from /boot/cmdline.txt EXCEPT console=tty1
    # 4. Run sudo chmod 777 /dev/serial0
    # Test code by connecting Pi TX and RX to each other
    # self.ser = serial.Serial("dev/serial0", timeout=0.1)
    os.system('sudo gpsd /dev/serial0 -F /var/run/gpsd.sock')
    self.session = gps.gps("localhost", "2947") #GPSD is on Port 2947


def read_gps(self):

    # Test code by connecting Pi TX and RX to each other
    # self.ser.write("TEST")
    # time.sleep(0.1)
    # data = ser.read(10)

    self.session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
    while True:
        try:
            report = self.session.next()
            # Wait for a 'TPV' report and display the current time
            # print report

            # If has TPV, report latitude and longitude
            if report['class'] == 'TPV':
                if hasattr(report, 'lat') and hasattr(report, 'lon'):
                    return [report.lat, report.lon]
        except KeyError:
            pass
        except KeyboardInterrupt:
            quit()
        except StopIteration:
                session = None
                print "GPSD has terminated"

0 个答案:

没有答案