VxWorks7.0的socketCAN程序在发送数据时总提示canSopktClone failed
程序如下;
#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