You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.1 KiB

2 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <string.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. #include <error.h>
  10. #include <termios.h>
  11. #include <malloc.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. typedef struct termios termios_t;
  15. typedef struct serial_data
  16. {
  17. char databuf[100]; // 发送/接受数据
  18. int serfd; // 串口文件描述符
  19. } ser_Data;
  20. void *sersend(void *arg);
  21. void *serrecv(void *arg);
  22. int main(int argc, char *argv[])
  23. {
  24. pthread_t pid1, pid2;
  25. pthread_attr_t *pthread_arr1, *pthread_arr2;
  26. pthread_arr1 = NULL;
  27. pthread_arr2 = NULL;
  28. int serport1fd;
  29. /* 进行串口参数设置 */
  30. termios_t *ter_s = malloc(sizeof(*ter_s));
  31. serport1fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); // 不成为控制终端程序,不受其他程序输出输出影响
  32. if (serport1fd < 0)
  33. {
  34. printf("%s open faild\r\n", argv[1]);
  35. return -1;
  36. }
  37. bzero(ter_s, sizeof(*ter_s));
  38. ter_s->c_cflag |= CLOCAL | CREAD; // 激活本地连接与接受使能
  39. ter_s->c_cflag &= ~CSIZE; // 失能数据位屏蔽
  40. ter_s->c_cflag |= CS8; // 8位数据位
  41. ter_s->c_cflag &= ~CSTOPB; // 1位停止位
  42. ter_s->c_cflag &= ~PARENB; // 无校验位
  43. ter_s->c_cc[VTIME] = 0;
  44. ter_s->c_cc[VMIN] = 0;
  45. /*1 VMIN> 0 && VTIME> 0
  46. VMIN为最少读取的字符数VMIN个字符read返回VMIN个字符VMIN个字符之前read返回已读取到的字符
  47. 2 VMIN > 0 && VTIME== 0
  48. VMIN个字符时read才返回read被永久阻塞
  49. 3 VMIN == 0 && VTIME> 0
  50. read返回read返回值是0
  51. 4 VMIN == 0 && VTIME== 0
  52. read总是立即就返回----by
  53. */
  54. cfsetispeed(ter_s, B115200); // 设置输入波特率
  55. cfsetospeed(ter_s, B115200); // 设置输出波特率
  56. tcflush(serport1fd, TCIFLUSH); // 刷清未处理的输入和/或输出
  57. if (tcsetattr(serport1fd, TCSANOW, ter_s) != 0)
  58. {
  59. printf("com set error!\r\n");
  60. }
  61. char buffer[] = {"hello my world!\r\n"};
  62. char recvbuf[100] = {};
  63. ser_Data snd_data;
  64. ser_Data rec_data;
  65. snd_data.serfd = serport1fd;
  66. rec_data.serfd = serport1fd;
  67. memcpy(snd_data.databuf, buffer, strlen(buffer)); // 拷贝发送数据
  68. pthread_create(&pid1, pthread_arr1, sersend, (void *)&snd_data);
  69. pthread_create(&pid2, pthread_arr2, serrecv, (void *)&rec_data);
  70. ssize_t sizec;
  71. while (1)
  72. {
  73. usleep(100000);
  74. }
  75. pthread_join(pid1, NULL);
  76. pthread_join(pid2, NULL);
  77. free(ter_s);
  78. return 0;
  79. }
  80. void *sersend(void *arg) // 串口发送线程函数
  81. {
  82. ser_Data *snd = (ser_Data *)arg;
  83. int ret;
  84. while (1)
  85. {
  86. ret = write(snd->serfd, snd->databuf, strlen(snd->databuf));
  87. if (ret > 0)
  88. {
  89. printf("send success, data is %s\r\n", snd->databuf);
  90. }
  91. else
  92. {
  93. printf("send error!\r\n");
  94. }
  95. usleep(300000);
  96. /*
  97. if()
  98. break;//退出
  99. */
  100. }
  101. }
  102. void *serrecv(void *arg) // 串口发送线程函数
  103. {
  104. ser_Data *rec = (ser_Data *)arg;
  105. int ret;
  106. while (1)
  107. {
  108. ret = read(rec->serfd, rec->databuf, 1024);
  109. if (ret > 0)
  110. {
  111. printf("recv success,recv size is %d,data is %s\r\n", ret, rec->databuf);
  112. }
  113. else
  114. {
  115. /*
  116. */
  117. }
  118. usleep(1000);
  119. /*
  120. if()
  121. break;//退出
  122. */
  123. }
  124. }