#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <assert.h>
#include <sys/stat.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <linux/can/error.h>
int main(int argc,char* argv[])
{
int nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame[2] = {{0}};
int fd = socket(PF_CAN, SOCK_RAW, CAN_RAW);//create a socket
if(fd < 0)
{
printf("can socket create failed\n");
}else{
printf("can socket create ok\n");
}
strcpy(ifr.ifr_name, "can0" );
ioctl(fd, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
bind(fd, (struct sockaddr *)&addr, sizeof(addr));
setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
can_err_mask_t err_mask = ( CAN_ERR_TX_TIMEOUT | CAN_ERR_BUSOFF );
setsockopt(fd, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,&err_mask, sizeof(err_mask));
frame[0].can_id = 0x110;
frame[0]. can_dlc = 8;
frame[0].data[0] = 0x55;
frame[0].data[1] = 0x00;
frame[0].data[2] = 0x55;
frame[0].data[3] = 0x55;
frame[0].data[4] = 0x55;
frame[0].data[5] = 0x55;
frame[0].data[6] = 0x55;
frame[0].data[7] = 0x55;
static int num = 0;
while(1)
{
frame[0].data[1] = (num++)%256;
nbytes = write(fd, &frame[0], sizeof(frame[0])); //send frame[0]
if(nbytes = sizeof(frame[0]))
{
printf("send successful!!!\n");
}
if(nbytes < 0)
{
printf("bus off!!!\n");
}
if(frame[0].can_id & CAN_ERR_TX_TIMEOUT)
{
printf("can send timeout\n");
}
if(frame[0].can_id & CAN_ERR_BUSOFF)
{
//system("ip link set can0 up restart-ms 500");
printf("bus off!!!!!!\n");
}
sleep(2);
}
}
Hi,TI
I want to ask a question?
I use linux os thar support the bus_off system,but I test it on TDA4 ,it doesn't works.
When I create a bus_off situation,but nbytes always equeal sizeof(frame[0]).
Bus_off not shows.even send times_out doesn't shows.