VxWorks7.0的socketCAN程序在发送数据时总提示canSopktClone failed

zhengboai11 2016-01-19 06:02:18
程序如下;
#include <vxWorks.h>
#include <stdio.h>
#include <tickLib.h>
#include <ioLib.h>
#include <lstLib.h>
#include <semLib.h>
#include <selectLib.h>
#include <errno.h>
#include <errnoLib.h>
#include <string.h>
#include <sockLib.h>
#include <private/iosLibP.h>

#include "fslimx6.h"
#include "fslimx6.h"
#include "sysLib.h"
#include "vmLib.h"

#include <canDevLib.h>
#include <socketCAN.h>

/*
* The rawsend( ) function shows how to use the send(), write(), and sendto()
* functions to send a CAN frame.
*/
int rawsend(void)
{
int i, s, nbytes, seed,ret;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
frame.can_id = 0x123;//can_id为帧的标识符
frame.can_dlc = 8;
frame.data[0]=0x01; //数据
frame.data[1]=0x02;
frame.data[2]=0x03;
frame.data[3]=0x04;
frame.data[4]=0x05;
frame.data[5]=0x06;
frame.data[6]=0x07;
frame.data[7]=0x08;

//套接字
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
strcpy(ifr.ifr_name, "can0" );
printf("can0 socket = %d\n", s);

//设置
ret = ioctl(s, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled with that device's index */

if(ret < 0)
perror("IOCTL Failed!\n");
printf("ifr.ifr_ifindex = %d\n", ifr.ifr_ifindex);


//协议族
addr.can_family = PF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

//绑定
addr.can_ifindex = 0;
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if(ret < 0)
printf("Bind Failed!3\n");
else
printf("Bind OK3\n");

//禁用过滤规则,本进程不接收报文,只负责发送
//setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);

addr.can_ifindex = ifr.ifr_ifindex;
nbytes = sendto(s, &frame, sizeof(struct can_frame), 0,(struct sockaddr*)&addr, sizeof(addr));
if (nbytes != sizeof(struct can_frame))
printf ("sendto fail3\n");
else
printf ("sendto succeed3\n");

printf("yyyyy\n");
return 0;
printf("ZZZZZ");
}

运行结果:
can0 socket = 8
ifr.ifr_ifindex = 1
Bind OK3
sendto succeed3
yyyyy
canSopktClone failed
...全文
2395 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,180

社区成员

发帖
与我相关
我的任务
社区描述
xworks是美国 Wind River System 公司( 以下简称风河公司 ,即 WRS 公司)推出的一个实时操作系统。
社区管理员
  • VxWorks开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧