中级会员
- 积分
- 328
- 金钱
- 328
- 注册时间
- 2019-6-21
- 在线时间
- 102 小时
|
30金钱
#define _GNU_SOURCE //在源文件开头定义_GNU_SOURCE 宏
//串口相关的头文件
#include<stdio.h> /*标准输入输出定义*/
#include<stdlib.h> /*标准函数库定义*/
#include<unistd.h> /*Unix 标准函数定义*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> /*文件控制定义*/
#include<termios.h> /*PPSIX 终端控制定义*/
#include<errno.h> /*错误号定义*/
#include<string.h>
#include<sys/time.h>
#include<assert.h>
#include"uart_val.h" /*自定义头文件*/
/* READ 方式 */
#include <sys/select.h>
#include<pthread.h>
#define test
static struct serport_info uart_val[2]={{9600,8,1,'N',1,1},{9600,8,1,'O',1,1}};
static DEV_Serial bat_init={ 0,BATTERY_UART3,BATTERY_ADD,SING_READ,BETTARY_A,NULL,0x04,
static DEV_Serial lmo_init={ 0,LMOTOR_UART4,LEFWHEEL_ADD,SING_SET,MOTOR_STOP_MOD,NULL,0
static DEV_Serial rmo_init={ 0,RMOTOR_UART5, RIGWHEEL_ADD, SING_SET,MOTOR_STOP_MOD,NULL
static char control[8] = {0x03,0x03,0x00,0x43,0x00,0x01,0x74,0x3c};
static INT32 fdr;
static INT32 ExitThreadFlag=0;
//static ptDEV_serial w_data;
INT32 ReceiveThreadFunc(void *arg);
/* 转换数据函数*/
static INT32 datatocontrol(INT8 *data,DEV_Serial contrl_data)
ptDEV_serial uart_init(INT8 *DEV_PORT)
{
int fd,ret,res;
pthread_attr_t rea_attr;
pthread_t rea_tid;
static ptDEV_serial dev_parat;
if (strcmp(DEV_PORT,BATTERY_UART3) == 0)
{
dev_parat = &bat_init;
fd = open(BATTERY_UART3 ,O_RDWR|O_NOCTTY|O_NONBLOCK);
if(fd < 0) {
printf("can't open BATTERY_UART3 file %d\r\n", fd);
dev_parat->flags = SERIAL_ERROR_OPEN;
}
dev_parat->fd = fd;
ret = uart_Setting(fd,&uart_val[0]);
}else if(strcmp(DEV_PORT,LMOTOR_UART4) == 0)
{
dev_parat = &lmo_init;
fd = open(LMOTOR_UART4 ,O_RDWR|O_NOCTTY|O_NONBLOCK);
if(fd < 0) {
printf("can't open LMOTOR_UART4 file %d\r\n", fd);
dev_parat->flags = SERIAL_ERROR_OPEN;
}
dev_parat->fd = fd;
ret = uart_Setting(fd,&uart_val[1]);
}else if(strcmp(DEV_PORT,RMOTOR_UART5) == 0)
{
dev_parat = &rmo_init;
fd = open(RMOTOR_UART5 ,O_RDWR|O_NOCTTY|O_NONBLOCK);
if(fd < 0) {
printf("can't open file %d\r\n", fd);
dev_parat->flags = SERIAL_ERROR_OPEN;
}
dev_parat->fd = fd;
ret = uart_Setting(fd,&uart_val[1]);
}else{
printf("Uart Init is fail!!!");
dev_parat->flags = SERIAL_ERROR_SETTING;
}
if(ret != 0)
{
printf("setting error!\r\n");
close(fd);
dev_parat->flags = SERIAL_ERROR_SETTING;
}
else {
printf("setting success!\r\n");
}
#if 1 /* 测试代码 */
/* 创建串口的接收线程 */
res = pthread_attr_init(&rea_attr);
if( res!=0 )
{
printf("Create attribute failed\n" );
}
/* 设置线程绑定属性 */
res = pthread_attr_setscope( &rea_attr, PTHREAD_SCOPE_SYSTEM );
/* 设置线程分离属性 */
res += pthread_attr_setdetachstate( &rea_attr, PTHREAD_CREATE_DETACHED );
sleep(1);
if( res!=0 )
{
printf( "Setting attribute failed\n" );
}
printf("read:val:%d,flag:%d\r\n",dev_parat->fd,dev_parat->flags);
res = pthread_create( &rea_tid, &rea_attr, (void *(*) (void *))&ReceiveThreadFunc,
if( res!=0 )
{
uart_close(fd);
dev_parat->flags = SERIAL_INVALID_RESOURCE;
}
printf("threads!!!!1:%d\r\n",dev_parat->fd);
pthread_attr_destroy( &rea_attr );
dev_parat->flags =UART_TRUE;
#endif /* 测试代码 */
return (ptDEV_serial )dev_parat;
}
#if 1
INT32 ReceiveThreadFunc(void *arg)
{
ptDEV_serial read_val =(ptDEV_serial)arg;
/* 定义读事件集合 */
fd_set fs_read;
struct timeval atime;
INT32 flag;
INT8 length;
INT8 r_data[64]={0x00};
while(1)
{
if(ExitThreadFlag)
{
break;
}
/*将读文件描述符加入描述符集合*/
FD_ZERO(&fs_read);
FD_SET(read_val->fd, &fs_read);
/*设置超时为10s*/
atime.tv_sec = 10;
atime.tv_usec = 0;
printf("point:%d,%,arg:%d,num:%d%",read_val->fd,((struct dev_data *)arg)->fd,re
/* 实现多路IO */
flag = select(read_val->fd + 1, &fs_read, NULL, NULL, &atime);
printf("flag:%d\r\n",flag);
if(flag < 0)
{
uart_threads_close(read_val->fd);
break;
}
if(flag > 0)
{
printf("threads!!!!5\r\n");
if(FD_ISSET(read_val->fd, &fs_read))
{
length = read(read_val->fd,r_data ,sizeof(r_data));
printf("read !!!!!\r\n");
#ifdef test
int i;
for(i=0;i<length;i++)
printf("data:[%d]:0x%x",i,r_data);
#endif
if(length > 0)
{
/* 处理函数 */
}
}
}
}
printf("ReceiveThreadFunc finished\n");
pthread_exit(NULL);
//close(read_val->fd);
return UART_TRUE;
}
#endif
ptDEV_serial dev_uart_send(ptDEV_serial w_parat,const DEV_MOD mod,UNI16 reg,UNI8 num,IN
{
INT8 set_data[32]={0x00};
INT8 r_data[32]={0x00};
INT32 len,res;
INT32 fd;
/*static ptDEV_serial w_parat;
//w_parat->fd = uart_init(DEV_PORT);
if (strcmp(DEV_PORT,BATTERY_UART3) == 0)
{
w_parat= &bat_init;
}else if(strcmp(DEV_PORT,LMOTOR_UART4) == 0)
{
w_parat = &lmo_init;
}else if(strcmp(DEV_PORT,RMOTOR_UART5) == 0)
{
w_parat = &rmo_init;
}else{
printf("Init is fail!!!");
w_parat->flags = SERIAL_ERROR_SETTING;
}*/
w_parat->s_mode = mod;
w_parat->reg=reg;
w_parat->data_num = num;
w_parat->s_data = data;
printf("read:val:%d\r\n",w_parat->fd);
len=datatocontrol( set_data ,*w_parat);
if( serial_send(w_parat->fd,set_data,len) == len)
{
/* uart_close(fd); */ //2021.09.19
/* return UART_TRUE; */
printf("send:mod:%d,reg:%d,num:%d,data:%d,fd:%d",w_parat->s_mode,w_parat->reg,w
return (ptDEV_serial)w_parat;
}else{
uart_close(w_parat->fd);
w_parat->flags = SERIAL_ERROR_WRITE;
return (ptDEV_serial)w_parat;
}
}
void passive_task(struct speed speed)
{
static int i;
ptDEV_serial fdr,fdl;
if((speed.s_mode & 0x01) == 0x00)
{
fdl = uart_init(LMOTOR_UART4);
dev_uart_send(fdl,SING_SET,0x43,0x01,&speed.L_speed);
fdr = uart_init(RMOTOR_UART5);
dev_uart_send(fdr,SING_SET,0x43,0x01,&speed.R_speed);
printf("l:%d,r:%d",speed.L_speed,speed.R_speed);
}
else if((speed.s_mode & 0x01) == 0x01){
if (speed.addr == BATTERY_ADD)
{
fdr = uart_init(BATTERY_UART3);
dev_uart_send(fdr,speed.s_mode,speed.reg,speed.num,&speed.L_speed);
}else if(speed.addr == LEFWHEEL_ADD)
{
fdr = uart_init(LMOTOR_UART4);
dev_uart_send(fdr,speed.s_mode,speed.reg,speed.num,&speed.L_speed);
}else if(speed.addr == RIGWHEEL_ADD)
{
fdr = uart_init(RMOTOR_UART5);
dev_uart_send(fdr,speed.s_mode,speed.reg,speed.num,&speed.L_speed);
}
}
if((speed.s_mode & 0x01) == 0x00)
{
uart_threads_close(fdl->fd);
uart_threads_close(fdr->fd);
}else{
uart_threads_close(fdr->fd);
}
}
int uart_threads_close(int fd)
{
ExitThreadFlag = 1;
assert(fd);
return close(fd);
}
|
|