SocketCAN

from Wikipedia, the free encyclopedia

SocketCAN is a collection of CAN drivers and a network layer, provided by Volkswagen AG Group Research on the Linux kernel as open source . It is also known as the Low Level CAN Framework (LLCF).

Typical layer model of CAN communication, with SocketCAN (left) or conventional (right)

The predominant CAN drivers are based on the model of a character device. Typically, these drivers only allow sending and receiving for a CAN controller. The implementations for this device class usually only allow a single process to access the device, i.e. H. this serial interface is blocked for all other processes. In addition, the interface of the driver to the application is different in detail and not standardized, which reduces the portability of a CAN application. The SocketCAN concept, on the other hand, uses the network device model. It allows several applications to access CAN functions at the same time. Individual applications can also use several CAN networks in parallel.

The essential aspect of sockets is already hidden in the name SocketCAN. The SocketCAN concept extends the classic Berkeley socket API of an operating system. SocketCAN adds a common protocol family called PF_CAN to the network stack18, which in addition to other well-known protocol families such as B. PF_INET coexists independently for the Internet. SocketCAN itself consists of network drivers for CAN controllers and an infrastructure for new CAN protocols. The CAN bus can be accessed directly with raw sockets.

There are also extensions for transport protocols such as B. ISO-TP. ISO-TP can segment larger data telegrams of up to 4095 bytes into a sequence of smaller CAN frames. A point-to-point connection can be established by addressing in the form of two CAN ID pairs. In addition, SocketCAN has a broadcast manager that enables CAN messages to be filtered and sent periodically. Support for SocketCAN is currently only available for Linux from kernel version 2.6.25. SocketCAN is actively being further developed, further network drivers for CAN controllers are constantly being added.

use

The application first sets up access to the CAN interface by initializing a socket (similar to TCP / IP communication), and binds this socket ( bind) to an interface (or all interfaces, if intended by the application). If the connection is successful, the socket is B. the functions for reading read(..)and writing can be write(..)used.

Python supports SocketCan from version 3.3.

The following simple example sends and reads a packet using the RAW socket.

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include <linux/can.h>
#include <linux/can/raw.h>
#include <string.h>

/* At time of writing, these constants are not defined in the headers */
#ifndef PF_CAN
#define PF_CAN 29
#endif

#ifndef AF_CAN
#define AF_CAN PF_CAN
#endif

/* ... */

/* Somewhere in your app */

   /* Create the socket */
   int skt = socket( PF_CAN, SOCK_RAW, CAN_RAW );

   /* Locate the interface you wish to use */
   struct ifreq ifr;
   strcpy(ifr.ifr_name, "can0");
   ioctl(skt, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
                          * with that device's index */

   /* Select that CAN interface, and bind the socket to it. */
   struct sockaddr_can addr;
   addr.can_family = AF_CAN;
   addr.can_ifindex = ifr.ifr_ifindex;
   bind( skt, (struct sockaddr*)&addr, sizeof(addr) );

   /* Send a message to the CAN bus */
   struct can_frame frame;
   frame.can_id = 0x123;
   strcpy( frame.data, "foo" );
   frame.can_dlc = strlen( frame.data );
   int bytes_sent = write( skt, &frame, sizeof(frame) );

   /* Read a message back from the CAN bus */
   int bytes_read = read( skt, &frame, sizeof(frame) );

See also

Web links

Individual evidence

  1. bugs.python.org