【问题标题】:Unable to read Raspberry Pi UART Pins无法读取 Raspberry Pi UART 引脚
【发布时间】:2020-04-17 20:29:38
【问题描述】:

我无法从 Raspberry Pi UART 引脚读取数据。这是我使用的 Python 代码:

import serial
import time
import string
import pynmea2

while True:
    port="/dev/ttyAMA0"
    ser=serial.Serial(port, baudrate=115200, timeout=0.5)
    dataout = pynmea2.NMEAStreamReader()
    newdata=ser.readline()

    if newdata[0:6] == "$GPRMC":
        newmsg=pynmea2.parse(newdata)
        lat=newmsg.latitude
        lng=newmsg.longitude
        gps = "Latitude=" + str(lat) + "and Longitude=" + str(lng)
        print(gps)

当我运行代码时,我得到了这个错误:

Traceback (most recent call last): File"/usr/lib/python3/distpackages/serial/serialposix.py", line 265, in openself.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) PermissionError: [Errno 13] Permission denied:'/dev/ttyAMA0' During handling of the above exception, another exception occurred: 
Traceback (most recent call last):   File "/home/pi/Desktop/sa.py", line 8, in <module> ser=serial.Serial(port, baudrate=115200, timeout=0.5)   File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 240, in__init__
    self.open()   File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 268, in open raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg)) serial.serialutil.SerialException: [Errno13] could not open port /dev/ttyAMA0: [Errno 13] Permission denied:'/dev/ttyAMA0'

【问题讨论】:

  • 请修正回溯的格式。将其发布为代码而不是引用。
  • 但是错误信息似乎很清楚:您没有UART设备的权限。
  • 要么更改权限,要么以root身份运行代码。
  • 感谢您的帮助,但即使我以管理员身份使用 python 或 thonny 运行它,它也没有响应

标签: python raspberry-pi gps raspberry-pi3


【解决方案1】:

您应该使用 sudo 运行它。您当前的用户没有打开 com 端口的权限。

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2019-07-06
    相关资源
    最近更新 更多