imx6ul usb转串口调试(代码)

蔚落 2022-12-30 04:59 359阅读 0赞

这里需要注意的是,当usb转串口模块插到usb上面/dev下面才能有设备,拔了设备就没了,所以操作的时候要注意。

  1. #include <stdio.h> //标准输入输出,如printf、scanf以及文件操作
  2. #include <stdlib.h> //标准库头文件,定义了五种类型、一些宏和通用工具函数
  3. #include <unistd.h> //定义 read write close lseek 等Unix标准函数
  4. #include <sys/types.h> //定义数据类型,如 ssiz e_t off_t 等
  5. #include <sys/stat.h> //文件状态
  6. #include <fcntl.h> //文件控制定义
  7. #include <termios.h> //终端I/O
  8. #include <errno.h> //与全局变量 errno 相关的定义
  9. #include <getopt.h> //处理命令行参数
  10. #include <string.h> //字符串操作
  11. #include <time.h> //时间
  12. #include <sys/select.h> //select函数
  13. #include <sys/ioctl.h>
  14. #include <net/if.h>
  15. #include <pthread.h>
  16. int usb2uartfd;
  17. int setOpt(int fd, int nSpeed, int nBits, int nParity, int nStop)
  18. {
  19. struct termios newtio, oldtio;
  20. // 保存测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息
  21. if (tcgetattr(fd, &oldtio) != 0)
  22. {
  23. perror("SetupSerial 1");
  24. return -1;
  25. }
  26. bzero(&newtio, sizeof(newtio)); //新termios参数清零
  27. newtio.c_cflag |= CLOCAL | CREAD; //CLOCAL--忽略 modem 控制线,本地连线, 不具数据机控制功能, CREAD--使能接收标志
  28. // 设置数据位数
  29. newtio.c_cflag &= ~CSIZE; //清数据位标志
  30. switch (nBits)
  31. {
  32. case 7:
  33. newtio.c_cflag |= CS7;
  34. break;
  35. case 8:
  36. newtio.c_cflag |= CS8;
  37. break;
  38. default:
  39. fprintf(stderr, "Unsupported data size\n");
  40. return -1;
  41. }
  42. // 设置校验位
  43. switch (nParity)
  44. {
  45. case 'o':
  46. case 'O': //奇校验
  47. newtio.c_cflag |= PARENB;
  48. newtio.c_cflag |= PARODD;
  49. newtio.c_iflag |= (INPCK | ISTRIP);
  50. break;
  51. case 'e':
  52. case 'E': //偶校验
  53. newtio.c_iflag |= (INPCK | ISTRIP);
  54. newtio.c_cflag |= PARENB;
  55. newtio.c_cflag &= ~PARODD;
  56. break;
  57. case 'n':
  58. case 'N': //无校验
  59. newtio.c_cflag &= ~PARENB;
  60. break;
  61. default:
  62. fprintf(stderr, "Unsupported parity\n");
  63. return -1;
  64. }
  65. // 设置停止位
  66. switch (nStop)
  67. {
  68. case 1:
  69. newtio.c_cflag &= ~CSTOPB;
  70. break;
  71. case 2:
  72. newtio.c_cflag |= CSTOPB;
  73. break;
  74. default:
  75. fprintf(stderr,"Unsupported stop bits\n");
  76. return -1;
  77. }
  78. // 设置波特率 2400/4800/9600/19200/38400/57600/115200/230400
  79. switch (nSpeed)
  80. {
  81. case 2400:
  82. cfsetispeed(&newtio, B2400);
  83. cfsetospeed(&newtio, B2400);
  84. break;
  85. case 4800:
  86. cfsetispeed(&newtio, B4800);
  87. cfsetospeed(&newtio, B4800);
  88. break;
  89. case 9600:
  90. cfsetispeed(&newtio, B9600);
  91. cfsetospeed(&newtio, B9600);
  92. break;
  93. case 19200:
  94. cfsetispeed(&newtio, B19200);
  95. cfsetospeed(&newtio, B19200);
  96. break;
  97. case 38400:
  98. cfsetispeed(&newtio, B38400);
  99. cfsetospeed(&newtio, B38400);
  100. break;
  101. case 57600:
  102. cfsetispeed(&newtio, B57600);
  103. cfsetospeed(&newtio, B57600);
  104. break;
  105. case 115200:
  106. cfsetispeed(&newtio, B115200);
  107. cfsetospeed(&newtio, B115200);
  108. break;
  109. case 230400:
  110. cfsetispeed(&newtio, B230400);
  111. cfsetospeed(&newtio, B230400);
  112. break;
  113. default:
  114. printf("\tSorry, Unsupported baud rate, set default 9600!\n\n");
  115. cfsetispeed(&newtio, B9600);
  116. cfsetospeed(&newtio, B9600);
  117. break;
  118. }
  119. // 设置read读取最小字节数和超时时间
  120. newtio.c_cc[VTIME] = 1; // 读取一个字符等待1*(1/10)s
  121. newtio.c_cc[VMIN] = 1; // 读取字符的最少个数为1
  122. tcflush(fd,TCIFLUSH); //清空缓冲区
  123. if (tcsetattr(fd, TCSANOW, &newtio) != 0) //激活新设置
  124. {
  125. perror("SetupSerial 3");
  126. return -1;
  127. }
  128. printf("Serial set done!\n");
  129. return 0;
  130. }
  131. void usb2uartfdCreate()
  132. {
  133. int ret = 0;
  134. usb2uartfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
  135. if(usb2uartfd < 0)
  136. {
  137. printf("usb uart open failed ret = %d\r\n",usb2uartfd);
  138. usb2uartfd = -1;
  139. return;
  140. }
  141. ret = fcntl(usb2uartfd, F_SETFL, 0);
  142. if(ret < 0)
  143. {
  144. printf("fcntl failed ret = %d\r\n",ret);
  145. close(usb2uartfd);
  146. usb2uartfd = -1;
  147. return;
  148. }
  149. if (isatty(usb2uartfd) == 0)
  150. {
  151. printf("standard input is not a terminal device\r\n");
  152. close(usb2uartfd);
  153. usb2uartfd = -1;
  154. return;
  155. }
  156. if (setOpt(usb2uartfd, 115200, 8, 'N', 1)== -1) //设置8位数据位、1位停止位、无校验
  157. {
  158. printf("Set opt Error\r\n");
  159. close(usb2uartfd);
  160. usb2uartfd = -1;
  161. return ;
  162. }
  163. tcflush(usb2uartfd, TCIOFLUSH); //清掉串口缓存
  164. fcntl(usb2uartfd, F_SETFL, 0); //串口阻塞
  165. printf("uart fd create ok\r\n");
  166. }
  167. void *uartrecv(void *arg)
  168. {
  169. unsigned int maxfd = 0;
  170. unsigned int ret = 0;
  171. struct timeval waittime;
  172. fd_set rfd;
  173. char buf[128];
  174. while(1)
  175. {
  176. FD_ZERO(&rfd);
  177. if(usb2uartfd == -1)
  178. return NULL;
  179. FD_SET(usb2uartfd,&rfd);
  180. if(maxfd < usb2uartfd)
  181. {
  182. maxfd = usb2uartfd;
  183. }
  184. waittime.tv_sec = 1;
  185. waittime.tv_usec = 0;
  186. ret = select(maxfd + 1,&rfd,NULL,NULL,&waittime);
  187. if(0 >= ret)
  188. {
  189. continue;
  190. }
  191. if(FD_ISSET(usb2uartfd,&rfd))
  192. {
  193. memset(buf,0,sizeof(buf));
  194. read(usb2uartfd,buf,sizeof(buf) - 1);
  195. printf("usb uart recv buf = %s\r\n",buf);
  196. }
  197. }
  198. }
  199. void main()
  200. {
  201. char buf[128];
  202. pthread_t ntid;
  203. usb2uartfdCreate();
  204. pthread_create(&ntid, NULL, uartrecv, NULL);
  205. while(1)
  206. {
  207. if(usb2uartfd != -1)
  208. {
  209. memset(buf,0,sizeof(buf));
  210. sprintf(buf,"this is usb uart send date");
  211. write(usb2uartfd,buf,strlen(buf));
  212. tcflush(usb2uartfd,TCOFLUSH);
  213. }
  214. sleep(5);
  215. }
  216. }

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读

    相关 imx6ul can调试代码

    下面是我的代码,然后代码呢实际的使用 需要注意,如果重新设置了can的引脚对应关系,需要重新配置设备树文件; 更改设备树文件:[https://blog.csdn.net/c

    相关 安卓USB串口

     准备硬件 1.先得准备一个有OTG功能的手机,也就是可以作为USB主机的手机,当然,现在绝大部分手机都是支持这个功能的,除非是早期的安卓老爷机才不支持。我的旧手机荣耀6