SocketCAN
来源:互联网 发布:富贵捕鱼源码 编辑:程序博客网 时间:2024/05/18 02:27
目录(?)[-]
- Usage
- References
- External links
SocketCAN is a set of open source CAN drivers and a networking stack contributed by Volkswagen Researchto the Linux kernel. Formerly known as Low Level CAN Framework (LLCF).
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
- ^ http://bugs.python.org/issue10141
- ^ viewable online from SocketCAN WebSVN or in
linux/Documentation/networking/can.txt
in most recent source trees
External links
- SocketCAN project site
- SocketCAN documentation (german)
- SocketCAN overview (english)
- SocketCAN
- SocketCAN
- SocketCAN
- SocketCAN bug记录
- linux socketcan编程详解
- can3--socketcan之mcp251x.c
- can3--socketcan之mcp251x.c
- atmel9260上linux socketcan MCP2515调试笔记
- atmel9260上linux socketcan MCP2515调试笔记
- 记几个调试SocketCAN的命令
- atmel9260上linux socketcan MCP2515调试笔记
- Linux下CAN总线速率设置,socketCAN。
- QT 环境下开发socketCan接口程序
- QT 环境下开发socketCan接口程序
- 求SOCKETCAN 读取指定CANID数据方法
- 基于bluez or socketcan等 程序编译时注意事项
- linux socketcan驱动编写及使用(研华PCI-1680)
- s3c2440 socketcan移植修改sja1000收发CAN数据帧
- linux学习笔记
- mysql 数据库之archive存储引擎札记
- 如何查看linux下串口是否可用?串口名称等?
- 例子:BASH SHELL下命令行获取手动输入的用户名和密码
- 五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
- SocketCAN
- 精通js正则表达式
- FTP文件传输协议
- 卷积学习与传统稀疏编码、ICA模型学习区别(逐步补充)
- The Coldest Place On Earth——Chapter 2 The Race
- css
- _stdcall 与_cdecl
- atmel9260上linux socketcan MCP2515调试笔记
- jQuery获得IE版本不准确(webbrowser)