当前位置:   article > 正文

linux应用:串口模块编程_ttyusb0

ttyusb0

Linux下串口是比较常用的通讯接口,有直接用串口通讯的,也有用USB转串口通讯的,还有其它方式转串口的,但不管是以什么方式,以为在linux下可以说一切皆文件,操作文件设备基本都是一样的,都是使用open,read,write,ioctrl这几个函数进行操作,串口的应用编程也不例外,本例程中的串口程序是一个比较完善的串口模块,封装了比较丰富的API,包括串口波特率、数据位、校验位、停止位等参数设置的API,方便串口端口初始化以及读写参数,开发应用可以直接使用!

一、查看系统中的串口设备

如下所示:输入 ls /dev/tty 然后table键补全,会出现诸多tty设备,其中

/dev/ttyn是虚拟控制台

/dev/ttySn是串行端口(串口)

/dev/ttyUSB0是USB到串口的转换器

而本例中使用的就是USB到串口的转换器,所以需要操作的就是这个设备文件

二、串口设备初始化过程

1、首先打开该文件设备:

  1. //后面可跟参数:O_NONBLOCK(非阻塞),O_RDWR(可读可写)
  2. //O_WRONLY(只写),O_RDONLY(只读),O_NOCTTY(非控制终端)
  3. int fd = open("/dev/ttyUSB0", O_RDWR|O_NOCTTY);//以可读可写方式打开,默认阻塞模式

2、 设置串口通讯速率:

  1. /**
  2. *@brief 设置串口通信速率
  3. *@param fd 类型 int 打开串口的文件句柄
  4. *@param speed 类型 int 串口速度
  5. *@return void
  6. */
  7. int set_speed(int fd, int speed)
  8. {
  9. int i;
  10. int status;
  11. struct termios Opt;
  12. tcgetattr(fd, &Opt);
  13. for ( i= 0; i < sizeof(spd_arr) / sizeof(int); i++) {
  14. if(speed == name_arr[i]) {
  15. tcflush(fd, TCIOFLUSH);
  16. cfsetispeed(&Opt, spd_arr[i]);
  17. cfsetospeed(&Opt, spd_arr[i]);
  18. status = tcsetattr(fd, TCSANOW, &Opt);
  19. if (status != 0) {
  20. printf("tcsetattr failed");
  21. return 1;
  22. }
  23. tcflush(fd,TCIOFLUSH);
  24. }
  25. }
  26. printf("set_speed\n");
  27. return 0;
  28. }

 3、设置串口的数据位,停止位和效验位:

  1. /**
  2. *@brief 设置串口数据位,停止位和效验位
  3. *@param fd 类型 int 打开的串口文件句柄
  4. *@param databits 类型 int 数据位 取值为 7 或者8
  5. *@param stopbits 类型 int 停止位 取值为 1 或者2
  6. *@param parity 类型 int 效验类型 取值为N,E,O,,S
  7. */
  8. int set_Parity(int fd, int databits, int parity, int stopbits, int RTSCTS)
  9. {
  10. struct termios options;
  11. if ( tcgetattr( fd,&options) != 0) {
  12. perror("SetupSerial 1");
  13. return -1;
  14. }
  15. options.c_cflag &= ~CSIZE;
  16. switch (databits) /*设置数据位数*/
  17. {
  18. case 7:
  19. options.c_cflag |= CS7;
  20. break;
  21. case 8:
  22. options.c_cflag |= CS8;
  23. break;
  24. default:
  25. fprintf(stderr,"Unsupported data size\n");
  26. return -1;
  27. }
  28. options.c_iflag |= INPCK;
  29. cfmakeraw(&options);
  30. //options.c_lflag |= (ICANON | ECHO | ECHOE);
  31. //options.c_lflag &= ~(ICANON | ECHO | ECHOE);
  32. //options.c_iflag &= ~(IXON | IXOFF);
  33. switch (parity)
  34. {
  35. case 'n':
  36. case 'N':
  37. options.c_cflag &= ~PARENB; /* Clear parity enable */
  38. options.c_iflag &= ~INPCK; /* Enable parity checking */
  39. break;
  40. case 'o':
  41. case 'O':
  42. options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
  43. break;
  44. case 'e':
  45. case 'E':
  46. options.c_cflag |= PARENB; /* Enable parity */
  47. options.c_cflag &= ~PARODD; /* 转换为偶效验*/
  48. break;
  49. case 'S':
  50. case 's': /*as no parity*/
  51. options.c_cflag &= ~PARENB;
  52. options.c_cflag &= ~CSTOPB;
  53. break;
  54. default:
  55. fprintf(stderr,"Unsupported parity\n");
  56. return -1;
  57. }
  58. /* 设置停止位*/
  59. switch (stopbits)
  60. {
  61. case 1:
  62. options.c_cflag &= ~CSTOPB;
  63. break;
  64. case 2:
  65. options.c_cflag |= CSTOPB;
  66. break;
  67. default:
  68. fprintf(stderr,"Unsupported stop bits\n");
  69. return -1;
  70. }
  71. /* Set rts/cts */
  72. if (RTSCTS)
  73. {
  74. printf("Set rts/cts");
  75. options.c_cflag |= CRTSCTS;
  76. }
  77. tcflush(fd,TCIFLUSH);
  78. options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/
  79. options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
  80. if (tcsetattr(fd,TCSANOW,&options) != 0)
  81. {
  82. printf("SetupSerial failed");
  83. return -1;
  84. }
  85. printf("set_Parity\n");
  86. return 0;
  87. }

到这里串口初始化过程就结束了,就可以编写代码进行串口通讯测试了

三、串口通讯测试

串口通讯测试main.c代码如下,过程就是通过串口模块部分提供的api,初始化一个串口,然后对串口数据进行读取,读取不到数据的时候会阻塞,因为open设备文件的时候默认是阻塞模式。

  1. #include "uart.h"
  2. int main(int argc, char **argv)
  3. {
  4. char buff[512];
  5. //打开串口0,波特率为1500000
  6. int fd = serial_def_init(0, 1500000);
  7. if(fd < 0) return 0;
  8. while(1)
  9. {
  10. serial_read(fd, buff, sizeof(buff));
  11. printf("read:%s\n", buff);
  12. }
  13. close(fd);//关闭串口
  14. return 0;
  15. }

然后编译:gcc -o app main.c uart.c

 

 我们看到编译后生成了app的文件,然后./app执行发现打开串口失败,这是因为我们的用户权限不够,一般打开硬件设备文件都需要root权限,所以我们可以以root用户权限去执行这个文件,如:sudo ./app

 于是就正常执行了这个程序,能从串口成功读取到数据!

完整的串口模块程序链接:linux下串口模块程序-Linux文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/211297
推荐阅读
相关标签
  

闽ICP备14008679号