前面所有关于连接到作为客户端的gpsd守护进程的python代码的答案要么引用了非常旧的库,要么不起作用.
我在这里的官方website上try 了这个示例代码--这也不起作用.
代码是:
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)
但当我try 此代码时,错误是:TypeError: JSONDecoder.__init__() got an unexpected keyword argument 'encoding'
完整堆栈跟踪为:
TypeError TraceBack(最近一次调用) 第6行[11]中的单元格 3会话=gps.gps(模式=gps.WATCH_ENABLE) 5try : ->;6 When 0==会话.Read(): 7如果不是(gps.MODE_SET&;ession.valid): 8#没有用处,可能不是TPV消息 9继续
文件~/test_fft/venv/lib/python3.11/site-packages/gps/gps.py:285,在gps.read(self)中 283 返回状态 284如果self.response.startswith("{")和self.response.endswith("}\r\n"): --285 self.unpack(self.response) 286 self .oldstyle_shim() 287 self.valid |= PACKET_SET
Gpsjson.unpack中的文件~/test_fft/venv/lib/python3.11/site-packages/gps/client.py:199,(自身,buf) 197"解压JSON字符串" 198try : -->;199 self.data=dictwRapper(json.ads(buf.strie(),Coding="ascii")) 200除ValueError外,e: 201提高json_error(buf,e.args[0])
File /usr/lib/python3.11/json/init.py:359,in loads(s,cls,object_hook,parse_float,parse_int,parse_constant,object_pairs_hook,**kw) 357如果parse_constant不是None: 358 kw = parse_constant --359 return cls(**kw).decode(s)
TypeError:JSONDecoder.init()获得意外的关键字参数""Coding""
这里还有一些其他的老答案: Using GPS library in Python 3和 gpsd python client
有没有一个python3客户端可以维护gpsd,或者有没有人知道一种从python向gpsd轮询gps位置的方法?