python获取gps_Python GPS模块:读取最新的GPS D
上面的答案對于任何使用gpsd的現代版本并且只在特定時間需要數據的人來說都是非常低效和過于復雜的,而不是流媒體。
大多數gpse每秒至少發送一次位置信息。大概是因為許多基于GPS的應用程序都需要實時更新,所以我看到的絕大多數gpsd客戶機示例都使用上述方法從gpsd觀看流并接收實時更新(或多或少是GPS發送的)。
然而,如果(在OP的例子中)您不需要流式信息,而只需要在請求時(即通過用戶交互或其他事件)最后報告的位置,則有一種更高效和更簡單的方法:讓gpsd緩存最新的位置信息,并在需要時進行查詢。
gpsd JSON protocol有一個?POLL;請求,該請求返回gpsd看到的最新GPS信息。不必迭代gps消息的積壓,并不斷讀取新消息以避免緩沖區已滿,您可以在gpsd會話開始時發送?WATCH={"enable":true}消息,然后在需要時使用?POLL;查詢最新位置信息。響應是一個JSON對象,包含gpsd從GPS上看到的最新信息。
如果您使用的是Python3,那么我找到的最簡單的方法就是使用pypi上提供的gpsd-py3包。要連接到gpsd,請獲取最新的位置信息,然后打印當前位置:import gpsd
gpsd.connect()
packet = gpsd.get_current()
print(packet.position())
您可以在需要新位置信息時重復gpsd.get_current()調用,在后臺,gpsd包將執行對gpsd的?POLL;調用并返回表示響應的對象。
使用內置的gps模塊來實現這一點并不是非常簡單,但是有許多其他Python客戶機可用,而且對于任何可以執行套接字通信的對象都是非常簡單的,包括這個使用telnet的示例:$ telnet localhost 2947
Trying ::1...
Connected to localhost.
Escape character is '^]'.
{"class":"VERSION","release":"3.16","rev":"3.16","proto_major":3,"proto_minor":11}
?WATCH={"enable":true}
{"class":"DEVICES","devices":[{"class":"DEVICE","path":"/dev/pts/10","driver":"SiRF","activated":"2018-03-02T21:14:52.687Z","flags":1,"native":1,"bps":4800,"parity":"N","stopbits":1,"cycle":1.00}]}
{"class":"WATCH","enable":true,"json":false,"nmea":false,"raw":0,"scaled":false,"timing":false,"split24":false,"pps":false}
?POLL;
{"class":"POLL","time":"2018-03-02T21:14:54.873Z","active":1,"tpv":[{"class":"TPV","device":"/dev/pts/10","mode":3,"time":"2005-06-09T14:34:53.280Z","ept":0.005,"lat":46.498332203,"lon":7.567403907,"alt":1343.165,"epx":24.829,"epy":25.326,"epv":78.615,"track":10.3788,"speed":0.091,"climb":-0.085,"eps":50.65,"epc":157.23}],"gst":[{"class":"GST","device":"/dev/pts/10","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","device":"/dev/pts/10","time":"2005-06-09T14:34:53.280Z","xdop":1.66,"ydop":1.69,"vdop":3.42,"tdop":3.05,"hdop":2.40,"gdop":5.15,"pdop":4.16,"satellites":[{"PRN":23,"el":6,"az":84,"ss":0,"used":false},{"PRN":28,"el":7,"az":160,"ss":0,"used":false},{"PRN":8,"el":66,"az":189,"ss":45,"used":true},{"PRN":29,"el":13,"az":273,"ss":0,"used":false},{"PRN":10,"el":51,"az":304,"ss":29,"used":true},{"PRN":4,"el":15,"az":199,"ss":36,"used":true},{"PRN":2,"el":34,"az":241,"ss":41,"used":true},{"PRN":27,"el":71,"az":76,"ss":42,"used":true}]}]}
?POLL;
{"class":"POLL","time":"2018-03-02T21:14:58.856Z","active":1,"tpv":[{"class":"TPV","device":"/dev/pts/10","mode":3,"time":"2005-06-09T14:34:53.280Z","ept":0.005,"lat":46.498332203,"lon":7.567403907,"alt":1343.165,"epx":24.829,"epy":25.326,"epv":78.615,"track":10.3788,"speed":0.091,"climb":-0.085,"eps":50.65,"epc":157.23}],"gst":[{"class":"GST","device":"/dev/pts/10","time":"1970-01-01T00:00:00.000Z","rms":0.000,"major":0.000,"minor":0.000,"orient":0.000,"lat":0.000,"lon":0.000,"alt":0.000}],"sky":[{"class":"SKY","device":"/dev/pts/10","time":"2005-06-09T14:34:53.280Z","xdop":1.66,"ydop":1.69,"vdop":3.42,"tdop":3.05,"hdop":2.40,"gdop":5.15,"pdop":4.16,"satellites":[{"PRN":23,"el":6,"az":84,"ss":0,"used":false},{"PRN":28,"el":7,"az":160,"ss":0,"used":false},{"PRN":8,"el":66,"az":189,"ss":45,"used":true},{"PRN":29,"el":13,"az":273,"ss":0,"used":false},{"PRN":10,"el":51,"az":304,"ss":29,"used":true},{"PRN":4,"el":15,"az":199,"ss":36,"used":true},{"PRN":2,"el":34,"az":241,"ss":41,"used":true},{"PRN":27,"el":71,"az":76,"ss":42,"used":true}]}]}
總結
以上是生活随笔為你收集整理的python获取gps_Python GPS模块:读取最新的GPS D的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 语言注释,HTML 注释
- 下一篇: 华盛顿市的布局,也属于风水