首页 > 编程知识 正文

linux两个程序间通信,不能用于linux中的进程通信

时间:2023-05-04 07:53:57 阅读:172583 作者:4739

实现Linux下CAN通信——的初始化,发送两个发送和接收(以队列形式),使用两个线程,目前还没有编写另一个超时响应。 接收部分使用select实现。

#ifndef _CAN_H_

#define _CAN_H_

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#包含

#include 'can_queue.h '

#include 'type.h '

#ifndef AF_CAN

#define AF_CAN 29

#endif

#ifndef PF_CAN

#define PF_CAN AF_CAN

#endif

typedef enum

{

CAN_PORT_0=0,//can0

CAN_PORT_1,//can1

}can_port_t;

typedef struct

{

char *name;

fd_set fdsr;

pthread_t send_thread;

pthread_t recv_thread;

pthread_t time_thread;

can_queue_t *send_queue; //接受和发送队列

can_queue_t *recv_queue;

} can_t;

voidCDgtx(intarg );

#endif /* _CAN_H_ */

#include 'can.h '

saticcan_t*can_init(intname ) )为

{

struct sockaddr_can addr;

struct ifreq ifr;

struct can_filter rfilter[1];

can_t*current=(can_t* ) malloc ) sizeof ) can_t );

current-FD=socket(pf_can,SOCK_RAW,CAN_RAW );

sprintf(IFR.IFR_name,' can%d ',name );

current-name=(char* ) malloc(6) 6;

memset (当前名称,0,6 );

sprintf(current-name,' can%d ',name );

ret=ioctl(current-FD,SIOCGIFINDEX,ifr );

是if(ret0)

{

退出(0;

}

addr.can_family=AF_CAN;

addr.can _ ifindex=IFR.IFR _ ifindex;

bind(current-FD,) struct sockaddr * (addr,sizeof ) addr );

rfilter[0].can_id=0x2;

rfilter[0].can_mask=0;

setsockopt(current-FD,SOL_CAN_RAW,CAN_RAW_FILTER,rfilter,sizeof ) rfilter );

返回当前;

}

状态void * can _ send _ thread (void * arg ) )。

{

can_t *current=arg;

can_frame_t frame;

uint8_t read_ret=0;

while(1)。

{

写(current-FD,frame,sizeof ) )帧);

read _ ret=current-send _ queue-can _ read (current-send _ queue,frame );

if(can_ok==read_ret ) )。

{

ret=write(current-FD,frame,sizeof ) ) frame );

usleep(1200;

}

usleep(100;

}

返回空值;

}

static void * can _ recv _ thread (void * arg ) )

{

can_frame_t frame;

struct timeval tv;

fd_set rset;

can_t *current=arg;

while(1)。

{

tv.tv_sec=0;

tv.tv_usec=200;

FD_zero(rset;

FD_set(current-FD,rset );

ret=select(current-FD1,rset,NULL,NULL,NULL );

if(0==ret ) )。

{

返回空值;

}

ret=read(current-FD,frame,sizeof ) ) frame );

重新压缩(frame ) )

{

返回空值;

}

current-recv _ queue-can _ write (current-recv _ queue,frame )==CAN_ERROR ) ) )。

{

}

}

返回空值;

}

voidCDgtx(intarg ) )。

{

can_t*current=can_init(arg;

current-recv _ queue=canqueueinit (can _ recv _ queue _ size;

current-send _ queue=canqueueinit (can _ send _ queue _ size;

ph read _ create (current-send _ thread,NULL,can_send_thread,) void * ) current );

ph read _ create (current-recv _ thread,NULL,can_recv_thread,) void * ) current );

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。