python - 如何捕获和管理GPS模块的异常?

标签 python gps raspberry-pi

我有一个使用 GPS 模块的树莓派。为了使用该模块,我运行如下代码:

##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps

if __name__ == '__main__':
    gps = microstacknode.hardware.gps.l80gps.L80GPS()
    while True:
        data = gps.get_gpgga()
        List = [list(data.values())[x] for x in [7, 9, 12]]
        string=str(List)
        string = string[1:-1]
        text_file = open("/home/pi/fyp/gps.txt","a")
        text_file.write(string + "\n")
        time.sleep(1)

但是,它时不时会出现此错误,因为它找不到我的位置:

Traceback (most recent call last):
  File "gps.py", line 8, in <module>
    data = gps.get_gpgga()
  File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 119, in get_gpgga
    pkt = self.get_nmea_pkt('GPGGA')
  File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 293, in get_nmea_pkt
    "Timed out before valid '{}'.".format(pattern))
microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError: Timed out before valid 'GPGGA'.

出现这个错误没关系。我遇到的问题是,如果发生这种情况,程序就会停止运行。有没有办法捕获该错误并使程序循环返回并重试,即使遇到此错误也是如此?

更新

if I try Stefan_Reinhardt's method, I would get the following error instead:

Traceback (most recent call last):
  File "gps.py", line 9, in <module>
    data = gps.get_gpgga()
  File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 119, in get_gpgga
    pkt = self.get_nmea_pkt('GPGGA')
  File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 293, in get_nmea_pkt
    "Timed out before valid '{}'.".format(pattern))
microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError: Timed out before valid 'GPGGA'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "gps.py", line 10, in <module>
    except NMEAPacketNotFoundError:
NameError: name 'NMEAPacketNotFoundError' is not defined

最佳答案

我同意Oisin的回答, 但我建议将 try-except 子句仅放在可能发生的行周围,并使用 continue 语句传递 while 循环的其余部分,这样它看起来像

##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps

if __name__ == '__main__':
    gps = microstacknode.hardware.gps.l80gps.L80GPS()
    while True:
        try:
            data = gps.get_gpgga()
        except NMEAPacketNotFoundError:
            continue
        List = [list(data.values())[x] for x in [7, 9, 12]]
        string=str(List)
        string = string[1:-1]
        text_file = open("/home/pi/fyp/gps.txt","a")
        text_file.write(string + "\n")
        time.sleep(1)

关于python - 如何捕获和管理GPS模块的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34811972/

相关文章:

python - Django 使用小部件创建一个只读的表单字段

qiskit - Qiskit 的 RepetitionCode 类现在在哪里?

Python模拟504网关超时错误

javascript - 如何在 Javascript 中查找/创建 GPS 点的凸包

javascript - pm2 自动启动 Discord.js 机器人在重新启动时不会再次连接(树莓派)

Python:没有名为 suds 的模块

c++ - 我可以通过使用 Wt 作为我的 Web 框架来完全隐藏 Web 应用程序中的 "behind"C 代码吗?

android - 如何在 Ionic 2 上设置要通过 GPS 验证的位置?

javascript - 创建连续执行的代码

c# - UWP IoT 扩展中的唯一设备 ID