全志H616开发(3)——基于官方外设开发(3)

基于官方外设开发(3)

一、Linux串口开发

  1. 基于wiringPi的串口开发
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <wiringPi.h>
    #include <wiringSerial.h>
    #include <pthread.h>
    #include <malloc.h>
    int fd;
    
    
    void *SendHandler()
    {
    	char *str;
    	str = (char *)malloc(128*sizeof(char));
    	
    	while(1) {
    		memset(str, '\0', 128*sizeof(char));
    		scanf("%s",str);
    	
    		while(*str) {
    			serialPutchar (fd, *str) ;
    			str++;
    		}
    	}
    }
    
    void *RevHandler()
    {
    	while(1) {
    		while (serialDataAvail (fd))
      	  {
         	 printf ("%c", serialGetchar (fd)) ;
        	}
    	}
    }
    
    int main ()
    {
    	pthread_t send_t;
    	pthread_t rev_t;
    
      if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
      {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
      if (wiringPiSetup () == -1)
      {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
    	pthread_create(&send_t, NULL, SendHandler, NULL);
    	pthread_create(&rev_t, NULL, RevHandler, NULL);
    
        while (1)
    	{
    	 //	usleep(1000);	
    	}
    
      return 0 ;
    }
    
    
  2. Linux原生串口开发
    • myserialTest.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdarg.h>
    #include <string.h>
    #include <termios.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #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 ;
        default :
    		return -2 ;
      }
    
      if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
        return -1 ;
    
      fcntl (fd, F_SETFL, O_RDWR) ;
    
    // 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 ;
        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 myserialPuts (const int fd, const char *s)
    {
    	int ret;
    	ret = write (fd, s, strlen (s));
    	if (ret < 0)
    		printf("Serial Puts Error\n");
    }
    
    int myserialGetstr (const int fd, char *buf)
    {
    	uint8_t x;
    	x = read(fd, buf, 32);
    	if(x != 1)
    		return -1;
    
    	return x;
    }
    
    • myserialTest.h
    int myserialOpen (const char *device, const int baud);
    void myserialPuts (const int fd, const char *s);
    int myserialGetstr (const int fd, char *buf);
    
    • serialTest.h
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <wiringPi.h>
    #include <wiringSerial.h>
    #include <pthread.h>
    #include <malloc.h>
    int fd;
    
    
    void *SendHandler()
    {
    	char *str;
    	str = (char *)malloc(128*sizeof(char));
    	
    	while(1) {
    		memset(str, '\0', 128*sizeof(char));
    		scanf("%s",str);
    	
    		while(*str) {
    			serialPutchar (fd, *str) ;
    			str++;
    		}
    	}
    }
    
    void *RevHandler()
    {
    	while(1) {
    		while (serialDataAvail (fd))
      	  {
         	 printf ("%c", serialGetchar (fd)) ;
        	}
    	}
    }
    
    int main ()
    {
    	pthread_t send_t;
    	pthread_t rev_t;
    
      if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
      {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
      if (wiringPiSetup () == -1)
      {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
    	pthread_create(&send_t, NULL, SendHandler, NULL);
    	pthread_create(&rev_t, NULL, RevHandler, NULL);
    
        while (1)
    	{
    	 //	usleep(1000);	
    	}
    
      return 0 ;
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值