Table of contents
1. Query the current allocation of the serial port
2. Change the hardware serial port to the default serial port
2.2 Serial port transceiver test (Python)
foreword
Raspberry Pi has 2 serial ports, hardware serial port (/dev/ttyAMA0) and mini serial port (/dev/ttyS0)
The introduction of hardware serial port and mini serial port on the Internet is as follows: "Hardware serial port" has a separate baud rate clock source, which has high performance and reliability, while "mini serial port" has low performance and simple functions, and there is no dedicated clock source for baud rate. It is provided by the CPU core clock, so the "mini serial port" has a fatal weakness: the baud rate is affected by the core clock, if the core intelligently adjusts the power consumption to reduce the main frequency, the corresponding baud rate of the mini serial port will be implicated.
The author first introduces the use of the hardware serial port (/dev/ttyAMA0)
Serial port configuration
Before starting the configuration, you need to open the Serial Port of the Raspberry Pi and close the Serial Console at the same time (not limited to this method). The demo steps I chose are as follows
Restart after setting, it is best to check after restarting
1. Query the current allocation of the serial port
Enter the command ls /dev/ser* -al to query
Taking mine as an example, the interface is as follows
This means that the default serial port currently assigned by my system is the hardware serial port, you can directly see the serial port tool minicom settings
If yours is opposite to mine, then you need to modify the configuration, please see 2. Change the hardware serial port to the default serial port
2. Change the hardware serial port to the default serial port
Enter the command sudo nano /boot/config.txt , if you are not used to using the nano editor, you can replace nano with vi, then add the following two lines at the end of the text, remember to save and exit
dtoverlay=pi3-miniuart-bt
force_turbo=1
The interface I added is as follows
At this time, restart the Raspberry Pi, and enter the command ls /dev/ser* -al to query the current serial port allocation (the author has verified it successfully)
Serial tool minicom settings
1. Install minicom
Enter the command sudo apt-get install minicom
This step is too routine, so I won't post pictures
2. minicom test
2.1 Wiring
The first is to find the pin distribution of the Raspberry Pi, which has been circled in the figure below, and then connect the RX of the usb to ttl to the TX of the Raspberry Pi, and the TX of the usb to ttl to the RX of the Raspberry Pi, VCC and GND are also certain To pick up! ! !
Friends who don’t know how to do it can learn by cramming. Just look at the wiring in the picture below and see the color of the DuPont line to connect.
2.2 Serial port transceiver test (Python)
I feel that the files in C language need to be compiled first and then run, which is a bit cumbersome and not as convenient as Python, so I usually use Python for demonstrations. If there is enough time, I will supplement the programs in C language.
Let’s not say much about creating new files and how to run Python files. Friends who don’t know how can read my beginner Raspberry Pi——(3) Raspberry Pi lights up LED (Python, C)
import serial
import time
#open serial
ser = serial.Serial("/dev/ttyAMA0", 115200)#set up serial
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()
2.3 Test effect
Just open a serial port debugging tool on the laptop, I will use XCOM as an example to demonstrate