Gimbal_UartDeal.c 2.6 KB
#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;
}