Gimbal_UartDeal.c
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
static void *UartDeal_rece(void *arg);
static void *UartDeal_send(void *arg);
static int Gimbal_Uart_fd = 0;
/******************************************************************
创建一个接收Gimbal的接收线程
******************************************************************/
int Gimbal_UartDeal_Receive(int Uart_fd)
{
int ret = 0;
pthread_t Uart_rece_task;
Gimbal_Uart_fd = Uart_fd;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,NULL); //串口接收线程
if(ret != 0)
{
printf("创建展架串口接收线程失败!\n");
}
else{
printf("创建展架串口接收线程成功!\n");
}
}
static void *UartDeal_rece(void *arg)
{
char getbuf[1024];
int ret = 0;
fd_set fs_read;
struct timeval tv_timeout;
//FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化
//如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。
FD_ZERO(&fs_read);
//FD_SET 用于在文件描述符集合中增加一个新的文件描述符。
FD_SET(Gimbal_Uart_fd, &fs_read);
//115200 / char 8 位 = 14400 个char数据
tv_timeout.tv_sec = 6000;//(10*20/115200+2);
tv_timeout.tv_usec = 0;
//2、正常接收
while(1)
{
//检查fs_read套节字是否有数据
select(Gimbal_Uart_fd+1, &fs_read, NULL, NULL, &tv_timeout);
usleep(10000);
//FD_ISSET 用于测试指定的文件描述符是否在该集合中。
//Gimbal_Uart_fd 是否在fsread中
if (FD_ISSET(Gimbal_Uart_fd, &fs_read))
{
//1、读取串口内容 ret 接收长度 getbuf 获取的字符
memset(getbuf,0,sizeof(getbuf)); //清空接收数组
ret = read(Gimbal_Uart_fd,getbuf,sizeof(getbuf));
//printf("Gimbalret: %d Gimbalgetbuf: %s\n", ret, getbuf);
// for (int i = 0; i < ret; i++)
// {
// printf("%x ",getbuf[i]);
// }
// printf("\n");
//Comm_4G_RecvCharmDeal(getbuf, ret);
}
}
}
/****************
*
*
* 发送函数
*
* ****************/
int Gimbal_UartSend(unsigned char *send, int num)
{
write(Gimbal_Uart_fd,send, num);
return 0;
}