参考链接:(写的非常详细,下述方法亲测有效)pi 3 - How do I make serial work on the Raspberry Pi3 (PiZeroW, Pi4 or later models) - Raspberry Pi Stack Exchange
please follow these 6 steps carefully.
Step 1 - Install Raspbian Jessie onto a SD card and boot the Pi when connected to a network Login via terminal or desktop and shell Configure the system with:
sudo raspi-config
Expand filesystem and enable serial on advanced page, exit and reboot.
Step 2 -this won't necessary if you have jessie new release Update the system with:
sudo apt-get update
sudo apt-get upgrade
Step 3 - Device Tree settings as below:
Add device tree to /boot/config.txt to disable the Raspberry Pi 3 bluetooth.
sudo nano /boot/config.txt
Add at the end of the file
*if you want to change the blutooth to miniuart port(bad)
dtoverlay=pi3-miniuart-bt
*if you want to disable the blutooth(good)
dtoverlay=pi3-disable-bt
Exit the editor saving your changes.
Step 4 - reboot the pi
sudo reboot
step 5 -
a)to disable the Serial Console edit the file using
sudo nano /boot/cmdline.txt
remove the word phase "console=serial0,115200
" or "console=ttyAMA0,115200
"
Exit and save your changes
b)to Enable the Serial Console edit the file using
sudo nano /boot/cmdline.txt
Change the file to the following:
dwc_otg.lpm_enable=0 console=tty1 console=serial0(or ttyAMA0),115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Exit and save your changes
Step 6 - reboot the pi
sudo reboot
补充:
tips:如果遇到了 报错 DeviceError:serial:open: Permission denied
改变一下串口权限即可 (serial0改为自己的串口设备)
sudo chmod a+rw /dev/serial0
可以用如下指令设置永久可操作:
sudo usermod -aG dialout liyf
其中liyf为用户名,把此用户名加入dialout用户组(dialout是一个group,它主要负责对于串口的权限)。