nextion树莓派3b串口详解与使用。

树莓派包含两个串口

1.硬件串口(/dev/ttyAMA0),硬件串口由硬件实现,有单独的波特率时钟源,性能高,可靠。一般优先选择这个使用。

2.mini串口(/dev/ttyS0),mini串口时钟源是由CPU内核时钟提供,波特率受到内核时钟的影响,不稳定。

想要通过树莓派的GPIO引脚进行稳定的串口通信,需要修改串口的映射关系。

serial0是GPIO引脚对应的串口,serial1是蓝牙对应的串口,可以使用 ls -l /dev/serial*  查看当前的映射关系
1.sudo raspi-config

Interfacing Options -> P6 Serial, 第一个选项(would you like a login shell to be accessible  over serial?)选择NO,第二个选项(would you like the serial port hardware to be enabled?)选择 YES

然后我们可以通过使用ls -l /dev/serial* 查看串口映射关系发现

serial0(GPIO串口)默认使用的是ttyS0(mini串口),serial1(蓝牙)使用的是ttyAMA0(硬件串口)

如果想使用稳定可靠的硬件串口,就要将树莓派3的硬件串口与mini串口默认映射对换。而这个需求官方也考虑到了,在系统中放了一个实现这个功能的文件---stretch版本系统中的文件为/boot/overlays/pi3-miniuart-bt.dtbo。使用该文件发挥功能只需在/boot/config.txt文件末尾添加一行代码
sudo nano /boot/config.txt

在文件最后面添加一行:dtoverlay=pi3-miniuart-bt

修改完成后 sudo reboot 重启树莓派后使用 ls -l /dev/serial* 查看映射如下:

如下图所示,serial0(GPIO串口)使用的是ttyAMA0(硬件串口),而serial1(蓝牙)使用的是ttys0(mini串口)
 

在树莓派系统的 /boot/overlays/ 目录下,提供了一个 pi3-miniuart-bt.dtbo 文件。其文件作用可以在 /boot/overlays/README 文件中进行查看,找到下面这一段(要查看可以cat /boot/overlays/README 查看得了),给出了解决的办法,简单的理解:將 Pi 3 Bluetooth 移到 mini-UART (ttyS0),bluetooth用mini-UART (ttyS0)串口。

Name:   pi3-miniuart-bt
Info:   Switch Pi3 Bluetooth function to use the mini-UART (ttyS0) and restore
        UART0/ttyAMA0 over GPIOs 14 & 15. Note that this may reduce the maximum
        usable baudrate.
        N.B. It is also necessary to edit /lib/systemd/system/hciuart.service
        and replace ttyAMA0 with ttyS0, unless you have a system with udev rules
        that create /dev/serial0 and /dev/serial1, in which case use
        /dev/serial1 instead because it will always be correct. Furthermore,
        you must also set core_freq=250 in config.txt or the miniuart will not
        work.
Load:   dtoverlay=pi3-miniuart-bt
Params: <None>
复制代码
  • 编辑/boot/config.txt,在文末添加以下内容
# 确保串口的功能激活
enable_uart=1
# 通过上面给出的方式配置
dtoverlay=pi3-miniuart-bt

有文章指出要修改/lib/systemd/system/hciuart.server,(估计是旧版本的rasbian系统)

编辑/lib/systemd/system/hciuart.server(蓝牙服务) 将 “ttyAMA0(硬件串口)”修改为“ttyS0(mini串口)”

sudo nano /lib/systemd/system/hciuart.service

将 “ttyAMA0(硬件串口)”修改为“ttyS0(mini串口)”

QQ图片20160310003124

保存退出

新版rasbian系统 编辑/lib/systemd/system/hciuart.server,

已使用dev-serial1(蓝牙)替代了。所以无需修改hciuart.server这个文件了。

 

猜你喜欢

转载自blog.csdn.net/hu5566798/article/details/105267373
今日推荐