I'm converting a Windows program to Linux and I have some problem writing to the CAN bus. Trying write
or send
returns -1 with errno
as "No such device or address"
Some simplified C code:
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) { perror("socket"); return errno; }
struct sockaddr_can addr;
addr.can_family = AF_CAN;
struct ifreq ifr;
strcpy(ifr.ifr_name, "any");
addr.can_ifindex = 0; // any can interface
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind"); return errno;
}
struct can_frame frame={0};
int nbytes = recv(s, &frame, sizeof(frame), MSG_DONTWAIT);
// The above works. Not the following:
struct can_frame frame={0x123, 2, .data={1, 2}};
int nbytes = send(s, &frame, sizeof(frame), 0);
// nbytes=-1, errno=6 "No such device or address"