The raw CAN bus
In a Linux-based system, we can use the SocketCAN implementation to manage these devices and then exchange data between them. It uses the Berkeley socket API and the Linux network stack (and raw sockets) to implement CAN device drivers as network interfaces to allow programmers familiar with network programming to easily learn how to use CAN sockets.
Note
See the Documentation/networking/can.txt
file in the kernel's repository for more information.
Here is a simplified example in C language regarding how to open a SocketCAN socket:
int s; char *ifname; struct sockaddr_can addr; struct can_frame frame; struct ifreq ifr; int ret; /* Open the PF_CAN socket */ s = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (s < 0) { perror("Error while opening socket"); exit(-1); } /* Find the CAN device */ strcpy(ifr.ifr_name, ifname); ret = ioctl(s, SIOCGIFINDEX, &ifr); if (ret < 0) { perror("ioctl"); exit(-1); } printf("...