简体   繁体   English

无法读取 Raspberry Pi UART 引脚

[英]Unable to read Raspberry Pi UART Pins

I'm unable to read data from Raspberry Pi UART pins.我无法从 Raspberry Pi UART 引脚读取数据。 This is the Python code I used:这是我使用的 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)

When I run the code, I get this error:当我运行代码时,我收到此错误:

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'

You should run it using sudo .您应该使用sudo运行它。 Your current user does not have permission to open the com port.您当前的用户无权打开 com 端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM