Tangwx

Tangwx

博客网站

ラズベリーパイの環境設定

Raspberry Pi 環境設定#

燒錄映像#

image-20230217202807072

燒錄完成後,開機。

開啟 VNC#

Preferences(首選項)-> Raspberry Pi Configuration(樹莓派設定)
選擇 Interfaces(介面)-> 開啟 VNC 的按鈕

image-20230217203141423

image-20230217203229798

開啟後,使用 VNC 工具連接到樹莓派,顯示虛擬桌面。

image-20230217203058361

開啟樹莓派硬體串口#

開啟 GPIO 串口功能,並使用硬體串口。
使用 sudo raspi-config 進入圖形界面。
選擇菜單 Interfacing Options -> 6 Serial

image-20230217204854148

image-20230217204928814

第一個選項(would you like a login shell to be accessible over serial?)選擇 NO
第二個選項(would you like the serial port hardware to be enabled?)選擇 YES
保存後重啟,查看映射關係 serial0 是 GPIO 引腳對應的串口,serial1 是藍牙對應的串口,預設未啟用 serial0。使用 ls -l /dev/serial* 查看當前的映射關係:

image-20230217205312764

比之前多了一個 gpio 的串口 serial0,並且使用的是 ttyS0。這裡已經是開啟了 GPIO 串口功能,但是使用的是 CPU 實現的軟體串口。

如果想使用穩定可靠的硬體串口,就要將樹莓派 3b+ 的硬體串口與 mini 串口預設映射對換(先禁用藍牙 sudo systemctl disable hciuart)。

/boot/config.txt 文件末尾添加一行代碼 dtoverlay=disable-bt,在命令行輸入

sudo systemctl disable hciuart

保存後重啟再查看設備對應關係 ls -l /dev/serial*,發現已經調換成功。看到 serial0 -> ttyAMA0 就是配置成功了。

image-20230217210932567

串口接收測試#

接收數據(回顯功能)

# -*- 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)
            ser.write(recv)
        # 清空接收緩衝區
        ser.flushInput()
        # 必要的軟體延遲
        time.sleep(0.1)
    
if __name__ == '__main__':
#如果本文件作為腳本運行
    try:
        main()
    except KeyboardInterrupt:
    #異常處理
        if ser != None:
            ser.close()

如果 import serial 报错,安装 python3-serial 模块

sudo apt-get install python3-serial

串口发送测试#

import serial
import time
ser = serial.Serial("/dev/ttyAMA0", 9600)  
ser.flushInput()  # 位置2
ser.write("begin".encode("utf-8"))  # 串口发送字符串begin
def main():
    while True:
        count = ser.inWaiting() 
        if count != 0:
            recv = ser.read(count) 
            ser.write("Recv some data is : ".encode("utf-8"))
            ser.write(recv)
            ser.flushInput()
        time.sleep(0.1)
 
if __name__ == '__main__':
    main()

安装虚拟键盘#

sudo apt-get install matchbox-keyboard

image-20230218102819936

sudo apt-get install matchbox-keyboard --fix-missing

image-20230218102854604

我们在下图中可以看到成功安装

image-20230218103022698

树莓派安装 PyQt5#

  1. 更換軟體源 sudo nano /etc/apt/sources.list

  2. 更換這個軟體源 deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi

  3. 更新 sudo apt-get update

  4. 安裝 pyqt5 sudo apt-get install python3-pyqt5【注意大小寫,我沒寫錯】

    image-20230218105841356

  5. 啟動 python3

  6. 輸入 import PyQt5【注意大小寫,我沒寫錯】

    image-20230218105858552

  7. 如果不報錯就說明安裝成功了。

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。