在python中的in-for循环外部获取gps3数据(在for循环外部调用类函数)

2024-09-30 05:18:03 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我有一个示例脚本,它用python从gps3获取数据

from gps3 import agps3
gps_socket = agps3.GPSDSocket()
data_stream = agps3.DataStream()
gps_socket.connect()
gps_socket.watch()
for new_data in gps_socket:
    if new_data:
        data_stream.unpack(new_data)
        print('Altitude = ', data_stream.alt)
        print('Latitude = ', data_stream.lat)

效果很好

我有一个不同的脚本,其中有函数调用:

def getdata()
    ch = port.readline(); #this gets the data
    data_stream.unpack(new_data)
    thetime = datastream.time; # I want to get the gps time right after the function call
    return ch, thetime 

我想做的是在调用readline函数后立即获取gps时间(readline与gps无关,在本例中,它是一个不同的uart)

我想从gps3获取一个新的_数据对象或一批新的gpsd数据

编辑: 我也在一个while循环中尝试了这个方法,但我不确定如何解包数据,因为它不起作用

while 1==1:
    time.sleep(1)
    newdata = gps_socket
    print(newdata)
    data_stream.unpack(newdata)
    print('Altitude = ', data_stream.alt)
    print('Latitude = ', data_stream.lat)
    print('Time = ', str(data_stream.time))

但这不起作用,我不知道为什么,这是我得到的错误:

<gps3.agps3.GPSDSocket object at 0xb5c85bf0>
Traceback (most recent call last):
  File "/home/pi/gpsmag3.py", line 19, in <module>
    data_stream.unpack(newdata)

我知道我从类中得到了一个指针,我只是不明白为什么解包不能得到数据

如何在函数调用中从gps_套接字获取新的_数据对象,以获取当时的gps时间


Tags: the数据脚本newdatastreamreadlinetime

热门问题