|
|
@@ -2,13 +2,101 @@
|
|
|
|
|
|
#include <string.h>
|
|
|
#include <stdio.h>
|
|
|
+#include <pthread.h>
|
|
|
+#include <stdint.h>
|
|
|
+#include <stdbool.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <signal.h>
|
|
|
|
|
|
-int main()
|
|
|
+volatile bool app_exit = false;
|
|
|
+char write_buffer[256];
|
|
|
+
|
|
|
+void sigint_handler(int signal)
|
|
|
{
|
|
|
+ switch(signal)
|
|
|
+ {
|
|
|
+ case SIGINT:
|
|
|
+ printf("Catch signal CTRL + C\n");
|
|
|
+ app_exit = true;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void pthread_cleanup_handler(void* arg)
|
|
|
+ {
|
|
|
+ printf("exit uart recv thread. %s\n", (char*)arg);
|
|
|
+}
|
|
|
+
|
|
|
+void* uart_recv(void* arg)
|
|
|
+{
|
|
|
+ if(!arg)
|
|
|
+ return NULL;
|
|
|
+ uart_handle_st* handle = (uart_handle_st*)arg;
|
|
|
+ char buffer[1024];
|
|
|
+ char temp[1024];
|
|
|
+ int read_size;
|
|
|
+ int recv_size = 0;
|
|
|
+ int i;
|
|
|
+ bool equal = true;
|
|
|
+
|
|
|
+ // 设置线程取消属性:允许取消,延迟取消
|
|
|
+ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
|
+ pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
|
|
|
+ pthread_cleanup_push(pthread_cleanup_handler, "uart recv");
|
|
|
+
|
|
|
+ while(!app_exit)
|
|
|
+ {
|
|
|
+ read_size = uart_read(handle, temp, sizeof(temp)); // 底层的 read 函数本身就是取消点
|
|
|
+ // printf("uart recv data size: %d\n", read_size);
|
|
|
+ memcpy(buffer + recv_size, temp, read_size);
|
|
|
+ recv_size += read_size;
|
|
|
+
|
|
|
+ if(read_size > 0)
|
|
|
+ {
|
|
|
+ for(i = 0; i < read_size; i++)
|
|
|
+ {
|
|
|
+ printf("0x%02x %03d", (uint8_t)temp[i], (uint8_t)temp[i]);
|
|
|
+ }
|
|
|
+ printf("\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(recv_size == sizeof(write_buffer))
|
|
|
+ {
|
|
|
+ recv_size = 0;
|
|
|
+ for(i = 0; i < sizeof(write_buffer); i++)
|
|
|
+ {
|
|
|
+ if(buffer[i] != write_buffer[i])
|
|
|
+ {
|
|
|
+ equal = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(equal)
|
|
|
+ printf("uart writed and received are equal\n");
|
|
|
+ else
|
|
|
+ printf("uart writed and received are not equal\n");
|
|
|
+ equal = true;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // pthread_testcancel(); // 手动插入取消点
|
|
|
+ }
|
|
|
+
|
|
|
+ pthread_cleanup_pop(1);
|
|
|
+ pthread_exit(NULL);
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+int main(int argc, char* argv[])
|
|
|
+{
|
|
|
+ signal(SIGINT, sigint_handler);
|
|
|
+
|
|
|
uart_handle_st uart;
|
|
|
memset(&uart, 0, sizeof(uart));
|
|
|
-
|
|
|
- uart_handle_set_device(&uart, "/dev/ttyS1");
|
|
|
+ uart_handle_set_device(&uart, "/dev/ttyUSB0");
|
|
|
uart_handle_set_baudrate(&uart, 115200);
|
|
|
uart_handle_set_databits(&uart, 8);
|
|
|
uart_handle_set_stopbits(&uart, 1);
|
|
|
@@ -16,9 +104,34 @@ int main()
|
|
|
uart_handle_set_flow_control(&uart, FLOW_CONTROL_NONE);
|
|
|
uart_handle_set_block(&uart, 1);
|
|
|
|
|
|
+ pthread_t uart_recv_thread;
|
|
|
+
|
|
|
if(uart_handle_open(&uart) < 0)
|
|
|
{
|
|
|
printf("Failed to open UART\n");
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
+ pthread_create(&uart_recv_thread, NULL, uart_recv, &uart);
|
|
|
+ // pthread_detach(uart_recv_thread);
|
|
|
+
|
|
|
+ int write_size;
|
|
|
+ for(int i = 0; i < sizeof(write_buffer); i++)
|
|
|
+ {
|
|
|
+ write_buffer[i] = i;
|
|
|
+ }
|
|
|
+
|
|
|
+ uint8_t data = 0;
|
|
|
+ while(!app_exit)
|
|
|
+ {
|
|
|
+ write_size = uart_write(&uart, &data, 1);
|
|
|
+ // printf("uart write data size: %d\n", write_size);
|
|
|
+ data++;
|
|
|
+ sleep(1);
|
|
|
+ }
|
|
|
+ printf("exit uart send\n");
|
|
|
+ pthread_cancel(uart_recv_thread);
|
|
|
+ pthread_join(uart_recv_thread, NULL);
|
|
|
+ uart_handle_close(&uart);
|
|
|
+ return 0;
|
|
|
}
|