0
点赞
收藏
分享

微信扫一扫

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯


文章目录

  • ​​设备连接​​
  • ​​应用下载​​
  • ​​配置过程​​
  • ​​note​​

设备连接

USB 串口交叉连线即可。
Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_树莓派

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_树莓派_02

应用下载

Win10 应用商店,串口调试助手下载:
Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_串口_03
界面如下,当串口连接后,切记打开串口
Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_串口_04

配置过程

  1. ssh 连接到树莓派
  2. ​sudo raspi-config​​​Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_树莓派_05
    Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_06

选择关闭串口登录功能,打开硬件串口调试功能,后者设置与否无所谓,本次实验采用软件串口进行,重点是关闭串口登录功能,因为涉及到串口的占用问题。

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_07

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_串口_08
重点是下图:

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_09
默认将​​​cmdline.txt​​​ 修改保存到 ​​/boot/​​​, 但是树莓派4B的启动文件在 ​​/boot/firmware​​​ 下,故:将 ​​/boot/firmware​​​ 下的 ​​cmdline.txt​​​ 先保存到 ​​/boot/​​ 下,再经过​raspi-config​​​ 配置完毕后,将​​/boot/​​​ 中修改后的 ​​cmdline.txt​​​ 文件,拷贝到 ​​/boot/firmware​​ 下即可。

最终效果如下,等到系统启动一半之后,串口调试工具中显示的输出就会终止,因为我们关闭了串口调试功能

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_树莓派_10
3. 配置完毕,构造程序如下:

Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_11
使用方式:

  1. 发送数据
    ​​​sudo python3 serial_send.py 123​​​Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_12
  2. 接收数据
    Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_ubuntu_13
    循环等待数据。
    Python3 + 树莓派4B + ubuntu server ttyS0 实现串口通讯_树莓派_14

note

树莓派有两个串口可以使用,一个是硬件串口(/dev/ttyAMA0),另一个是mini串口(/dev/ttyS0)。硬件串口有单独的波特率时钟源,性能好,稳定性强;mini串口功能简单,稳定性较差,波特率由CPU内核时钟提供,受内核时钟影响。
树莓派(3/4代)板载蓝牙模块,默认的硬件串口是分配给蓝牙模块使用的,而性能较差的mini串口是分配给GPIO串口 TXD0、RXD0。

ubuntu@ubuntu:~/Documents$ cat /boot/firmware/cmdline.txt # 配置过后的内容# 原本内容
net.ifnames=0 dwc_otg.lpm_enable=0 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc
ubuntu@ubuntu:~/Documents$ cat /boot/firmware/cmdline_backup.txt # 原本内容
net.ifnames=0 dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc


举报

相关推荐

0 条评论