Hello,
I am using the following code to start socketcan communication in C/C++ for the can0 port on ApalisiMX8 on the Ixora board.
Source: SocketCAN - Controller Area Network — The Linux Kernel documentation
See sample code below.
Issue is that I am always getting a bind error when using can0.
Also tried using /sys/class/net/can0 and /sys/devices/platform/bus@5a000000/5a8d0000.can/net/can0 as alternative names, but still getting a bind error.
What is the correct reference to use?
int s1,s2;
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;
//Trying to change name -can0, /sys/devices/platform/bus@5a000000/5a8d0000.can/net/can0, /sys/class/net/can0
const char *ifname = “can0”;
//port1
if((s1 = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("Error while opening socket \n ");
return -1;
}
else{
printf("socket at index %d\n", s1);
}
strcpy(ifr.ifr_name, ifname);
ioctl(s1, SIOCGIFINDEX, &ifr);
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
printf("%s at index %d\n", ifname, ifr.ifr_ifindex);
if(bind(s1, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("Error in socket bind");
return -2;
}