Raspberry Pi uses serial port to communicate (this is the serial port of Raspberry Pi written in c)

This is the serial port of Raspberry Pi written in c

Retrieved from: https://blog.csdn.net/guet_gjl/article/details/85164072

Raspberry Pi uses serial port to communicate

guet_gjl 2018-12-21 16:19:53 12069 Collection 18

copyright

Raspberry Pi serial communication

I use the Raspberry Pi 3b+ model. Various modifications on the Internet make the Raspberry Pi’s serial port work. It is too confusing. Some tutorials may even be broken by the Raspberry Pi system, especially the Raspberry Pi. The Chinese tutorial in the laboratory is wrong, and the system crashes and reinstalls directly according to that. So I am here to record the process of simple serial port configuration and communication.

First step configuration

In fact, configuration only requires three steps

1. Turn on the serial. This can be found directly in the Raspberry Pi Configuration in the current latest system. Change the Serial Port to Enable in Interfaces and restart it.

Two, modify the configuration file

sudo gedit /boot/cmdline.txt
  • 1

Delete the console=serial1,115200 inside

Third, you need to change the serial port, because serial0 and serial1 are different, the default one we need is occupied by Bluetooth

By ls -l /devcan see

sudo gedit /boot/config.txt
  • 1

Add a sentence at the end

dtoverlay=pi3-miniuart-bt
  • 1

In this way, after restarting, the two serial port types are exchanged, and we can use this serial port to communicate
as follows:
Insert picture description here

The second step is to write a serial port program

Here, there are many ways of serial communication and testing, and communication can be carried out through python gcc etc.

The python method is relatively simple, but I thought about it, and it may not be suitable for my future work, so here I only use this as a test interface to see if it can be used. For details, please refer to the blogger’s python method
https://blog.csdn .net/weixin_41656968/article/details/80085836

Let’s talk about communication using gcc g++. Here is a useful library wiringPi

How to install it will not be introduced, it is very simple. Mainly depends on how to use

The blogger wrote very carefully about this, and the introduction is also very clear
https://www.cnblogs.com/lulipro/p/5992172.html

This code can be used to test whether the interface is open, etc.
Create a new .c file, and then use gcc to run

#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");
	serialClose(fd);
	return 0;
}

After editing, compile and run with gcc, you can output a hello world in the terminal!

  gcc test.c -o test -lwiringPi
    ./test

Be sure to bring the following library, otherwise the compilation will fail

Guess you like

Origin blog.csdn.net/sinat_16643223/article/details/108893959