使用 gpsd 或 cgps 返回纬度和经度然后退出

bou*_*uby 2 unix gps gpsd

我想要一种非常简单的方法来从 unix 命令行从 USB 加密狗查询我的 gps 位置。

现在,我知道我已经有了一个正常运行的软件和硬件系统,cgps命令成功地向我展示了我的位置就证明了这一点。我现在希望能够从命令行对我的 gps 位置(纬度,十进制长)发出简短请求。我的 USB 串口的路径是/dev/ttyUSB0,我正在使用一个输出通用 NMEA 语句的 Global Sat 加密狗

我怎样才能做到这一点?

谢谢

Nod*_*dak 5

telnet 127.0.0.1 2947

?WATCH={"enable":true}

?POLL;

给你你的答案,但你仍然需要把小麦和谷壳分开。它还假设 GPS 不是从冷启动进入的。

可以调用一个简短的脚本,例如;

#!/bin/bash
exec 2>/dev/null
# get positions
gpstmp=/tmp/gps.data
gpspipe -w -n 40 >$gpstmp"1"&
ppid=$!
sleep 10
kill -9 $ppid
cat $gpstmp"1"|grep -om1 "[-]\?[[:digit:]]\{1,3\}\.[[:digit:]]\{9\}" >$gpstmp
size=$(stat -c%s $gpstmp)
if [ $size -gt 10 ]; then
   cat $gpstmp|sed -n -e 1p >/tmp/gps.lat
   cat $gpstmp|sed -n -e 2p >/tmp/gps.lon
fi
rm $gpstmp $gpstmp"1"  
Run Code Online (Sandbox Code Playgroud)

这将导致输出 40 个句子,然后greplat/lon 到临时文件,然后清理。

或者,从GPS3 github 存储库将 alphagps3.py放在与以下 Python2.7-3.4 脚本相同的目录中并执行。

from time import sleep
import gps3

the_connection = gps3.GPSDSocket()
the_fix = gps3.DataStream()

try:
    for new_data in the_connection:
        if new_data:
            the_fix.refresh(new_data)
        if not isinstance(the_fix.TPV['lat'], str):  # check for valid data
            speed = the_fix.TPV['speed']
            latitude = the_fix.TPV['lat']
            longitude = the_fix.TPV['lon']
            altitude = the_fix.TPV['alt']
            print('Latitude:', latitude, 'Longitude:', longitude)
            sleep(1)
except KeyboardInterrupt:
    the_connection.close()
    print("\nTerminated by user\nGood Bye.\n")
Run Code Online (Sandbox Code Playgroud)

如果您还希望它在一次迭代后关闭import sys,然后替换sleep(1)sys.exit()