Raspberry Pi 10: Serial communication programming between the Raspberry Pi and the computer

Serial communication programming between Raspberry Pi and computer

Serial communication: usually used in multi-machine communication.
Problem 1: Concept
Kushiyuki communication:Data is transmitted in bits—the communication distance is relatively long
Serial communication distance:
15 meters parallel communication: multi-bit simultaneous transmission (8-bit, 16-bit, 32-bit, 64-bit...) The transmission distance is relatively short
Asynchronous communication: Receiver and sender use different clock frequency signals
Synchronous communication: The receiver and the sender use the same clock frequency signal
Simplex communication: Data can only be transmitted in one direction-radio
Half duplex: Means that data can be transmitted in both directions of a signal carrier, but not at the same time. —Intercom
Full duplex(Multi-machine communication): At the same time, it can be used as a receiving device or a sending device. —Mobile phone
problem 2:
Data Format(Language): start bit data bit, parity bit, stop bit
Baud rate(Speaking rate): Common baud rate: Can be set by yourself: 4800 9600 115200

1. View the pin number table

Use the following command in the console:

gpio readall

You can also check the figure below.
== Note==: When viewing, face the USB port of the Raspberry Pi to yourself, so that the view is correct.

Insert picture description here
Two, serial communication

Insert picture description here
Insert picture description here
Three, the first use of the serial port requires configuration

Under the linux operating system, everything is a file.
View all files:

cd /dev
ls

Insert picture description hereSee if it means the serial port file:
Insert picture description here
we need to modify the configuration to achieveSerial communication(The previous serial port configuration is to make the serial portprintThe data when the operating system starts), as follows:

Insert picture description here
If not foundinittabFile, then just restart

Realize serial communication through programming:

Reference Code

include<stdio.h>
include<wiringPi.h>
#include<wiringSerial.h>
int main()
{
    
    
	int fd;
 	int cmd
 	wiringPiSetup();
 	fd=serialOpen("/dev/ttyAMA0",9600);
 	while(1)
 	{
    
    
  		while(serialDAtaAvail(fd) != -1) //串口有数据,		去读 
  		{
    
    
  			 cmd=serialGetchar(fd); 
   			if(cmd==2)
   			{
    
    
    			serialPuts(fd,"liujinhui2\r\n")
   			}
   			if(cmd==3)
   			{
    
    
    			serialPuts(fd,"liujinhui3\r\n")
   			}
   			if(cmd==4)
   			{
    
    
    			serialPuts(fd,"liujinhui4\r\n")
   			}
   		serialPuts(fd,"liujinhui\r\n");//向串口写入数据 
  		delayMicroseconds(1000000);//每间隔1s打印一次 
 	}
 	return 0;
}

Compile and run:

gcc demo5.c -lwiringPi
./a.out
//通过串口助手:
//接收数据:
liujinhui
liujinhui
。。。
//发送数据:
2---liujinhui2
3---liujinhui3
4---liujinhui4

Learning reference:
detailed explanation of the wiringPi library for Raspberry Pi

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108688915