• 【全志H616 使用标准库 完成自制串口库(分文件实现) orangepi zero2(开源)】.md updata: 23/11/07


    H616 把玩注意:

    Linux内核版本5.16 及以上,需手动配置i2c-3 uart5驱动

    uart5 即ttyS5串口通讯

    配置示例
    orangepi@orangepizero2:~/y$ uname -r //查看内核版本
    5.16.17-sun50iw9 
    
    //打开修改boot下的配置文件
    orangepi@orangepizero2:~/y$ sudo vi  /boot/orangepiEnv.txt
        1 verbosity=7
        2 bootlogo=serial
        3 console=both
        4 disp_mode=1920x1080p60
        5 overlay_prefix=sun50i-h616
        6 rootdev=UUID=5e468073-a25c-4048-be42-7f5    cfc36ce25
        7 rootfstype=ext4
        8 overlays=i2c3 uart5  //在这里添加适配i2c-3和uart5
        9 usbstoragequirks=0x2537:0x1066:u,0x2537:    0x1068:u
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    分文件编译时需将每个文件一同编译 (空格隔开)
    例: ggc a.c b.c b.h -lpthread -lxxx…;
    常用可封装成一个build.sh 脚本,方便编译;
    例:
    orangepi@orangepizero2:~/y$ cat build.sh 
    gcc $1 uartTool.h uartTool.c -lwiringPi -lwiringPiDev -lpthread -lm -lcrypt -lrt 
    给sh脚本执行权限后,使用即可完成编译:
    orangepi@orangepizero2:~/y$ ./build.sh my_uartxxx.c 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    常用命令

    查看驱动文件

    例_串口:ls /dev/ttyS*
    在这里插入图片描述

    查看内核检测信息/硬件

    例_usb设备:ls /dev/bus/usb

    使用wiringPi库 完成串口通讯程序:

    serialTest.c
    /* serialTest.c */
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int fd;
    
    void* func1()
    {
      char buf[32] = {0};
      char *data = buf;
    
      while(1){
        memset(data,'\0',32);
        printf("input send data to serial: \n");
        scanf("%s",data);
        //发送数据_串口
        int i = 0;
        while(*data){
          serialPutchar (fd, *data++) ;      
        }
      }
    }
    
    void* func2()
    {
      while(1){
         while(serialDataAvail(fd)){ 
          //接收数据_串口
          printf("%c",serialGetchar (fd));
          //清空/刷新写入缓存区(标准输出流 stdout);
          //确保打印输出立即显示在控制台上,而不是在缓冲区中等待。
          fflush (stdout) ;
         }
      }
    }
    
    int main ()
    {
      
      unsigned int nextTime ;
    
      pthread_t t1,t2;
    
      if ((fd = serialOpen ("/dev/ttyS5", 115200)) < 0)
      {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
      }
    
     
      pthread_create(&t1,NULL,func1,NULL); 
      pthread_create(&t2,NULL,func2,NULL); 
    
       if (wiringPiSetup () == -1)
      {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
      } 
    
      
      while(1){sleep(10);};
      
      return 0 ;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    使用标准库 完成自制串口库(分文件实现):

    uartTool.h
    /* uartTool.h */
    int mySerialOpen (const char *device, const int baud);
    void mySerialsend (const int fd,const char* s);
    int mySerialread (const int fd,char* buf);
    
    • 1
    • 2
    • 3
    • 4
    uartTool.c
    /* 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 ;
    		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 mySerialsend (const int fd,const char* s)
    {
    	int ret;
    	ret = write (fd, s, strlen (s));
    	if (ret < 0){
    		printf("Serial Puts Error\n");
    	}
    }
    
    int mySerialread (const int fd,char* buf)
    {
    	int n_read = read (fd, buf, 1);
    	if(n_read < 0){
    		perror("read error");
    	}
    	return n_read ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    my_uart_ttyS5_test.c
    /* my_uart_ttyS5_test.c */
    #include 
    #include 
    #include 
    // #include 
    // #include 
    #include 
    #include 
    #include 
    
    #include "uartTool.h"
    
    int fd;
    
    void* func1()
    {
        char data[32] = {0};
        
    
        while(1){
            memset(data,'\0',32);
            scanf("%s",data);
            //发送数据_串口
            mySerialsend (fd, data) ;     
        }
    }
    
    void* func2()
    {
        char buf[32] = {0};
        while(1){
            while(mySerialread(fd,buf)){ 
            //接收数据_串口
            printf("%s",buf);        
            }
        }
    }
    
    int main ()
    {
    
        pthread_t t1,t2;
    
        if ((fd = mySerialOpen ("/dev/ttyS5", 115200)) < 0)
        {
            fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
            return 1 ;
        }
        
        pthread_create(&t1,NULL,func1,NULL); 
        pthread_create(&t2,NULL,func2,NULL); 
        
        while(1){sleep(10);};
        
        return 0 ;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    【C++入门系列】——类和对象
    MATLAB中的稀疏矩阵和密集矩阵
    (附源码)ssm介绍信智能实现系统 毕业设计 260930
    商城积分系统的设计方案(上)-- 需求分析
    Kotlin高仿微信-第21篇-个人信息-修改头像
    Python大作业——爬虫+可视化+数据分析+数据库(可视化篇)
    javaweb第一章 mysql数据库
    机场、油库、边防、电站都在用的调频无人机干扰设备---TFN MR61
    力扣每日一题:1710. 卡车上的最大单元数【自定义排序+贪心】
    网络编程:导入电子词典
  • 原文地址:https://blog.csdn.net/Wdf_123654/article/details/134263032