SocketCAN

 
From Wikipedia, the free encyclopedia
  (Redirected from Socketcan)
Jump to: navigation, search

SocketCAN is a set of open source CAN drivers and a networking stack contributed by Volkswagen Research to the Linux kernel. Formerly known as Low Level CAN Framework (LLCF).

Typical CAN Communication layers. With SocketCAN (left) or conventional (right).

Established CAN drivers are based on the model of character devices. Typically they only allow sending to and receiving from the CAN controller. Conventional implementations for this device class only allow a single process on the device which means that all other processes are blocked in the meantime as known from accessing a device via the serial interface. In addition, these drivers typically all differ slightly in the interface presented to the application, stifling portability. The SocketCAN concept on the other hand uses the model of network devices, which allows multiple applications to access one CAN device simultaneously. Also, single application are able to access multiple CAN networks in parallel.

The SocketCAN concept extends the Berkeley sockets API in Linux by introducing a new protocol family PF_CAN that coexists with other protocol families like PF_INET for the Internet Protocol. The communication with the CAN bus is done analogue to the use of the Internet Protocol via Sockets. Fundamental components of SocketCAN are the network device drivers for different CAN controllers and the implementation of the CAN protocol family. The protocol family PF_CAN provide the structures to enable different protocols on the bus: Raw sockets for direct CAN communication and transport protocols for point-to-point connections. Moreover the broadcast manager which is part of the CAN protocol family provides functions e.g. for sending CAN messages periodically or realize complex message filters.

Patches about CAN were added in the 2.6.25 Linux kernel. Meanwhile some controller drivers were added and work is going on to add drivers for a variety of controllers.

 Usage

The application first sets up its access to the CAN interface by initialising a socket (much like in TCP/IP communications), then binding that socket to an interface (or all interfaces, if the application so desires). Once bound, the socket can then be used like a UDP socket via read, write, etc...

Python added support for SocketCan in version 3.3.[1]

The following is a simple (incomplete) example, that sends a packet, then reads back a packet using the raw interface. It is based on the notes documented in the Linux Kernel[2].

#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) );

References

  1. ^ http://bugs.python.org/issue10141
  2. ^ viewable online from SocketCAN WebSVN or in linux/Documentation/networking/can.txt in most recent source trees

 External links

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值