imx6ul uart8路串口调试(代码)

朴灿烈づ我的快乐病毒、 2022-12-30 04:00 288阅读 0赞

uart这里的调试要注意的是管脚的功能配置,需要更改设备树文件;

更改设备树文件:https://blog.csdn.net/cao849861802/article/details/111604277

其他问题:这里是操作的232的配置,如果想做485的最好用硬件来直接做485,因为我这边用软件尝试了几次都未成功。

  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 uartfd[7];
  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 uartFdCreate()
  132. {
  133. int i = 0;
  134. int ret = 0;
  135. char buf[64];
  136. for(i = 0; i < 7; i++)
  137. {
  138. memset(buf,0,sizeof(buf));
  139. sprintf(buf,"/dev/ttymxc%d",i+1);
  140. uartfd[i] = open(buf, O_RDWR | O_NOCTTY | O_NDELAY);
  141. if(uartfd[i] < 0)
  142. {
  143. printf("%s open failed ret = %d\r\n",buf,uartfd[i]);
  144. uartfd[i] = -1;
  145. continue;
  146. }
  147. ret = fcntl(uartfd[i], F_SETFL, 0);
  148. if(ret < 0)
  149. {
  150. printf("fcntl failed ret = %d\r\n",ret);
  151. close(uartfd[i]);
  152. uartfd[i] = -1;
  153. continue;
  154. }
  155. if (isatty(uartfd[i]) == 0)
  156. {
  157. printf("standard input is not a terminal device\r\n");
  158. close(uartfd[i]);
  159. uartfd[i] = -1;
  160. continue;
  161. }
  162. if (setOpt(uartfd[i], 115200, 8, 'N', 1)== -1) //设置8位数据位、1位停止位、无校验
  163. {
  164. printf("Set opt Error\r\n");
  165. close(uartfd[i]);
  166. uartfd[i] = -1;
  167. continue;
  168. }
  169. tcflush(uartfd[i], TCIOFLUSH); //清掉串口缓存
  170. fcntl(uartfd[i], F_SETFL, 0); //串口阻塞
  171. printf("uart%d fd create ok,fd = %d\r\n",i+1,uartfd[i]);
  172. }
  173. }
  174. void *uartrecv(void *arg)
  175. {
  176. int i = 0;
  177. unsigned int maxfd = 0;
  178. unsigned int ret = 0;
  179. struct timeval waittime;
  180. fd_set rfd;
  181. char buf[128];
  182. printf("uart recv task stat****\r\n");
  183. while(1)
  184. {
  185. FD_ZERO(&rfd);
  186. for(i = 0; i < 7; i++)
  187. {
  188. if(uartfd[i] != -1)
  189. {
  190. FD_SET(uartfd[i],&rfd);
  191. if(maxfd < uartfd[i])
  192. {
  193. maxfd = uartfd[i];
  194. }
  195. }
  196. }
  197. waittime.tv_sec = 1;
  198. waittime.tv_usec = 0;
  199. ret = select(maxfd + 1,&rfd,NULL,NULL,&waittime);
  200. if(0 >= ret)
  201. {
  202. continue;
  203. }
  204. for(i = 0; i < 7; i++)
  205. {
  206. if(FD_ISSET(uartfd[i],&rfd))
  207. {
  208. memset(buf,0,sizeof(buf));
  209. read(uartfd[i],buf,sizeof(buf) - 1);
  210. printf("uart%d recv buf = %s\r\n",i+1,buf);
  211. }
  212. }
  213. }
  214. }
  215. void main()
  216. {
  217. int i = 0;
  218. int retlen = 0;
  219. char buf[128];
  220. pthread_t ntid;
  221. uartFdCreate();
  222. pthread_create(&ntid, NULL, uartrecv, NULL);
  223. while(1)
  224. {
  225. for(i = 0 ; i < 7; i++)
  226. {
  227. if(uartfd[i] != -1)
  228. {
  229. memset(buf,0,sizeof(buf));
  230. sprintf(buf,"this is uart%d send date",i+ 1);
  231. retlen = write(uartfd[i],buf,strlen(buf));
  232. if(retlen != strlen(buf))
  233. {
  234. tcflush(uartfd[i],TCOFLUSH);
  235. }
  236. }
  237. }
  238. sleep(5);
  239. }
  240. }

发表评论

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

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

相关阅读

    相关 imx6ul can调试代码

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