关闭串口_Raspberry Pi Zero W:串口(UART)的配置和使用

Raspberry Pi Zero W:串口(UART)的配置和使用

开启UART

据官方所言(https://www.raspberrypi.org/documentation/configuration/uart.md):树莓派CPU内部有两个串口,一个PL001 UART和一个Mini UART。Mini UART没有时钟源,必须由内核提供时钟源,而内核频率本身是变化的,导致Mini UART速率不稳,无法正常使用。RPI ZERO W与以往老版本RPI不同,没有将PL011 UART分配GPIO中的UART(GPIO14和GPIO15),而是将其分配给了蓝牙模块。我们需要关闭蓝牙对PL011 UART的使用,恢复GPIO串口对PL011 UART的使用。

127956111e005fda64899ac0b790a9cb.png

查看映射关系:

$ sudo ls -l /dev

serial0是GPIO中的UART,ttyAMA0是PL001 UART。可以看到并没有serial0文件

0e2764cc27bd357250b402b745dc161f.png

修改cmdline.txt文件

编辑cmdline.txt文件

$ sudo nano /boot/cmdline.txt

原来cmdline.txt文件的内容如下:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=a05c3c8f-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

将有关console的内容全部删掉,修改后的cmdline.txt文件内容如下:

dwc_otg.lpm_enable=0 root=PARTUUID=a05c3c8f-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

(2)关闭板载蓝牙

禁用蓝牙功能

$ sudo systemctl disable hciuart

编辑config.txt文件

$ sudo nano /boot/config.txt

增加如下一行:

dtoverlay=pi3-disable-bt

重启树莓派

$ sudo shutdown -r now

再次查看映射关系,发现serial0成功映射到ttyAMA0

d6b0488fdfc8151eedf8ae9be5979958.png

(3)禁用串口的控制台功能

编辑config.txt文件,增加如下一行:

enable_uart=1

也可以使用raspi-config自动操作:

sudo raspi-config

选择第五项

f9514e221038d6826b76b619b041f2ee.png
ead79947ef53c600c0f794b7cc18bfa5.png
d66da695830bc42971f108a4d7371f00.png

开启硬件串口

(4)重启树莓派

$ sudo shutdown -r now

4.2 测试

(1)安装并导入serial模块

$ sudo pip install pyserial$ python >>> import serial

如图所示模块导入成功:

1954fc09e48a65e75fc1a68d64b5e551.png

开启串口

>>> ser = serial.Serial('/dev/ttyAMA0',19200)

使用串口助手发送“hello”

88370d10101b0251140725e2d278bcbb.png

成功读取到串口助手发来的数据

# 读取5个字符

>>> ser.read(5)

# 关闭串口

>>> ser.close()
74c114f2c023b9418be37d11f2f80d01.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值