• Linux嵌入式串口UART测试程序


    Linux串口UART测试程序,收到什么,打印什么。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #define FALSE -1
    11. #define TRUE 0
    12. #define flag 1
    13. #define noflag 0
    14. int wait_flag = noflag;
    15. int STOP = 0;
    16. int res;
    17. int speed_arr[] =
    18. { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600,
    19. B4800, B2400, B1200, B300,B115200 };
    20. int name_arr[] =
    21. { 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400,
    22. 1200, 300, 115200,};
    23. void
    24. set_speed (int fd, int speed)
    25. {
    26. int i;
    27. int status;
    28. struct termios Opt;
    29. tcgetattr (fd, &Opt);
    30. for (i = 0; i < sizeof (speed_arr) / sizeof (int); i++)
    31. {
    32. if (speed == name_arr[i])
    33. if (speed == name_arr[i])
    34. {
    35. tcflush (fd, TCIOFLUSH);
    36. cfsetispeed (&Opt, speed_arr[i]);
    37. cfsetospeed (&Opt, speed_arr[i]);
    38. status = tcsetattr (fd, TCSANOW, &Opt);
    39. if (status != 0)
    40. {
    41. perror ("tcsetattr fd1");
    42. return;
    43. }
    44. tcflush (fd, TCIOFLUSH);
    45. }
    46. }
    47. }
    48. int
    49. set_Parity (int fd, int databits, int stopbits, int parity)
    50. {
    51. struct termios options;
    52. if (tcgetattr (fd, &options) != 0)
    53. {
    54. perror ("SetupSerial 1");
    55. return (FALSE);
    56. }
    57. options.c_cflag &= ~CSIZE;
    58. options.c_lflag &= ~ECHO; //这个是重点!!!!!!!! 关闭回显功能
    59. switch (databits)
    60. {
    61. case 7:
    62. options.c_cflag |= CS7;
    63. break;
    64. case 8:
    65. options.c_cflag |= CS8;
    66. break;
    67. default:
    68. fprintf (stderr, "Unsupported data size\n");
    69. return (FALSE);
    70. }
    71. switch (parity)
    72. {
    73. case 'n':
    74. case 'N':
    75. options.c_cflag &= ~PARENB; /* Clear parity enable */
    76. options.c_iflag &= ~INPCK; /* Enable parity checking */
    77. break;
    78. case 'o':
    79. case 'O':
    80. options.c_cflag |= (PARODD | PARENB);
    81. options.c_iflag |= INPCK; /* Disnable parity checking */
    82. break;
    83. case 'e':
    84. case 'E':
    85. options.c_cflag |= PARENB; /* Enable parity */
    86. options.c_cflag &= ~PARODD;
    87. options.c_iflag |= INPCK; /* Disnable parity checking */
    88. break;
    89. case 'S':
    90. case 's': /*as no parity */
    91. options.c_cflag &= ~PARENB;
    92. options.c_cflag &= ~CSTOPB;
    93. break;
    94. default:
    95. fprintf (stderr, "Unsupported parity\n");
    96. return (FALSE);
    97. }
    98. switch (stopbits)
    99. {
    100. case 1:
    101. options.c_cflag &= ~CSTOPB;
    102. break;
    103. case 2:
    104. options.c_cflag |= CSTOPB;
    105. break;
    106. default:
    107. fprintf (stderr, "Unsupported stop bits\n");
    108. return (FALSE);
    109. }
    110. /* Set input parity option */
    111. if (parity != 'n')
    112. options.c_iflag |= INPCK;
    113. tcflush (fd, TCIFLUSH);
    114. options.c_cc[VTIME] = 150;
    115. options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
    116. if (tcsetattr (fd, TCSANOW, &options) != 0)
    117. {
    118. perror ("SetupSerial 3");
    119. return (FALSE);
    120. }
    121. return (TRUE);
    122. }
    123. void
    124. signal_handler_IO (int status)
    125. {
    126. printf ("received SIGIO signale.\n");
    127. wait_flag = noflag;
    128. }
    129. void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
    130. {
    131. char h1,h2;
    132. unsigned char s1,s2;
    133. int i;
    134. for (i=0; i
    135. {
    136. h1 = pbSrc[2*i];
    137. h2 = pbSrc[2*i+1];
    138. s1 = toupper(h1) - 0x30;
    139. if (s1 > 9)
    140. s1 -= 7;
    141. s2 = toupper(h2) - 0x30;
    142. if (s2 > 9)
    143. s2 -= 7;
    144. pbDest[i] = s1*16 + s2;
    145. }
    146. }
    147. void print_use()
    148. {
    149. printf("*************Send hex data to uart and waitting timeout recv data************************ \n");
    150. printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
    151. printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
    152. printf("uart-test -d [/dev/ttyS0] -b [Baudrate 115200] -s [hex data eg:AA550102] \n");
    153. printf("******************************************************************* \n");
    154. }
    155. int main (int argc, char* argv[])
    156. {
    157. int i=0;
    158. int j=0;
    159. printf ("This program updates last time at %s %s\n", __TIME__, __DATE__);
    160. printf ("TEST FOR ZTJY IOT HW \n");
    161. print_use();
    162. if(argc != 7)
    163. {
    164. print_use();
    165. return -1;
    166. }
    167. int fd;
    168. // fd = open (argv[2], O_RDWR|);
    169. fd = open (argv[2], O_RDWR|O_NOCTTY|O_NONBLOCK); //注意这里的标志位 是重点
    170. if (fd == -1)
    171. {
    172. perror ("serialport open error\n");
    173. return -1;
    174. }
    175. else
    176. {
    177. printf ("open ");
    178. printf ("%s", argv[2]);
    179. printf (" succesfully\n");
    180. }
    181. set_speed (fd, atoi(argv[4]));
    182. if (set_Parity (fd, 8, 1, 'N') == FALSE)
    183. {
    184. printf ("Set Parity Error\n");
    185. exit (0);
    186. }
    187. unsigned char sendData[1024]={0};
    188. StrToHex(sendData,argv[6],strlen(argv[6])/2);
    189. write(fd,sendData,strlen(sendData));
    190. //fflush();
    191. printf("Send Data: \n");
    192. for(i=0;i<strlen(sendData);i++)
    193. {
    194. printf("-0x%2x-",sendData[i]);
    195. }
    196. printf("\n");
    197. char buf[2048];
    198. fd_set rd;
    199. int nread = 0;
    200. while(1)
    201. {
    202. FD_ZERO(&rd);
    203. FD_SET(fd, &rd);
    204. while(FD_ISSET(fd, &rd))
    205. {
    206. if(select(fd+1, &rd, NULL,NULL,NULL) < 0)
    207. {
    208. perror("select error\n");
    209. }
    210. else
    211. {
    212. while((nread = read(fd, buf, sizeof(buf))) > 0)
    213. {
    214. printf("nread = %d,%s\n",nread, buf);
    215. for(j=0;j
    216. {
    217. printf("-0x%2x-",buf[j]);
    218. }
    219. printf("\n");
    220. printf("\n");
    221. printf("\n");
    222. memset(buf, 0 , sizeof(buf));
    223. printf("Exit uart-teset App\r\n");
    224. break;
    225. }
    226. }
    227. }
    228. }
    229. close (fd);
    230. printf("Exit uart-teset App");
    231. return 0;
    232. }

  • 相关阅读:
    Python21天学习挑战赛Day(9)·操作MongoDB数据库
    【学习】如何高效地进行集成测试
    Python自学之路--002:Python 如何生成exe可执行文件
    JMeter压测如何分配业务比例?
    常用的 Spring Boot 注解及其作用
    LeetCoded贪心算法系列——455.分发饼干
    Elasticsearch 安装踩坑!
    智能安防系统-视频监控系统
    二.maven常用功能点
    分割常用损失函数
  • 原文地址:https://blog.csdn.net/hjj651471519/article/details/133425679