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.

139 lines
3.9 KiB

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