main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "uart.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <pthread.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <unistd.h>
  8. #include <signal.h>
  9. volatile bool app_exit = false;
  10. char write_buffer[256];
  11. void sigint_handler(int signal)
  12. {
  13. switch(signal)
  14. {
  15. case SIGINT:
  16. printf("Catch signal CTRL + C\n");
  17. app_exit = true;
  18. break;
  19. default:
  20. break;
  21. }
  22. }
  23. void pthread_cleanup_handler(void* arg)
  24. {
  25. printf("exit uart recv thread. %s\n", (char*)arg);
  26. }
  27. void* uart_recv(void* arg)
  28. {
  29. if(!arg)
  30. return NULL;
  31. uart_handle_st* handle = (uart_handle_st*)arg;
  32. char buffer[1024];
  33. char temp[1024];
  34. int read_size;
  35. int recv_size = 0;
  36. int i;
  37. bool equal = true;
  38. // 设置线程取消属性:允许取消,延迟取消
  39. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  40. pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  41. pthread_cleanup_push(pthread_cleanup_handler, "uart recv");
  42. while(!app_exit)
  43. {
  44. read_size = uart_read(handle, temp, sizeof(temp)); // 底层的 read 函数本身就是取消点
  45. // printf("uart recv data size: %d\n", read_size);
  46. memcpy(buffer + recv_size, temp, read_size);
  47. recv_size += read_size;
  48. if(read_size > 0)
  49. {
  50. for(i = 0; i < read_size; i++)
  51. {
  52. printf("0x%02x %03d", (uint8_t)temp[i], (uint8_t)temp[i]);
  53. }
  54. printf("\n");
  55. }
  56. if(recv_size == sizeof(write_buffer))
  57. {
  58. recv_size = 0;
  59. for(i = 0; i < sizeof(write_buffer); i++)
  60. {
  61. if(buffer[i] != write_buffer[i])
  62. {
  63. equal = false;
  64. break;
  65. }
  66. }
  67. if(equal)
  68. printf("uart writed and received are equal\n");
  69. else
  70. printf("uart writed and received are not equal\n");
  71. equal = true;
  72. }
  73. // pthread_testcancel(); // 手动插入取消点
  74. }
  75. pthread_cleanup_pop(1);
  76. pthread_exit(NULL);
  77. return NULL;
  78. }
  79. int main(int argc, char* argv[])
  80. {
  81. signal(SIGINT, sigint_handler);
  82. uart_handle_st uart;
  83. memset(&uart, 0, sizeof(uart));
  84. uart_handle_set_device(&uart, "/dev/ttyUSB0");
  85. uart_handle_set_baudrate(&uart, 115200);
  86. uart_handle_set_databits(&uart, 8);
  87. uart_handle_set_stopbits(&uart, 1);
  88. uart_handle_set_parity(&uart, PARITY_NONE);
  89. uart_handle_set_flow_control(&uart, FLOW_CONTROL_NONE);
  90. uart_handle_set_block(&uart, 1);
  91. pthread_t uart_recv_thread;
  92. if(uart_handle_open(&uart) < 0)
  93. {
  94. printf("Failed to open UART\n");
  95. return -1;
  96. }
  97. pthread_create(&uart_recv_thread, NULL, uart_recv, &uart);
  98. // pthread_detach(uart_recv_thread);
  99. int write_size;
  100. for(int i = 0; i < sizeof(write_buffer); i++)
  101. {
  102. write_buffer[i] = i;
  103. }
  104. uint8_t data = 0;
  105. while(!app_exit)
  106. {
  107. write_size = uart_write(&uart, &data, 1);
  108. // printf("uart write data size: %d\n", write_size);
  109. data++;
  110. sleep(1);
  111. }
  112. printf("exit uart send\n");
  113. pthread_cancel(uart_recv_thread);
  114. pthread_join(uart_recv_thread, NULL);
  115. uart_handle_close(&uart);
  116. return 0;
  117. }