From the relevant information of Raspberry Pi, we can see that the physical pins 8 and 10 of Raspberry Pi can be mapped to two serial ports, one of which is the hardware serial port (/dev/ttyAMA0) and the other is the mini serial port (/dev/ttyS0). The hardware serial port has a separate baud rate clock source, which has good performance and strong stability; The mini serial port has simple functions and poor stability. The baud rate is provided by the CPU kernel clock and is affected by the kernel clock.
Raspberry Pi (3rd/4th generation) onboard Bluetooth module, the default hardware serial port is assigned to the Bluetooth module, while the poor performance mini serial port is assigned to the GPIO serial ports TXD0 and RXD0.
Run the following command to view the default serial port allocation method:
ls /dev -al
Due to the hardware serial port being assigned to onboard Bluetooth, we need to release it and set the hardware serial port to be assigned to GPIO serial port.
1.After SSH logs into the Raspberry Pi system
xxxxxxxxxx
sudo raspi-config
Enter the Raspberry Pi system configuration interface and select Interfacing Options:
Choose P6 Serial
Select to turn off the serial port login function and turn on the hardware serial port debugging function.
After completion, prompt the following interface and press OK
Exit raspi config settings and follow the prompts to restart Raspberry Pi.
2.Set the hardware serial port to GPIO serial port
Edit the config.txt file in the/boot directory
xxxxxxxxxx
sudo nano /boot/config.txt
Add the following two lines to the end:
xdtoverlay=pi3-miniuart-bt
force_turbo=1
The modified image is shown below.
Save: Ctrl+O,Exit: Ctrl+X。
Reboot Raspberry Pi.
xxxxxxxxxx
sudo reboot
After restarting the Raspberry Pi, enter ls/dev - al again, and you can see that the two serial ports have switched positions with each other:
Minicom serial port assistant testing
Installing Minicom
xxxxxxxxxx
sudo apt-get install minicom
Start Minicom after installation is complete
xxxxxxxxxx
minicom -D /dev/ttyAMA0 -b 9600
Among them, - D represents selecting serial port/dev/ttyAMA0, and - b sets the baud rate to 9600. This parameter can be set without any need, with a default of 115200.
Open the serial port to transfer data through the USB to TTL module.
The operation of exiting Minicom is quite complicated. According to the prompt, you need to first press Ctrl+A, then press Z to pop up the following menu.
Next, press X, and finally select YES and press Enter to confirm.
4.C language test code
Print hello world
The code is as follows:
xxxxxxxxxx
#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
int fd;
if(wiringPiSetup() < 0)return 1;
if((fd = serialOpen("/dev/ttyAMA0",9600)) < 0)return 1;
printf("serial test start ...n");
serialPrintf(fd,"Hello World!!!n");
while(1)
{
serialPutchar(fd,serialGetchar(fd));
}
serialClose(fd);
return 0;
}
Create a new testCom. c file and copy the above code into it
xxxxxxxxxx
nano testCom.c
Save: Ctrl+O, exit Ctrl+X
Use the gcc compiler to compile programs:
xxxxxxxxxx
gcc testCom.c -o test -lwiringPi
Run a program
xxxxxxxxxx
./test
You can also send data to Raspberry Pi through the serial port, and Raspberry Pi will directly return it to the serial port for display.
Note: If there is a display of garbled code, please check the baud rate and set it to 9600, which corresponds to the above code.