作者 潘浩彬

更新 JZsdk_Uart_UartDeal.c

#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>
#include "Uart_Config.h"
#include "JZsdk_Uart_Recv/JZsdk_Uart_RecvDeal.h"
#include "JZsdkLib.h"
static int Uart_4G_fd;
static int Uart_DEV1_fd;
static int Uart_DEV2_fd;
static void *UartDeal_rece(void *arg);
/*********************
*
* 串口接收线程
*
* *****************/
/******************************************************************
创建串口接收线程
******************************************************************/
int JZsdk_Uart_UartDeal_Receive(int Uart_fd, int Uart_Dev_name)
{
int ret = 0;
pthread_t Uart_rece_task;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
if (Uart_Dev_name == UART_DEV_1)
{
Uart_DEV1_fd = Uart_fd;
}
else if (Uart_Dev_name == UART_DEV_2)
{
Uart_DEV2_fd = Uart_fd;
}
else if (Uart_Dev_name == UART_4G)
{
Uart_4G_fd = Uart_fd;
}
int* uart_fd_ptr = malloc(sizeof(int)); // 动态分配内存来存储 Uart_fd 变量
*uart_fd_ptr = Uart_fd;
ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,uart_fd_ptr); //串口接收线程
if(ret != 0)
{
printf("创建串口%x 接收线程失败!\n",Uart_Dev_name);
free(uart_fd_ptr);
}
else{
printf("创建串口%x 接收线程成功!\n",Uart_Dev_name);
}
}
static int UartDeal_Recv_interface(int Uart_fd, unsigned char *getbuf, int len)
{
int i = 0;
int Frame_len = 0;
if (Uart_fd == Uart_DEV1_fd)
{
USER_LOG_INFO("串口-设备1号,接受到数据len: %d", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
else if (Uart_fd == Uart_DEV2_fd)
{
USER_LOG_INFO("串口-设备2号,接受到数据len: %d ", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
else if (Uart_fd == Uart_4G_fd)
{
USER_LOG_INFO("串口-设备4G,接受到数据len: %d ", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
}
static void *UartDeal_rece(void *arg)
{
unsigned char getbuf[1024];
int ret = 0;
fd_set fs_read;
struct timeval tv_timeout;
int Uart_fd = *(int*) arg;
//FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化
//如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。
FD_ZERO(&fs_read);
//FD_SET 用于在文件描述符集合中增加一个新的文件描述符。
FD_SET(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(Uart_fd+1, &fs_read, NULL, NULL, &tv_timeout);
delayMs(10);
//FD_ISSET 用于测试指定的文件描述符是否在该集合中。
//Uart_fd 是否在fsread中
if (FD_ISSET(Uart_fd, &fs_read))
{
//1、读取串口内容 ret 接收长度 getbuf 获取的字符
memset(getbuf,0,sizeof(getbuf)); //清空接收数组
ret = read(Uart_fd,getbuf,sizeof(getbuf));
USER_LOG_INFO("RecvLen:%d\n",ret);
//UartDeal_Recv_interface(Uart_fd, getbuf, ret);
}
}
free(arg);
}
/****************
*
*
* 发送函数
*
* ****************/
int JZsdk_Uart_UartSend(int UartPort ,unsigned char *send, int num)
{
if (UartPort == UART_4G)
{
write(Uart_4G_fd,send, num);
return 0;
}
else if (UartPort == UART_DEV_1)
{
write(Uart_DEV1_fd,send, num);
return 0;
}
else if (UartPort == UART_DEV_2)
{
write(Uart_DEV2_fd,send, num);
return 0;
}
}
#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>
#include "Uart_Config.h"
#include "JZsdk_Uart_Recv/JZsdk_Uart_RecvDeal.h"
#include "JZsdkLib.h"
static int Uart_4G_fd;
static int Uart_DEV1_fd;
static int Uart_DEV2_fd;
static void *UartDeal_rece(void *arg);
/*********************
*
* 串口接收线程
*
* *****************/
/******************************************************************
创建串口接收线程
******************************************************************/
int JZsdk_Uart_UartDeal_Receive(int Uart_fd, int Uart_Dev_name)
{
int ret = 0;
pthread_t Uart_rece_task;
pthread_attr_t task_attribute; //线程属性
pthread_attr_init(&task_attribute); //初始化线程属性
pthread_attr_setdetachstate(&task_attribute, PTHREAD_CREATE_DETACHED); //设置线程属性
if (Uart_Dev_name == UART_DEV_1)
{
Uart_DEV1_fd = Uart_fd;
}
else if (Uart_Dev_name == UART_DEV_2)
{
Uart_DEV2_fd = Uart_fd;
}
else if (Uart_Dev_name == UART_4G)
{
Uart_4G_fd = Uart_fd;
}
int* uart_fd_ptr = malloc(sizeof(int)); // 动态分配内存来存储 Uart_fd 变量
*uart_fd_ptr = Uart_fd;
ret = pthread_create(&Uart_rece_task,&task_attribute,UartDeal_rece,uart_fd_ptr); //串口接收线程
if(ret != 0)
{
printf("创建串口%x 接收线程失败!\n",Uart_Dev_name);
free(uart_fd_ptr);
}
else{
printf("创建串口%x 接收线程成功!\n",Uart_Dev_name);
}
}
static int UartDeal_Recv_interface(int Uart_fd, unsigned char *getbuf, int len)
{
int i = 0;
int Frame_len = 0;
if (Uart_fd == Uart_DEV1_fd)
{
USER_LOG_INFO("串口-设备1号,接受到数据len: %d", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV1(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
else if (Uart_fd == Uart_DEV2_fd)
{
USER_LOG_INFO("串口-设备2号,接受到数据len: %d ", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_DEV2(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
else if (Uart_fd == Uart_4G_fd)
{
USER_LOG_INFO("串口-设备4G,接受到数据len: %d ", len);
printf("getbuf: %s\n", getbuf);
for (int i = 0; i < len; i++)
{
printf("%x ",getbuf[i]);
}
printf("\n");
do
{
if ( (getbuf[i] == 0x5A && getbuf[i+1] == 0x5A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x5B && getbuf[i+1] == 0x5B &&getbuf[i+2] == 0x77)
|| (getbuf[i] == 0x6A && getbuf[i+1] == 0x6A &&getbuf[i+2] == 0x77) || (getbuf[i] == 0x6B && getbuf[i+1] == 0x6B &&getbuf[i+2] == 0x77))
{
Frame_len = (getbuf[3] << 8) + getbuf[4];
JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], Frame_len);
i = i+Frame_len;
len = len - Frame_len;
Frame_len = 0;
}
else
{
JZsdk_Uart_RecvDeal_CharmDeal_Uart_4G(&getbuf[i], len);
len = 0;
}
}
while (len >= 12);
}
}
static void *UartDeal_rece(void *arg)
{
unsigned char getbuf[1024];
int ret = 0;
fd_set fs_read;
struct timeval tv_timeout;
int Uart_fd = *(int*) arg;
//FD_ZERO 将指定的文件描述符集清空,在对文件描述符集合进行设置前,必须对其进行初始化
//如果不清空,由于在系统分配内存空间后,通常并不作清空处理,所以结果是不可知的。
FD_ZERO(&fs_read);
//FD_SET 用于在文件描述符集合中增加一个新的文件描述符。
FD_SET(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(Uart_fd+1, &fs_read, NULL, NULL, &tv_timeout);
delayMs(10);
//FD_ISSET 用于测试指定的文件描述符是否在该集合中。
//Uart_fd 是否在fsread中
if (FD_ISSET(Uart_fd, &fs_read))
{
//1、读取串口内容 ret 接收长度 getbuf 获取的字符
memset(getbuf,0,sizeof(getbuf)); //清空接收数组
ret = read(Uart_fd,getbuf,sizeof(getbuf));
UartDeal_Recv_interface(Uart_fd, getbuf, ret);
}
}
free(arg);
}
/****************
*
*
* 发送函数
*
* ****************/
int JZsdk_Uart_UartSend(int UartPort ,unsigned char *send, int num)
{
if (UartPort == UART_4G)
{
write(Uart_4G_fd,send, num);
return 0;
}
else if (UartPort == UART_DEV_1)
{
write(Uart_DEV1_fd,send, num);
return 0;
}
else if (UartPort == UART_DEV_2)
{
write(Uart_DEV2_fd,send, num);
return 0;
}
}
... ...