SocketCAN

SocketCAN is a collection of open source CAN drivers and a network layer, beige represents Volkswagen Research to the Linux kernel. Also known as Low Level CAN Framework ( LLCF ).

Predominant CAN drivers are based on the model of a character-oriented device ( character device ). Typically, these drivers allow for a CAN controller only to send and receive. The implementations for this class of devices usually only allow a single process to access the device, ie for all other processes, this serial port blocked. In addition, the interface of the driver application to the details are different and non- uniform, which reduces the portability of a CAN application. The SocketCAN concept, however, uses the model of the network devices. It allows multiple applications simultaneously on CAN - access functions. Also, individual applications can use multiple CAN networks in parallel.

On behalf SocketCAN already hides the essential aspect of sockets. The SocketCAN concept extends the classical Berkeley socket API of an operating system. SocketCAN adds this, add a common protocol family called PF_CAN the network Stack18 which independently coexists alongside other well-known protocol families like PF_INET for the Internet. Elemental is SocketCAN even from network drivers for the CAN controller and an infrastructure for new CAN protocols. Using raw sockets can be accessed directly on the CAN bus.

There are also enhancements for transport protocols such as ISO - TP. ISO -TP can segment larger data frames to 4095 bytes into a sequence of smaller CAN frames. By addressing in the form of two pairs of the CAN ID establish a point - to-point connection is possible. In addition, there are in SocketCAN a broadcast manager, which enables CAN messages to filter and periodically send. Support for SocketCAN is given to jetzigem time exclusively for Linux from kernel version 2.6.25. SocketCAN is actively developed, always adding more network drivers for CAN controller.

Use

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

Python supports SocketCAN since version 3.3.

The following simple example sends and reads a packet from the use of a raw socket.

# include # include # include file # include   # include # include # include   / * At time of writing, thesis 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 did device 's index * /       / * Select did 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

  • Can4linux
736270
de