修改串口程序为多线程收发

Change-Id: Ib045a91886c7cfb7716a75ff0a8429128bff2f3a
diff --git a/mbtk/test/ql_uart_test.c b/mbtk/test/ql_uart_test.c
index 3936ef6..2bef6ac 100755
--- a/mbtk/test/ql_uart_test.c
+++ b/mbtk/test/ql_uart_test.c
@@ -1,21 +1,20 @@
 #include <stdio.h>
 #include <errno.h>
-
+#include <stdlib.h>
+#include <pthread.h>
 #include "ql/ql_uart.h"
 
+int fd;
+pthread_mutex_t mutexsum;	//创建mutex锁
 
-int main(int argc, char *argv[])
-{
-    if(argc != 3) {
-        printf("./uart_test <dev> <baudrate>\n");
-        return -1;
-    }
+typedef struct {
+    char *node;
+    char *rate;
+} thread_arg;
 
-    int fd = Ql_UART_Open(argv[1], (Enum_BaudRate)atoi(argv[2]), FC_NONE);
-    if(fd < 0) {
-        printf("Ql_UART_Open() fail.\n");
-        return -1;
-    }
+void *read_function(void *arg) {
+    thread_arg *args = (thread_arg *)arg;
+    printf("read_function() node = %s rate = %s\n", args->node, args->rate);
 
     ST_UARTDCB dcb;
     memset(&dcb, 0x0, sizeof(ST_UARTDCB));
@@ -31,6 +30,7 @@
     char buff[1024];
     int len;
     while(1) {
+        // pthread_mutex_lock(&mutexsum);
         memset(buff, 0x0 ,1024);
         len = Ql_UART_Read(fd, buff, 1024);
         if(len > 0) {
@@ -40,17 +40,57 @@
             } else {
                 printf("<%s\n", buff);
 
-                Ql_UART_Write(fd, "OK\r\n", 4);
+                Ql_UART_Write(fd, "Module received data!\r\n", 23);
             }
-        } else {
-            printf("Ql_UART_Read() fail:%d, errno = %d\n", len, errno);
         }
+        // pthread_mutex_unlock(&mutexsum);
     }
     Ql_UART_Close(fd);
-
-    printf("exit!!!\n");
-
-    return 0;
+    return NULL;
 }
 
+void *write_function(void *arg) {
+    char str[20] = "write success!\r\n";
+    while (1)
+    {
+        sleep(3);
+        // pthread_mutex_lock(&mutexsum);
+        Ql_UART_Write(fd, str, strlen(str));
+        // pthread_mutex_unlock(&mutexsum);
+    }
+    Ql_UART_Close(fd);
+    return NULL;
+}
 
+int main(int argc, char *argv[])
+{
+    printf("main() start \n");
+    mbtk_log_init("radio", "MBTK_UART");
+    if(argc != 3) {
+        printf("./uart_test <dev> <baudrate>\n");
+        return -1;
+    }
+
+    printf("main() start  node = %s rate = %s\n", argv[1], argv[2]);
+    fd = Ql_UART_Open(argv[1], (Enum_BaudRate)atoi(argv[2]), FC_NONE);
+    if(fd < 0) {
+        printf("Ql_UART_Open() fail.\n");
+        return -1;
+    }
+
+    // pthread_mutex_init(&mutexsum, NULL);
+    pthread_t read_thread, write_thread;
+
+    thread_arg args = {argv[1], argv[2]};
+    printf("start read_thread node = %s rate = %s\n", args.node, args.rate);
+    pthread_create(&read_thread, NULL, read_function, &args);
+
+    printf("start write_thread \n");
+    pthread_create(&write_thread, NULL, write_function, NULL);
+
+    pthread_join(read_thread, NULL);
+    pthread_join(write_thread, NULL);
+
+    printf("main() end \n");
+    return 0;
+}
\ No newline at end of file