之前所有关于作为客户端连接到 gpsd 守护进程的 python 代码的答案要么引用非常旧的库,要么不起作用。
我已经在官方网站尝试了示例代码 - 但这也不起作用。
代码是:
import gps
session = gps.gps(mode=gps.WATCH_ENABLE)
try:
while 0 == session.read():
if not (gps.MODE_SET & session.valid):
# not useful, probably not a TPV message
continue
print('Mode: %s(%d) Time: ' %
(("Invalid", "NO_FIX", "2D", "3D")[session.fix.mode],
session.fix.mode), end="")
# print time, if we have it
if gps.TIME_SET & session.valid:
print(session.fix.time, end="")
else:
print('n/a', end="")
if ((gps.isfinite(session.fix.latitude) and
gps.isfinite(session.fix.longitude))):
print(" Lat %.6f Lon %.6f" %
(session.fix.latitude, session.fix.longitude))
else:
print(" Lat n/a Lon n/a")
except KeyboardInterrupt:
# got a ^C. Say bye
print('')
# Got ^C, or fell out of the loop. Cleanup, and leave.
session.close()
exit(0)
但是当我尝试此代码时,错误是:
TypeError: JSONDecoder.__init__() got an unexpected keyword argument 'encoding'
完整的堆栈跟踪是:
TypeError Traceback(最近一次调用最后一次) 第 6 行 [11] 中的单元格 3 会话 = gps.gps(mode=gps.WATCH_ENABLE) 5 尝试: ----> 6 while 0 == session.read(): 7 如果没有(gps.MODE_SET & session.valid): 8 # 没用,可能不是 TPV 消息 9 继续
文件 ~/test_fft/venv/lib/python3.11/site-packages/gps/gps.py:285,在 gps.read(self) 中 283 返回状态 第284章 ”): --> 285 self.unpack(self.response) 第286章 287 self.valid |= PACKET_SET
文件 ~/test_fft/venv/lib/python3.11/site-packages/gps/client.py:199,在 gpsjson.unpack(self, buf) 中 197 “解压 JSON 字符串” 198 尝试: --> 199 self.data = dictwrapper(json.loads(buf.strip(), 编码=“ascii”)) 200 除了 ValueError 为 e: 201 引发 json_error(buf, e.args[0])
文件/usr/lib/python3.11/json/init.py:359,在负载中(s,cls,object_hook,parse_float,parse_int,parse_constant,object_pairs_hook,** kw) 第357章 358 千瓦['parse_constant'] = parse_constant --> 359 返回 cls(**kw).decode(s)
TypeError: JSONDecoder.init() 得到了意外的关键字参数“encoding”
这里还有一些其他旧答案: 在 Python 3 中使用 GPS 库 和 gpsd python 客户端
是否有用于 gpsd 的维护 python3 客户端,或者有人知道如何从 python 轮询 GPS 位置到 gpsd 吗?
一种适用于 python 3 的方法:
import gpsd
# Connect to the local gpsd
gpsd.connect()
# Get the current position
packet = gpsd.get_current()
# Get the latitude and longitude
latitude = packet.lat
longitude = packet.lon
print("Latitude:", latitude)
print("Longitude:", longitude)
这使用:https://github.com/MartijnBraam/gpsd-py3或者这里有一个维护的分叉版本:https://github.com/hatsunearu/py-gpsd2