首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何捕获和管理GPS模块的异常?

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

Stack Overflow用户
提问于 2016-01-15 21:18:17
回答 2查看 261关注 0票数 1

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

代码语言:javascript
运行
复制
##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)

但是,它会时不时地给出这个错误,因为它找不到我的位置:

代码语言:javascript
运行
复制
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'.

有这样的错误也没关系。我的问题是,一旦发生这种情况,程序就会停止运行。有没有一种方法可以捕捉到这个错误,并让程序循环返回并重试,即使它遇到了这个错误?

更新

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2016-01-15 21:33:10

我同意Oisin的回答,但我建议将try-except子句放在可能发生的地方,并使用continue语句传递while-loop的其余部分,这样看起来就像

代码语言:javascript
运行
复制
##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)
票数 2
EN

Stack Overflow用户

发布于 2016-01-15 21:24:01

这应该可以工作,但它可能会陷入无限递归循环。

代码语言:javascript
运行
复制
##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps

if __name__ == '__main__':
    getPos()

def getPos():
    try:
        while True:
            gps = microstacknode.hardware.gps.l80gps.L80GPS()
            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)
    except microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError:
        getPos()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34811972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档