1.先从wiringpi库复制一个串口代码
2.查看串口类型
3.将代码修改成ttyS5
4.改完代码后打开串口助手然后编译
代码示例:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdlib.h>
#include <unistd.h>
int fd;
void* Sendhandler()
{
char *sendbuf;
sendbuf = (char *)malloc(32*sizeof(32));
while(1){
memset(sendbuf,'\0',32);
scanf("%s",sendbuf);
while(*sendbuf){
serialPutchar(fd,*sendbuf++);
}
}
}
void* Revhandler()
{
while(1){
while(serialDataAvail(fd))
{
printf("%c",serialGetchar(fd));
fflush(stdout);
}
}
}
int main ()
{
int count ;
unsigned int nextTime ;
pthread_t idSend;
pthread_t idRev;
if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
{
fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
return 1 ;
}
pthread_create(&idSend,NULL,Sendhandler,NULL);
pthread_create(&idRev,NULL,Revhandler,NULL);
if (wiringPiSetup () == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}
while(1){
sleep(10);
}
printf ("\n") ;
return 0 ;
}