1 /*
2 * Copyright (c) 2022 Unionman Technology Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <pthread.h>
23 #include "serial_uart.h"
24
25 static int fd;
26
27 // 从串口读的线程
_serial_input_task(void)28 void *_serial_input_task(void)
29 {
30 int i = 0;
31 int ret = ERR;
32 int buf = 0;
33 int recv[FRAME_LEN] = {0};
34
35 printf("Gesture Sensor Ready!\n");
36 while (1) {
37 for (i = 0; i < FRAME_LEN; i++) {
38 ret = read(fd, &buf, 1);
39 if (ret == ERR) {
40 perror("read error\n");
41 exit(0);
42 }
43 recv[i] = buf;
44 }
45 ret = data_proce(recv);
46 if (ret == ERR) {
47 perror("data process error\n");
48 exit(0);
49 }
50 }
51 }
52
main(int argc,char ** argv)53 int main(int argc, char **argv)
54 {
55 char *uart_dev = UART_TTL_NAME;
56 int ret = ERR;
57
58 fd = open(uart_dev, O_RDWR);
59 if (fd == ERR) {
60 perror("open file fail\n");
61 return ERR;
62 }
63 ret = uart_init(fd, 9600L);
64 if (ret == ERR) {
65 perror("uart init error\n");
66 return ERR;
67 }
68
69 // 创建线程 一直执行读串口的操作
70 pthread_t pid_t;
71 pthread_create(&pid_t, NULL, (void *)_serial_input_task, 0);
72
73 while (1) {
74 sleep(10L);
75 }
76 close(fd);
77
78 return 0;
79 }
80