景区外文网站建设,成都建设网站哪家好,苏州建设网站首页,秦皇岛建设部网站前言
当我们要使用串口进行zigbee的短距离通信时,发现无法使用串口. 原因
树莓派3bCPU内部有两个串口,一个硬件串口(就是我们平时使用的UART),还有一个迷你串口(mini-uart),在老版本的树莓派中把硬件串口分配在GPIO上,可以单独使用.但是在新的树莓派中官方把硬件串口给了蓝牙…前言
当我们要使用串口进行zigbee的短距离通信时,发现无法使用串口. 原因
树莓派3bCPU内部有两个串口,一个硬件串口(就是我们平时使用的UART),还有一个迷你串口(mini-uart),在老版本的树莓派中把硬件串口分配在GPIO上,可以单独使用.但是在新的树莓派中官方把硬件串口给了蓝牙模块上,而将一个没有时钟源,必须使用内核提供时钟参考源的mini串口,分发给GPIO的串口,这样以来由于内核的频率本身是在一直变换的,导致mini串口的速率不稳定,出现不能使用的情况.(我这边甚至没有显示出来(就是没有/dev/serial0 -ttyS0)),工程师也知道这个bug了就没直接启动mini串口,只准我们用蓝牙.
解决只有serial1 - ttyAMA0的情况
这是没打开GPIO串口的情况只有serial1蓝牙使用的是ttyAMA0(硬件串口) 这里我试了很多方法,大体分为两种: 1,sudo nano /boot/config.txt 去里面打开写enable_uart1,或者其他这就要看(cd /boot/overlays/#提供了README)这个文件的README了. 下面就是README的全部有关UART的解释 krnbt_baudrate Set the baudrate of the PL011 UART when usedwith krnbtonuart0 Set to off to disable uart0 (default on)uart1 Set to on or off to enable or disable uart1(default varies)
Name: disable-bt
Info: Disable onboard Bluetooth on Pi 3B, 3B, 3A, 4B and Zero W, restoringUART0/ttyAMA0 over GPIOs 14 15.N.B. To disable the systemd service that initialises the modem so itdoesnt use the UART, use sudo systemctl disable hciuart.
Load: dtoverlaydisable-bt
Params: NoneName: dpi18
Info: Overlay for a generic 18-bit DPI displayThis uses GPIOs 0-21 (so no I2C, uart etc.), and activates the output2-3 seconds after the kernel has started.
Load: dtoverlaydpi18
Params: NoneName: dpi18cpadhi
Info: Overlay for a generic 18-bit DPI display (in mode 6 connection scheme)This uses GPIOs 0-9,12-17,20-25 (so no I2C, uart etc.), and activatesthe output 3-3 seconds after the kernel has started.
Load: dtoverlaydpi18cpadhi
Params: NoneName: dpi24
Info: Overlay for a generic 24-bit DPI displayThis uses GPIOs 0-27 (so no I2C, uart etc.), and activates the output2-3 seconds after the kernel has started.
Load: dtoverlaydpi24
Params: NoneName: midi-uart0
Info: Configures UART0 (ttyAMA0) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart0
Params: NoneName: midi-uart1
Info: Configures UART1 (ttyS0) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart1
Params: NoneName: midi-uart2
Info: Configures UART2 (ttyAMA1) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart2
Params: NoneName: midi-uart3
Info: Configures UART3 (ttyAMA2) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart3
Params: NoneName: midi-uart4
Info: Configures UART4 (ttyAMA3) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart4
Params: NoneName: midi-uart5
Info: Configures UART5 (ttyAMA4) so that a requested 38.4kbaud actually gets31.25kbaud, the frequency required for MIDI
Load: dtoverlaymidi-uart5
Params: NoneName: miniuart-bt
Info: Switch the onboard Bluetooth function on Pi 3B, 3B, 3A, 4B and Zero Wto 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.serviceand replace ttyAMA0 with ttyS0, unless using Raspbian or anotherdistribution with udev rules that create /dev/serial0 and /dev/serial1,in which case use /dev/serial1 instead because it will always becorrect. Furthermore, you must also set core_freq and core_freq_min tothe same value in config.txt or the miniuart will not work.
Load: dtoverlayminiuart-bt,paramval
Params: krnbt Set to on to enable autoprobing of Bluetoothdriver without need of hciattach/btattachName: pi3-miniuart-bt
Info: This overlay has been renamed miniuart-bt, keeping pi3-miniuart-bt asan alias for backwards compatibility.
Load: DeprecatedName: qca7000-uart0
Info: in-techs Evaluation Board for PLC Stamp micro (UART)This uses uart0/ttyAMA0 over GPIOs 14 15 to connect the QCA7000.But it requires disabling of onboard Bluetooth onPi 3B, 3B, 3A, 4B and Zero W.
Load: dtoverlayqca7000-uart0,paramval
Params: baudrate Set the baudrate for the UART (default115200)Name: sc16is750-i2c
Info: Overlay for the NXP SC16IS750 UART with I2C InterfaceEnables the chip on I2C1 at 0x48 (or the addr parameter value). Toselect another address, please refer to table 10 in reference manual.
Load: dtoverlaysc16is750-i2c,paramval
Params: int_pin GPIO used for IRQ (default 24)addr Address (default 0x48)xtal On-board crystal frequency (default 14745600)Name: sc16is752-i2c
Info: Overlay for the NXP SC16IS752 dual UART with I2C InterfaceEnables the chip on I2C1 at 0x48 (or the addr parameter value). Toselect another address, please refer to table 10 in reference manual.
Load: dtoverlaysc16is752-i2c,paramval
Params: int_pin GPIO used for IRQ (default 24)addr Address (default 0x48)xtal On-board crystal frequency (default 14745600)Name: sc16is752-spi0
Info: Overlay for the NXP SC16IS752 Dual UART with SPI InterfaceEnables the chip on SPI0.
Load: dtoverlaysc16is752-spi0,paramval
Params: int_pin GPIO used for IRQ (default 24)xtal On-board crystal frequency (default 14745600)Name: sc16is752-spi1
Info: Overlay for the NXP SC16IS752 Dual UART with SPI InterfaceEnables the chip on SPI1.N.B.: spi1 is only accessible on devices with a 40pin header, eg:A, B, Zero and PI2 B; as well as the Compute Module.Name: uart0
Info: Change the pin usage of uart0
Load: dtoverlayuart0,paramval
Params: txd0_pin GPIO pin for TXD0 (14, 32 or 36 - default 14)rxd0_pin GPIO pin for RXD0 (15, 33 or 37 - default 15)pin_func Alternative pin function - 4(Alt0) for 1415,7(Alt3) for 3233, 6(Alt2) for 3637Name: uart1
Info: Change the pin usage of uart1
Load: dtoverlayuart1,paramval
Params: txd1_pin GPIO pin for TXD1 (14, 32 or 40 - default 14)rxd1_pin GPIO pin for RXD1 (15, 33 or 41 - default 15)Name: uart2
Info: Enable uart 2 on GPIOs 0-3. BCM2711 only.
Load: dtoverlayuart2,param
Params: ctsrts Enable CTS/RTS on GPIOs 2-3 (default off)Name: uart3
Info: Enable uart 3 on GPIOs 4-7. BCM2711 only.
Load: dtoverlayuart3,param
Params: ctsrts Enable CTS/RTS on GPIOs 6-7 (default off)Name: uart4
Info: Enable uart 4 on GPIOs 8-11. BCM2711 only.
Load: dtoverlayuart4,param
Params: ctsrts Enable CTS/RTS on GPIOs 10-11 (default off)Name: uart5
Info: Enable uart 5 on GPIOs 12-15. BCM2711 only.
Load: dtoverlayuart5,param
Params: ctsrts Enable CTS/RTS on GPIOs 14-15 (default off) 2,以下为第二种方法:
sudo raspi-config
找到Interfacing选项找到serial。 第一个问题是would you like a login shell to be accessible over serial? 选否。 第二个问题是would you like the serial port hardware to be enabled?选是。 我用的系统版本是2018-11-13-raspbian-stretch你的问题顺序可能不一样看清对应问题选择。
补充
/dev/serial0(对应的shell串口,有引脚引出) -ttyS0(mini串口,用的是内核时钟不稳定,实际测试关闭shell之后并没有出现过异常情况) /dev/serial1(对应的蓝牙,没有引脚引出) -ttyAMA0(硬件串口稳定) cd /boot/overlays/#提供了README Name: disable-bt
Info: Disable onboard Bluetooth on Pi 3B, 3B, 3A, 4B and Zero W, restoringUART0/ttyAMA0 over GPIOs 14 15.N.B. To disable the systemd service that initialises the modem so itdoesnt use the UART, use sudo systemctl disable hciuart.
Load: dtoverlaydisable-bt
Params: None
README文件中说明了这个文件的功能是将树莓派3的蓝牙切换到mini串口ttyS0并且恢复硬件串口ttyAMA0到GPIO 1415脚中。并且给出了载入的方法。 ls -l /dev 此时GPIO映射的串口是默认的/dev/ttyS0这个mini串口。
使用下面这条指令编辑 /boot/config.txt 文件
sudo nano /boot/config.txt在该文件中增加一行代码
dtoverlaydisable-bt
然后保存文件重启树莓派使之生效。 再通过 ls -l /dev 命令查看修改后的映射关系 对比修改前的关系可以看出serial0和serial1 与 ttyAMA0和ttyS0的映射关系对换完成了也就是ttyAMA0映射到了引出的GPIO Tx Rx上。
禁用串口的控制台功能
前面的步骤已经交换了硬件串口与mini串口的映射关系但现在想使用树莓派外接串口模块进行通信还不行因为树莓派IO引出的串口默认是用来做控制台使用的它的初衷是为了在没有网络接口时通过串口对树莓派进行相关的配置。因此需要禁用这个默认功能使得串口为我们自由使用。
在树莓派命令窗口中分别通过如下两个命令停止和禁用串口的控制台功能
sudo systemctl stop serial-gettyttyAMA0.service
sudo systemctl disable serial-gettyttyAMA0.service由于我们前面已经交换了串口的映射关系因此这里注意是ttyAMA0。 然后通过下列指令编辑cmdline.txt文件
sudo nano /boot/cmdline.txt然后看到里面类似如下的内容
dwc_otg.lpm_enable0 consoleserial0,115200 consoletty1 root/dev/mmcblk0p2 rootfstypeext4 elevatordeadline fsck.repairyes rootwait
把consoleserial0,115200删掉 consoleserial0,115200 剩下的内容类似如下
dwc_otg.lpm_enable0 consoletty1 root/dev/mmcblk0p2 rootfstypeext4 elevatordeadline rootwait 然后重新启动树莓派使修改生效
编写一个简单的串口程序
新建一个test.py文件
# -*- coding: utf-8 -*
import serial
import time
# 打开串口
ser serial.Serial(/dev/ttyAMA0, 9600)
def main():while True:# 获得接收缓冲区字符count ser.inWaiting()if count ! 0:# 读取内容并回显recv ser.read(count)print(recv)ser.write(recv)# 清空接收缓冲区ser.flushInput()# 必要的软件延时time.sleep(0.1)if __name__ __main__:try:main()except KeyboardInterrupt:if ser ! None:ser.close()
小插曲
#这里switch_function(reading)返回值是数字,但是ser.write()填入的值是数字则意思是发送几个字节
#所以我们把这里整成字符串,格式是utf-8.
count str(switch_function(reading))if count ! 0:recv count.encode(utf-8)ser.write(recv)
#python中的多线程def Tmer_task(processed_frame):print(收到识别指令正在处理...)image_base64 image_to_base64(processed_frame)if image_base64:print(正在识别垃圾类型请稍候...)reading analyze_meter_image(image_base64)if reading:print( * 30)print(f识别结果: {reading})print( * 30)count str(switch_function(reading))if count ! 0:recv count.encode(utf-8)ser.write(recv)print(已发送指令请等待垃圾处理完成)else:print(识别失败请重试)def periodic_task(cap, interval):while True:ret, frame cap.read()if not ret:print(无法获取摄像头画面)break# 处理图像现在返回彩色图像processed_frame img_p(frame)# 显示实时画面#cv2.imshow(Camera Feed, processed_frame)# 调用任务处理函数Tmer_task(processed_frame)# 等待间隔时间time.sleep(interval)ask_thread threading.Thread(targetperiodic_task, args(cap, 10))task_thread.start()