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.

80 lines
1.7 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
  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. #include "uart.hpp"
  14. using namespace iflytop;
  15. class Uart g_uart;
  16. int g_rec_len;
  17. void *thread_uart_send(void *arg) {
  18. while (1) {
  19. if (g_rec_len > 0) {
  20. g_uart.send((char *)arg, g_rec_len);
  21. g_rec_len = 0;
  22. }
  23. usleep(1000);
  24. }
  25. }
  26. void *thread_uart_receive(void *arg) {
  27. while (1) {
  28. if (g_rec_len == 0) {
  29. g_rec_len = g_uart.receive((char *)arg, 128);
  30. }
  31. usleep(1000);
  32. }
  33. }
  34. int main(int argc, char *argv[]) {
  35. char share_buffer[128];
  36. pthread_t pid1, pid2;
  37. pthread_attr_t *pthread_arr1, *pthread_arr2;
  38. pthread_arr1 = NULL;
  39. pthread_arr2 = NULL;
  40. if (argc != 3) {
  41. cout << "input error,please input ttyS and baudrate" << endl;
  42. return -1;
  43. }
  44. if (g_uart.open(argv[1], argv[2]) < 0) {
  45. cout << "open uart error" << endl;
  46. return -2;
  47. } else {
  48. cout << "open uart success" << endl;
  49. }
  50. if (pthread_create(&pid1, pthread_arr1, thread_uart_send, (void *)&share_buffer) != 0) {
  51. cout << "create uart send thread error" << endl;
  52. return -3;
  53. } else {
  54. cout << "create uart send thread success" << endl;
  55. }
  56. if (pthread_create(&pid2, pthread_arr2, thread_uart_receive, (void *)&share_buffer) != 0) {
  57. cout << "create uart receive thread error" << endl;
  58. return -4;
  59. } else {
  60. cout << "create uart receive thread success" << endl;
  61. }
  62. while (1) {
  63. usleep(100000);
  64. }
  65. pthread_join(pid1, NULL);
  66. pthread_join(pid2, NULL);
  67. return 0;
  68. }