liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <errno.h> |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <pthread.h> |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 5 | #include "ql/ql_uart.h" |
| 6 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 7 | int fd; |
| 8 | pthread_mutex_t mutexsum; //创建mutex锁 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 9 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 10 | typedef struct { |
| 11 | char *node; |
| 12 | char *rate; |
| 13 | } thread_arg; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 14 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 15 | void *read_function(void *arg) { |
| 16 | thread_arg *args = (thread_arg *)arg; |
| 17 | printf("read_function() node = %s rate = %s\n", args->node, args->rate); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 18 | |
| 19 | ST_UARTDCB dcb; |
| 20 | memset(&dcb, 0x0, sizeof(ST_UARTDCB)); |
| 21 | dcb.databit = DB_CS8; |
| 22 | dcb.parity = PB_NONE; |
| 23 | dcb.flowctrl = FC_NONE; |
| 24 | |
| 25 | if(Ql_UART_SetDCB(fd, &dcb)) { |
| 26 | printf("Ql_UART_SetDCB() fail.\n"); |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | char buff[1024]; |
| 31 | int len; |
| 32 | while(1) { |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 33 | // pthread_mutex_lock(&mutexsum); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 34 | memset(buff, 0x0 ,1024); |
| 35 | len = Ql_UART_Read(fd, buff, 1024); |
| 36 | if(len > 0) { |
| 37 | if(memcmp(buff, "exit", 4) == 0) { |
| 38 | Ql_UART_Write(fd, "exit\r\n", 6); |
| 39 | break; |
| 40 | } else { |
| 41 | printf("<%s\n", buff); |
| 42 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 43 | Ql_UART_Write(fd, "Module received data!\r\n", 23); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 44 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 45 | } |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 46 | // pthread_mutex_unlock(&mutexsum); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 47 | } |
| 48 | Ql_UART_Close(fd); |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 49 | return NULL; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 50 | } |
| 51 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 52 | void *write_function(void *arg) { |
| 53 | char str[20] = "write success!\r\n"; |
| 54 | while (1) |
| 55 | { |
| 56 | sleep(3); |
| 57 | // pthread_mutex_lock(&mutexsum); |
| 58 | Ql_UART_Write(fd, str, strlen(str)); |
| 59 | // pthread_mutex_unlock(&mutexsum); |
| 60 | } |
| 61 | Ql_UART_Close(fd); |
| 62 | return NULL; |
| 63 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 64 | |
xl.meng | 7637ba9 | 2023-10-11 18:38:19 +0800 | [diff] [blame] | 65 | int main(int argc, char *argv[]) |
| 66 | { |
| 67 | printf("main() start \n"); |
| 68 | mbtk_log_init("radio", "MBTK_UART"); |
| 69 | if(argc != 3) { |
| 70 | printf("./uart_test <dev> <baudrate>\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | printf("main() start node = %s rate = %s\n", argv[1], argv[2]); |
| 75 | fd = Ql_UART_Open(argv[1], (Enum_BaudRate)atoi(argv[2]), FC_NONE); |
| 76 | if(fd < 0) { |
| 77 | printf("Ql_UART_Open() fail.\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | // pthread_mutex_init(&mutexsum, NULL); |
| 82 | pthread_t read_thread, write_thread; |
| 83 | |
| 84 | thread_arg args = {argv[1], argv[2]}; |
| 85 | printf("start read_thread node = %s rate = %s\n", args.node, args.rate); |
| 86 | pthread_create(&read_thread, NULL, read_function, &args); |
| 87 | |
| 88 | printf("start write_thread \n"); |
| 89 | pthread_create(&write_thread, NULL, write_function, NULL); |
| 90 | |
| 91 | pthread_join(read_thread, NULL); |
| 92 | pthread_join(write_thread, NULL); |
| 93 | |
| 94 | printf("main() end \n"); |
| 95 | return 0; |
| 96 | } |