【发布时间】:2017-04-18 12:28:51
【问题描述】:
我正在尝试在 Raspberry Pi3 和具有 RS-232 连接的数字传感器之间建立通信。我买了一顶帽子,上面也有一个 RS-232 端口,可以连接到 Pi 上的 rx 和 tx 引脚。 Pi 安装了 raspbian,我正在使用 Python 3 编写代码。
在我阅读的教程中,我必须先配置 Pi 以允许串行通信。我已经完成了以下步骤:
第 1 步:
sudo raspi-config -> Interfacing Options -> Serial -> [login shell accesible over serial? -> no] -> [serial port hardware enabled? -> yes]
第 2 步:
sudo nano /boot/cmdline.txt
换行为:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
第 3 步:
sudo nano /boot/config.txt
添加行:
dtoverlay=pi3-miniuart-bt
enable_uart=1
force_turbo=1
第 4 步:
sudo apt-get install python-serial
在这个设置之后,我用 Python 3 编写了一个非常短的程序来测试它:
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write(bytes(5, 'UTF8'))
数字 5 告诉传感器更改显示单位。代码运行时没有错误消息,但我没有看到传感器的单位发生变化。如果我使用串口转 USB 转换器将 RS232 连接到我的笔记本电脑并使用 Putty 监控串口,我也看不到任何发送。
如果有人能告诉我问题出在哪里,那就太好了。但我也很乐意为我自己找到解决方案提供任何意见。
提前非常感谢, 弗洛里安
【问题讨论】:
标签: python-3.x serial-port raspberry-pi3