[ARM+Linux] 基于wiringPi库的串口通信

wiringOP-master/examples/serialTest.c中,wiringPi库中自带的串口程序:

#include 
#include 
#include 

#include 
#include 

int main ()
{
    int fd ;
    int count ;
    unsigned int nextTime ;

    if ((fd = serialOpen ("/dev/ttyS2", 115200)) < 0)//打开在/dev.ttyS2路径下的文件,波特率配置成115200,如果返回值为-1说明打开失败
    {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
    }

    if (wiringPiSetup () == -1)//初始化wiringPi库
    {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
    }

    nextTime = millis () + 300 //这个函数返回 一个 从你的程序执行 wiringPiSetup 初始化函数(或者wiringPiSetupGpio ) 到 当前时间 经过的 毫秒数。返回类型是unsigned int,最大可记录 大约49天的毫秒时长。


    for (count = 0 ; count < 256 ; )
    {
        if (millis () > nextTime)
        {
            printf ("\nOut: %3d: ", count) ;
            fflush (stdout) ;//fflush(stdout)刷新标准输出缓冲区,把输出缓冲区里的东西打印到标准输出设备上。
            serialPutchar (fd, count) ;//将字符输出到串口
            nextTime += 300 ;
            ++count ;
        }

        delay (3) ;

        while (serialDataAvail (fd))
        {
            printf (" -> %3d", serialGetchar (fd)) ;//获取串口的数据
            fflush (stdout) ;
        }
    }

    printf ("\n") ;
    return 0 ;

(90条消息) fflush(stdio)、fflush(stdout)详解_hanxp001的博客-CSDN博客

根据官方的wiringPi库修改接收和发送串口数据

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int fd ;

void* SendHandler()
{
    char *SendBuf;
    SendBuf = (char *)malloc(32*sizeof(32));
    while(1)
    {
        memset(SendBuf,'\0',32);
        scanf("%s",SendBuf);
        while(*SendBuf != NULL)
        {
            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);//创建线程1发送数据
    pthread_create(&idrev,NULL,RevHandler,NULL);//创建线程2接收数据
    if (wiringPiSetup () == -1)
    {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
    }

    while(1)
    {
        sleep(10);
    }
    printf ("\n") ;
    return 0 ;
}


 修改过后通过创建线程来接收和发送串口数据

    pthread_create(&idsend,NULL,SendHandler,NULL);//创建线程1发送数据
    pthread_create(&idrev,NULL,RevHandler,NULL);//创建线程2接收数据

(90条消息) Linux 线程_TX564的博客-CSDN博客

接收数据通过调用wiringPi库自带函数 serialGetchar(fd)来获取上位机通过串口发送的数据

[ARM+Linux] 基于wiringPi库的串口通信_第1张图片

通过scanf获取来自键盘输入的数据再通过serialPutchar(fd, *SendBuf++);发送数据

 (90条消息) wiringPI库_LEO-max的博客-CSDN博客

基于C库的原生开发串口主要是靠一个结构体实现所有的启动位,停止位,数据位,奇偶校验,波特率

[ARM+Linux] 基于wiringPi库的串口通信_第2张图片 [ARM+Linux] 基于wiringPi库的串口通信_第3张图片

根据wiringPi库自己写一个串口

先在桌面新建一个UartTool.c文件然后拖入Source Insight中编写

UartTool.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "wiringSerial.h"


int MyserialOpen (const char *device, const int baud)
{
   struct termios options ;
   speed_t myBaud ;
   int	  status, fd ;

   switch(baud)
   	{
		case 9600: myBaud = B9600;break;
		case 115200: myBaud = B115200;break;
    }
   if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)//打开设备
    return -1 ;

   // Get and modify current options:

    tcgetattr (fd, &options) ;

    cfmakeraw   (&options) ;
    cfsetispeed (&options, myBaud) ;//波特率设置,进波特率
    cfsetospeed (&options, myBaud) ;//波特率设置,出波特率

    options.c_cflag |= (CLOCAL | CREAD) ;
    options.c_cflag &= ~PARENB ;//设置奇偶校验位
    options.c_cflag &= ~CSTOPB ;//停止位
    options.c_cflag &= ~CSIZE ;
    options.c_cflag |= CS8 ;//数据位,8位
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
    options.c_oflag &= ~OPOST ;

    options.c_cc [VMIN]  =   0 ;
    options.c_cc [VTIME] = 100 ;	// Ten seconds (100 deciseconds)
 
    tcsetattr (fd, TCSANOW, &options) ;

    ioctl (fd, TIOCMGET, &status);

    status |= TIOCM_DTR ;
    status |= TIOCM_RTS ;

    ioctl (fd, TIOCMSET, &status);

    usleep (10000) ;	// 10mS

  return fd ;
}


void serialSendstring (const int fd, const char *s)
{
	int ret;
	ret = write (fd, s, strlen (s));
	if (ret < 0)
		printf("Serial Puts Error\n");

}


int serialGetstring(const int fd,char*buffer)
{
	int n_read;
	n_read = read(fd,buffer,32);
	return n_read;
}


int serialDataAvail (const int fd)
{
  int result ;

  if (ioctl (fd, FIONREAD, &result) == -1)
    return -1 ;

  return result ;
}

 UartTool.h

   int MyserialOpen (const char *device, const int baud);
  
   void serialSendstring (const int fd, const char *s);
  
   int serialGetstring(const int fd,char*buffer);
  
   int serialDataAvail (const int fd);

UartTest.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "UartTool.h"
#include 
#include 


int fd;

void* readSerial()
{
    char buffer[32] = {'\0'};
     while(1)
     {
        while(serialDataAvail(fd))//从串口读取一个字节(没有可用数据会等待10s)如果有的话返回数据,没有的话返回-1
        {
            memset(buffer,'\0',sizeof(buffer));
            serialGetstring(fd,buffer);
            printf("GET->%s\n",buffer);
        }
     }
}

void* sendSerial()
{
    char buffer[32] = {'\0'};
    while(1)
    {
        memset(buffer,'\0',sizeof(buffer));
        scanf("%s",buffer);
        serialSendstring(fd,buffer);
    }
}


int main(int argc,char** argv)
{
    char deviceName[32] = {'\0'};
    pthread_t readt;
    pthread_t sendt;
    if(argc < 2)
    {
        printf("uage:%s /dev/ttyS?\n",argv[0]);
        return -1;
    }

    strcpy(deviceName,argv[1]);

    if((fd =MyserialOpen(deviceName,115200)) == -1)
    {
        printf("open %s error!\n",deviceName);
        return -1;
    }
    pthread_create(&readt,NULL,readSerial,NULL);
    pthread_create(&sendt,NULL,sendSerial,NULL);


    while(1){sleep(10);}
}

通过以上操作就可以摆脱wiringPi库了 

 核心就是打开某个设备(文件),将参数配置好传给内核,内核驱动根据给的各个参数去配置硬件的寄存器,向串口写数据其实就是写文件操作,读数据其实就是读文件操作

基于串口开发一个小项目:用语音控制抖音上滑下滑点赞锁屏 

你可能感兴趣的:(linux,运维,服务器)