Experiment on implementing serial communication on Raspberry Pi 4B through wiringP library

1. Preparation before realizing serial communication

Raspberry Pi 4b requires some configuration to implement serial communication with other devices. You can click on this article to view the reference article.

WiringPi library serial port related API reference article link wiringPI library serial port API

2. Realize the sending of a single character

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>

int main()
{
    
    
	//初始化外设库
	if(wiringPiSetup() == -1){
    
    
		printf("init wiringPi error\n");
		return -1;
	
	}

	int fd;//串口文件描述符

	//打开串口
   if((fd =serialOpen("/dev/ttyAMA0",9600)) == -1){
    
    

	   printf("open serial fail!\n");
	   exit(-1);

   }

	//发送数据
	while(1){
    
    
	
		serialPutchar(fd,'x');
		delayMicroseconds(1000000);//延时1s
	}


	serialClose(fd);//关闭串口
	
	return 0;
}

operation result:
Insert image description here

3. Implement string sending

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>

int main()
{
    
    
	//初始化外设库
	if(wiringPiSetup() == -1){
    
    
		printf("init wiringPi error\n");
		return -1;
	
	}

	int fd;//串口文件描述符
	char *ch="xfj hansome\r\n";

	//打开串口
   if((fd =serialOpen("/dev/ttyAMA0",9600)) == -1){
    
    

	   printf("open serial fail!\n");
	   exit(-1);

   }

	//发送数据
	while(1){
    
    
	
		serialPuts(fd,ch);//发送一个字符串
		serialPrintf(fd,"wlecome my srial,%d\r\n",fd);//这个函数可以像printf一样实现格式的控制发送
		delayMicroseconds(1000000);//延时1s
	}


	serialClose(fd);//关闭串口
	
	return 0;
}

Insert image description here

4. Realize the reception of single characters

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>

int main()
{
    
    
	//初始化外设库
	if(wiringPiSetup() == -1){
    
    
		printf("init wiringPi error\n");
		return -1;
	
	}

	int fd;//串口文件描述符
	int data;//用于接收

	//打开串口
   if((fd =serialOpen("/dev/ttyAMA0",9600)) == -1){
    
    

	   printf("open serial fail!\n");
	   exit(-1);

    }

	//发送数据
	while(1){
    
    
	
		if(serialDataAvail(fd) != -1){
    
    
			data = serialGetchar(fd);
			printf("rev:%c\n",data);
	
		}
	}


	serialClose(fd);//关闭串口
	
	return 0;
}

operation result:
Insert image description here

5. Implement string reception

#include <stdio.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main()
{
    
    
	//初始化外设库
	if(wiringPiSetup() == -1){
    
    
		printf("init wiringPi error\n");
		return -1;
	
	}

	int fd;//串口文件描述符
	char *ch= "xfj hansome\r\n";
	char buffer[128]={
    
    0};

	//打开串口
   if((fd =serialOpen("/dev/ttyAMA0",9600)) == -1){
    
    

	   printf("open serial fail!\n");
	   exit(-1);

    }

	while(1){
    
    
	
		if(serialDataAvail(fd) != -1){
    
    
			read(fd,buffer,sizeof(buffer));
			printf("rec:%s\n",buffer);
			memset(buffer,0,sizeof(buffer));
		}
	}


	serialClose(fd);//关闭串口
	
	return 0;
}

operation result:
Insert image description here

If you want to receive and send at the same time, you can open multiple threads and let different threads handle sending and receiving respectively, or you can send certain messages after they are judged to meet preset conditions.

Guess you like

Origin blog.csdn.net/m0_68038554/article/details/131916353