2

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"
dargaud
  • 2,431
  • 2
  • 26
  • 39
  • 1
    Is the name of your can interface really `any` and not `can0`? – Pablo Mar 30 '23 at 22:20
  • You are absolutely right, it fails with `any` but it works with `vcan0`. Thank! I'll see what the difference is in more details. – dargaud Mar 31 '23 at 09:58
  • 1
    I guess that socketcan is intelligent that if you read from `any`, then it will poll from all active can interfaces. However, if you try to write, then which one should it use? And that's why it fails. I don't know, I never use `any` as the interface name. – Pablo Mar 31 '23 at 10:20

0 Answers0