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.

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