raspberry pi python 中的 UART 未接收到数据
UART in python on raspberry pi doesn't receive data
我正在尝试在 raspberryPi 上实现 Python 3 中的数据发送(作为更大项目的一部分),当我连接 Rx 和 Tx 引脚时无法接收数据。无论使用 Python 2 还是 3(据我所知,this API 允许 Python 3 编程)我要么得到 Received: b'\n'
响应,要么得到这样的异常:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 471, in write
n = os.write(self.fd, d)
OSError: [Errno 5] Input/output error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./uart.py", line 12, in <module>
port.write(bytearray(input_data, 'utf-8'))
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 485, in write
raise SerialException('write failed: %s' % (v,))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error
不过我只能写缓冲 reader。
我制作的代码在这里:
#!/usr/bin/env python3
import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600)
while True:
input_data = input("Say sth: ")
if input_data != 'exit':
port.write(bytearray(input_data, 'utf-8'))
print('Sent: {0}'.format(bytearray(input_data, 'ASCII')))
output_data = port.readline()
print('Received: {0}\n'.format(str(output_data)))
else:
break
port.close()
我想使用 ASCII 编码,因为它将进一步连接到带有 C 代码的微控制器。我还检查了是否有任何数据写入缓冲区(确实如此),我已经尝试布局程序在发送数据后休眠一秒钟,我尝试使用 port.read(port.inWaiting())
和 port.read(in_waiting)
(在后一种情况下未找到属性)但似乎没有任何帮助。
我也试过 this 这个例子;我确定连接了正确的引脚,并且我已经使用 sudo apt-get update
和 sudo apt-get upgrade
更新和升级了我的 raspbian,当我输入 sudo apt-get install python3-serial
时,我被告知我已经有了最新版本安装。
我发布这个回答是为了结束这个话题,并帮助任何可能遇到类似困难的人。
由于处理器具有不同的体系结构,因此尝试使用 setserial 设置端口是 pointles,但这正是问题所在。
pi@raspberrypi ~ $ sudo setserial -g /dev/ttyAMA0
/dev/ttyAMA0, UART: undefined, Port: 0x0000, IRQ: 83
我找到的答案 here 解决了所有问题。
我正在尝试在 raspberryPi 上实现 Python 3 中的数据发送(作为更大项目的一部分),当我连接 Rx 和 Tx 引脚时无法接收数据。无论使用 Python 2 还是 3(据我所知,this API 允许 Python 3 编程)我要么得到 Received: b'\n'
响应,要么得到这样的异常:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 471, in write n = os.write(self.fd, d) OSError: [Errno 5] Input/output error
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "./uart.py", line 12, in <module> port.write(bytearray(input_data, 'utf-8')) File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 485, in write raise SerialException('write failed: %s' % (v,)) serial.serialutil.SerialException: write failed: [Errno 5] Input/output error
不过我只能写缓冲 reader。
我制作的代码在这里:
#!/usr/bin/env python3
import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600)
while True:
input_data = input("Say sth: ")
if input_data != 'exit':
port.write(bytearray(input_data, 'utf-8'))
print('Sent: {0}'.format(bytearray(input_data, 'ASCII')))
output_data = port.readline()
print('Received: {0}\n'.format(str(output_data)))
else:
break
port.close()
我想使用 ASCII 编码,因为它将进一步连接到带有 C 代码的微控制器。我还检查了是否有任何数据写入缓冲区(确实如此),我已经尝试布局程序在发送数据后休眠一秒钟,我尝试使用 port.read(port.inWaiting())
和 port.read(in_waiting)
(在后一种情况下未找到属性)但似乎没有任何帮助。
我也试过 this 这个例子;我确定连接了正确的引脚,并且我已经使用 sudo apt-get update
和 sudo apt-get upgrade
更新和升级了我的 raspbian,当我输入 sudo apt-get install python3-serial
时,我被告知我已经有了最新版本安装。
我发布这个回答是为了结束这个话题,并帮助任何可能遇到类似困难的人。
由于处理器具有不同的体系结构,因此尝试使用 setserial 设置端口是 pointles,但这正是问题所在。
pi@raspberrypi ~ $ sudo setserial -g /dev/ttyAMA0
/dev/ttyAMA0, UART: undefined, Port: 0x0000, IRQ: 83
我找到的答案 here 解决了所有问题。