• 浅浅的 linux 串口驱动应用


    参考:

    Linux串口编程
    Linux串口应用编程

    主要流程 :
    1、open打开串口设备,获取串口设备文件描述符(Linux一切都是文件~)
    2、设置波特率、数据位、停止位、校验位等-
    3、read()、write()操作文件描述符进行串口通信
    4、close()关闭设备

    一、打开串口设备

    1、open串口

    跟打开其他文件一样,只不过可能会加上O_NOCTTY这两个选项O_NDELAY。

    fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
    
    • 1

    可采用下面的文件打开模式:

    O_RDONLY:以只读方式打开文件

    O_WRONLY:以只写方式打开文件

    O_RDWR:以读写方式打开文件

    (O_RDONLY和O_WRONLY和O_RDWR三个中必选一个)

    O_APPEND:写入数据时添加到文件末尾
    O_CREATE:如果文件不存在则产生该文件,使用该标志需要设置访问权限位mode_t
    O_EXCL:指定该标志,并且指定了O_CREATE标志,如果打开的文件存在则会产生一个错误
    O_TRUNC:如果文件存在并且成功以写或者只写方式打开,则清除文件所有内容,使得文件长度变为0

    O_NOCTTY:如果打开的是一个终端设备,这个程序不会成为对应这个端口的控制终端,如果没有该标志,任何一个输入,例如键盘中止信号等,都将影响进程。
    O_NONBLOCK:该标志与早期使用的O_NDELAY标志作用差不多。程序不关心DCD信号线的状态,如果指定该标志,进程将一直在休眠状态,直到DCD信号线为0。

    2、使用终端相关函数库修改串口参数配置

    LINUX 使用tcgetattr与tcsetattr函数控制终端
    嵌入式linux入门3-2-串口
    tcgetattr函数与tcsetattr函数
    使用 open 打开串口驱动以后,需要对串口的参数进行配置,依次使用如下的函数对串口配置
    以下函数可以参考:https://linux.die.net/man/3/cfsetspeed

    函数名称作用
    int tcgetattr(int fd, struct termios *termios_p);用于获取与终端相关的参数,termios_p是返回的具体参数内容
    int cfmakeraw(struct termios *termios_p);使得串口工作于RAW模式,在此模式下,tty子系统会将从串口收到的所有字节数据直接递交给应用层,而不进行额外处理,此时read()函数会在读取完缓冲区的所有数据后立刻退出并返回实际读到的数值,而不是等到\r或\n的到来
    int cfsetspeed(struct termios *termios_p, speed_t speed);设置波特率
    int tcsetattr(int fd, int optional_actions,const struct termios *termios_p);将修改后的终端参数设置到标准输入中

    termios 结构体如下:

    #include 
     
    struct termios
    {
        tcflag_t c_iflag;		/* 输入模式标志 */
        tcflag_t c_oflag;		/* 输出模式标志 */
        tcflag_t c_cflag;		/* 控制模式标志 */
        tcflag_t c_lflag;		/* 本地模式标志 */
        cc_t c_line;			/* 线路规程 */
        cc_t c_cc[NCCS];		/* 控制属性 */
        speed_t c_ispeed;		/* 输入速度 */
        speed_t c_ospeed;		/* 输出速度 */
    #define _HAVE_STRUCT_TERMIOS_C_ISPEED 1
    #define _HAVE_STRUCT_TERMIOS_C_OSPEED 1
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在打开串口的配置的时候需要着重设置比如波特率速度控制模式等参数,其他参数可以从旧的配置文件里面直接套用。

    bool Open(void) 
    {
        fd = open("/dev/ttyS3", O_RDWR); // 以读写方式打开文件
        
        struct termios term;
    
        tcgetattr(fd, &term); // 用于获取与终端相关的参数
         
        cfmakeraw(&term); // 使得串口工作于RAW模式,为原始模式
    
    	/* 计算得到具体的参数配置 */
        term.c_cflag &= ~CSTOPB; // 1位停止位
    	//term.c_cflag |= CSTOPB; // 2位停止位 
    
    	term.c_cflag |= PARENB; // 无校验
    	//term.c_cflag &= ~PARENB; // 开校验
    	term.c_cflag &= ~PARODD; // 表示奇校验
    	//term.c_cflag |= PARODD; // 无校验
        
    	term.c_cflag &= ~CSIZE; // 去掉数据位屏蔽
    	term.c_cflag |= CS8; // 8位数据位 可选CS7
    
    	/* 常见的波特率:
    	B115200
    	B9600
    	*/
        cfsetspeed(&term, B115200); // 配置串口波特率
       
    	/* 
    	TCSANOW - 立即生效
    	TCSADRAIN - 输入输出完成后生效
    	TCSAFLUSH - 刷新缓冲区后生效
    	前面已经完成了对 struct termios 结构体各个成员进行配置,但是配置还未生效,需要将配置参数
    	写入到串口,使其生效。通过 tcsetattr()函数将配置参数写入到硬件设备。
    	*/
        tcsetattr(fd, TCSAFLUSH, &term); // 写入配置
    	
    
        /* 设置低延迟模式 */
    	struct serial_struct serial_info; 
    
    	ioctl(fd, TIOCGSERIAL, &serial_info);
       
        serial_info.flags |= ASYNC_LOW_LATENCY;
    
        ioctl(fd, TIOCSSERIAL, &serial_info);
    
        return true;
    }
    
    • 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

    二、给串口写数据

    写入数据也很简单,使用write就可以发送数据了:

    write(fd, w_buf, strlen(w_buf));
    
    • 1

    和写入其他设备文件的方式相同,write函数也会返回发送数据的字节数或者在发生错误的时候返回-1。通常,发送数据最常见的错误就是EIO,当调制解调器或者数据链路将Data Carrier Detect(DCD)信号线弄掉了,就会发生这个错误。而且,直至关闭端口这个情况会一直持续。

    三、读数据

    参考
    poll机制
    linux poll函数
    Linux串口应用编程详解(Serial)

    1)poll函数

    poll函数的定义如下:

    #include 
    
    int poll(struct pollfd fd[], nfds_t nfds, int timeout);
    
    • 1
    • 2
    • 3

    第一个参数需要传入的结构体数组参数如下:

    struct pollfd{
    	int fd; // 文件描述符
    	short event;// 请求的事件
    	short revent;// 返回的事件
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    成功时,poll()返回结构体中revents域不为0的文件描述符个数;如果在超时前没有任何事件发生,poll()返回0;失败时,poll()返回-1,并设置errno为下列值之一:
      EBADF:一个或多个结构体中指定的文件描述符无效。
      EFAULT:fds指针指向的地址超出进程的地址空间。
      EINTR:请求的事件之前产生一个信号,调用可以重新发起。
      EINVAL:nfds参数超出PLIMIT_NOFILE值。
      ENOMEM:可用内存不足,无法完成请求。
    实际的使用中可以:

    {
    	struct pollfd fds[1];
    	fds[0].fd = fd_;
    	fds[0].events = POLLIN; // 使用阻塞中断 监听事件 
    	
    	int poll_return = poll(fds, 1, timeout);
    	if (poll_return == 0)
    	{
    	    printf("阻塞超时,返回");
    	    return TIMEOUT;
    	} 
    	else if (poll_return < 0)
    	{
    	    printf(" Error polling serial port: %s ", std::string(strerror(errno)));
    	    return ERROR;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2) ioctl(fd, FIONREAD, &bytes);

    参考:ioctl 之 FIONREAD
    当不确定串口接收缓存区接收到的数据大小具体的长度时,可以使用 ioctrl 读取缓存区的长度来读取数据;
    ioctl 是用来设置硬件控制寄存器,或者读取硬件状态寄存器的数值之类的。

    而read,write 是把数据丢入缓冲区,硬件的驱动从缓冲区读取数据一个个发送或者把接收的数据送入缓冲区。

    ioctl(keyFd, FIONREAD, &b)

    得到缓冲区里有多少字节要被读取,然后将字节数放入b里面。

    接下来就可以用read了。

    read(keyFd, &b, sizeof(b))
    从串口上读取数据:

    read(fd,r_buf,sizeof(r_buf));
    
    • 1

    素食:

    #include "stdio.h"
    #include  /* Error number definitions */
    #include  /* File control definitions */
    #include 
    #include 
    #include  /* POSIX terminal control definitions */
    #include   /* UNIX standard function definitions */
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include
    
    int ReadBytes(int fd);
    
    int main(void)
    {
      int fd;
      struct termios term;
    
      printf("test begin\n"); 
      fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY);
      if (fd == -1)
      {
        printf("Err INfo: open ttyS3 error!\n");
      }
      printf("ttyS3 ok!\n");
    
      if(tcgetattr(fd, &term) != 0) // 用于获取与终端相关的参数
      {
          printf("tcgetattr error\n");
      }
    
      cfmakeraw(&term); // 使得串口工作于RAW模式,为原始模式
    
      /* 计算得到具体的参数配置 */
      term.c_cflag &= ~CSTOPB; // 1位停止位
    	//term.c_cflag |= CSTOPB; // 2位停止位 
    
      term.c_cflag &= ~PARENB; // 无校验
    	//term.c_cflag |= PARENB; // 开校验
      
    	//term.c_cflag &= ~PARODD; // 表示奇校验
    	//term.c_cflag |= PARODD; // 无校验
        
    	term.c_cflag &= ~CSIZE; // 去掉数据位屏蔽
    	term.c_cflag |= CS8; // 8位数据位 可选CS7
    
      /* set timeout in deciseconds for noncanonical read */
      term.c_cc[VTIME] = 0;
      /* set minimum number of bytes to read */
      term.c_cc[VMIN] = 0;
    
      /* 清空缓冲区 */
      tcflush(fd ,TCIFLUSH);/* tcflush清空终端未完成的输入/输出请求及数据;TCIFLUSH表示清空正收到的数据,且不读取出来 */
    
    	/* 常见的波特率:
    	B115200
    	B9600
    	*/
      if(cfsetspeed(&term, B115200) < 0) // 配置串口波特率
      {
          printf("cfsetspeed error\r");
      }
    
    	/* 
    	TCSANOW - 立即生效
    	TCSADRAIN - 输入输出完成后生效
    	TCSAFLUSH - 刷新缓冲区后生效
    	前面已经完成了对 struct termios 结构体各个成员进行配置,但是配置还未生效,需要将配置参数
    	写入到串口,使其生效。通过 tcsetattr()函数将配置参数写入到硬件设备。
    	*/
      if(tcsetattr(fd, TCSADRAIN, &term) < 0) // 写入配置
    	{
        printf("tcsetattr error\r");
      }
    
        /* 设置低延迟模式 */
    	struct serial_struct serial_info; 
    
    	ioctl(fd, TIOCGSERIAL, &serial_info);
       
      serial_info.flags |= ASYNC_LOW_LATENCY;
    
      ioctl(fd, TIOCSSERIAL, &serial_info);
    
      //close(fd);
    
      write(fd, "begin\r\n", 7);
      while(1)
      {
        sleep(0.1);
        //write(fd, "123\r\n", 5);
        ReadBytes(fd);
      }
      close(fd);
    }
    
    int ReadBytes(int fd) 
    {
        char buffer[100];
        int bytes;
        struct pollfd fds[1];
        std::vector<uint8_t> output;
    
        if(fd < 0)
        {
            printf("error\n");
        }
    
        fds[0].fd = fd;
        fds[0].events = POLLIN; // 使用阻塞中断 监听事件 
    
        int poll_return = poll(fds, 1, 1000);
        if (poll_return == 0) {
            //printf(" Timed out while waiting for data. \n");
            return 0;
        } else if (poll_return < 0) {
            int error_num = errno;
            switch (error_num) {
                case EINTR:
                    return 2;
                default:
                    printf("Error polling serial port");
                    return 3;
            }
        }
    
    /*
        int rev = read(fd, buffer, 100);
        if(rev == -1)
        {
          printf("error: read nothing %d\n", rev);
        }
        printf("%s", buffer);
        return 0;
    */
    
        ioctl(fd, FIONREAD, &bytes); // 返回输入缓冲区中的字节数。
        size_t to_read = static_cast<size_t>(bytes);
    
        size_t output_size = output.size();
        output.resize(output_size + to_read);
    
        int result = read(fd, output.data() + output_size, to_read);
        if (result > 0) {
            output.resize(output_size + result);
        } else {
            output.resize(output_size);
        }
        //printf("rev buffer:[ %s, %d]\n", output.data(), bytes);
        printf("%s", output.data());
        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
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
  • 相关阅读:
    必做实验6 输入输出应用
    Java8 BiConsumer<T, U> 函数接口浅析分享(含示例,来戳!)
    HarmonyOS应用API手势方法-绑定手势方法
    ES6案例 ------ 模拟浏览器网页顶部切换栏【面向对象】
    dubbo项目整合nacos注册中心问题记录
    Java并发编程——Java筑基
    pytorch使用cat()和stack()拼接tensors
    面试题:深拷贝、浅拷贝、引用拷贝的区别
    XDU2019级保研数据统计分析
    PyQt5 QDockWidget
  • 原文地址:https://blog.csdn.net/qq_44179528/article/details/126747469