1. Overview / What is Socket CAN
--------------------------------
The socketcan package is an implementation of CAN protocols (Controller Area Network) for Linux.  CAN is a networking technology which has widespread use in automation, embedded devices, and automotive fields.  While there have been other CAN implementations for Linux based on character devices, Socket CAN uses the Berkeley socket API, the Linux network stack and implements the CAN device drivers as network interfaces.  The CAN socket API has been designed as similar as possible to the TCP/IP protocols to allow programmers, familiar with network programming, to easily learn how to use CAN sockets.
2. Motivation / Why using the socket API
----------------------------------------
There have been CAN implementations for Linux before Socket CAN so the question arises, why we have started another project.  Most existing implementations come as a device driver for some CAN hardware, they are based on character devices and provide comparatively little functionality.  Usually, there is only a hardware-specific device driver which provides a character device interface to send and receive raw CAN frames, directly to/from the controller hardware. Queueing of frames and higher-level transport protocols like ISO-TP have to be implemented in user space applications.  Also, most character-device implementations support only one single process to open the device at a time, similar to a serial interface.  Exchanging the CAN controller requires employment of another device driver and often the need for adaption of large parts of the application to the new driver's API.
Socket CAN was designed to overcome all of these limitations.  A new protocol family has been implemented which provides a socket interface to user space applications and which builds upon the Linux network layer, so to use all of the provided queueing functionality.  A device driver for CAN controller hardware registers itself with the Linux network layer as a network device, so that CAN frames from the controller can be passed up to the network layer and on to the CAN protocol family module and also vice-versa.  Also, the protocol family module provides an API for transport protocol modules to register, so that any number of transport protocols can be loaded or unloaded dynamically.  In fact, the can core module alone does not provide any protocol and cannot be used without loading at least one additional protocol module.  Multiple sockets can be opened at the same time, on different or the same protocol module and they can listen/send frames on different or the same CAN IDs.  Several sockets listening on the same interface for frames with the same CAN ID are all passed the same received matching CAN frames.  An application wishing to communicate using a specific transport protocol, e.g. ISO-TP, just selects that protocol when opening the socket, and then can read and write application data byte streams, without having to deal with CAN-IDs, frames, etc.
Similar functionality visible from user-space could be provided by a character device, too, but this would lead to a technically inelegant solution for a couple of reasons:
* Intricate usage.  Instead of passing a protocol argument to socket(2) and using bind(2) to select a CAN interface and CAN ID, an application would have to do all these operations using ioctl(2)s.
* Code duplication.  A character device cannot make use of the Linux network queueing code, so all that code would have to be duplicated
  for CAN networking.
* Abstraction.  In most existing character-device implementations, the hardware-specific device driver for a CAN controller directly
  provides the character device for the application to work with.
  This is at least very unusual in Unix systems for both, char and
  block devices.  For example you don't have a character device for a certain UART of a serial interface, a certain sound chip in your computer, a SCSI or IDE controller providing access to your hard
  disk or tape streamer device.  Instead, you have abstraction layers which provide a unified character or block device interface to the application on the one hand, and a interface for hardware-specific device drivers on the other hand.  These abstractions are provided
  by subsystems like the tty layer, the audio subsystem or the SCSI
  and IDE subsystems for the devices mentioned above.
  The easiest way to implement a CAN device driver is as a character device without such a (complete) abstraction layer, as is done by most existing drivers.  The right way, however, would be to add such a
  layer with all the functionality like registering for certain CAN
  IDs, supporting several open file descriptors and (de)multiplexing
  CAN frames between them, (sophisticated) queueing of CAN frames, and providing an API for device drivers to register with.  However, then
  it would be no more difficult, or may be even easier, to use the networking framework provided by the Linux kernel, and this is what Socket CAN does.
  The use of the networking framework of the Linux kernel is just the natural and most appropriate way to implement CAN for Linux.
  
 
 参照http://archive.cnblogs.com/a/1916143/,交叉编译了can-utils 4.0.6的几个重要工具。busybox的文件系统还要移植ip命令。
  1、
  首先配置can0
  ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1
  这时dmesg可以看到sja1000_fpga_pci 0000:07:04.0: setting BTR0=0x01 BTR1=0x1c
  周立功的usbcan-2a测试模块里,波特率250kbs时就是BTR0=0x01 BTR1=0x1c
  2、
  ip -details link show can0 查看一下
  can0: <NOARP,UP,LOWER_UP,ECHO> mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
    link/can 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 500000 sample-point 0.875 
    tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
    sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 16000000
 
3、接收测试,接收测试软件发送的帧:
# ./candump can0
interface = can0, family = 29, type = 3, proto = 1
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
 
4、发送测试
./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
interface = can0, family = 29, type = 3, proto = 1
测试软件上能看到接收的帧
 
5、重启
使用内核文档说的ip link set can0 type can restart-ms 100 会报
RTNETLINK answers: Device or resource busy
使用ifconfig can0 down ;ip link set can0 up type can
即可
 
4种常见波特率:
250kbps:
ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1   
125kbps:                                    
ip link set can0 type can tq 250 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1                                       
500kbps:
ip link set can0 type can tq 75 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1 
1000kbps:
ip link set can0 up type can bitrate 2000000
常见用法:
ip -details link show can0
ifconfig can0 down ;ip link set can0 up type can
./candump can0 
./cansend   can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

linux socket can测试的更多相关文章

  1. linux socket高性能服务器处理框架

    这个博客很多东西 http://blog.csdn.net/luozhonghua2014/article/details/37041765   思考一种高性能的服务器处理框架 1.首先需要一个内存池 ...

  2. OpenFastPath(2):原生态Linux Socket应用如何移植到OpenFastPath上?

    版本信息: ODP(Open Data Plane): 1.19.0.2 OFP(Open Fast Path): 3.0.0 1.存在的问题 OpenFastPath作为一个开源的用户态TCP/IP ...

  3. Linux Socket - 基本socket链接

    0x0000 Linux Socket 函数 bind listen connect accept send recv read write 0x0001 Server绑不上ip 报错位置在bind函 ...

  4. Linux socket 编程中存在的五个隐患

    前言:         Socket API 是网络应用程序开发中实际应用的标准 API.尽管该 API 简单,但是   开发新手可能会经历一些常见的问题.本文识别一些最常见的隐患并向您显示如何避免它 ...

  5. .Net Core Socket 压力测试

    原文:.Net Core Socket 压力测试 .Net Core Socket 压力测试 想起之前同事说go lang写的push service单机可以到达80万连接,于是就想测试下.Net C ...

  6. linux环境下测试环境搭建

    一.linux环境下测试环境搭建过程简述: 1.前端后台代码未分离情况下: 主要步骤为:安装jdk,安装mysql,安装tomcat,将项目代码部署到tomcat/webapps/下. 2.前端后台代 ...

  7. [linux]Socket编程的头文件

    socket编程中需要用到的头文件 sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arp ...

  8. Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码

    Kali Linux Web 渗透测试视频教—第二十课-利用kali linux光盘或者usb启动盘破解windows密码 文/玄魂 目录 Kali Linux Web 渗透测试视频教—第二十课-利用 ...

  9. Kali Linux Web 渗透测试— 第二十课-metasploit.meterpreter

    Kali Linux Web 渗透测试— 第二十课-metasploit.meterpreter 原文链接:http://www.xuanhun521.com/Blog/7fc11b7a-b6cb-4 ...

随机推荐

  1. 了解Redis 和 Memcached 的区别

    1.Redis支持服务器端的数据操作:Redis相比Memcached来说,拥有更多的数据结构和并支持更丰富的数据操作,通常在Memcached里,你需要将数据拿到客户端来进行类似的修改再set回去. ...

  2. 魔术布局效果-使用本地JSON数据提供数据服务

    在线演示 有社区朋友不知道如何修改外部OpenAPI为本地的JSON服务,这里做一个简单演示. 阅读原文:魔术布局效果-使用本地JSON数据提供数据服务

  3. 聊聊高并发(二十)解析java.util.concurrent各个组件(二) 12个原子变量相关类

    这篇说说java.util.concurrent.atomic包里的类,总共12个.网上有非常多文章解析这几个类.这里挑些重点说说. watermark/2/text/aHR0cDovL2Jsb2cu ...

  4. &lt;转&gt;云主机配置OpenStack使用spice的方法

    按官方文档(openstack-install-guide-yum-juno)搭建和配置的OpenStack默认使用novnc作为云主机的控制台訪问方式,假设须要配置使用GUI的操作系统,会显得比較尴 ...

  5. Linux中setup命令command not found如何解决?

    1.安装setuptool    yum -y install setuptool 2.系统服务管理  yum -y install ntsysv 3.setup中配套的防火墙设置也得装上去,不然会出 ...

  6. 隐函数画图with R

    隐函数画图 with R 这个函数 sin(xsiny)-cos(ycosx)=0 图是这个样子 怎么用R画出来呢?下面是代码 x<-y<-seq(-10,20,0.1) f<-fu ...

  7. [Swift A] - Using Swift with Cocoa and Objective-C--Mix and Match

    Swift与Objective-C的兼容能力允许你在同一个工程中同时使用两种语言.你可以用这种叫做“mix and match”的特性来开发基于混合语言的应用.使用Swfit的最新特性--“mix a ...

  8. Linux下的编译器(转)

    转自:http://ju.outofmemory.cn/entry/2051 简单的说,编译器就是一个可执行程序,它专门用于将程序员易于编写的高级语言 (如 C 语言) 翻译为机器可以识别的低级语言. ...

  9. Google C++ Coding Style 学习笔记

    写在前面:最新公司马上就要开始开发一款视觉产品,工程量较大,且需要对客户提供可以二次开 发的SDK,整个项目用C++编写. 这就对代码质量提出了非常高的要求,同时,如何设计出优雅稳定的API也是相当大 ...

  10. HttpSolrServer 实例管理参考,来自org.eclipse.smila.solr

    http://dev.eclipse.org/svnroot/rt/org.eclipse.smila/trunk/core/org.eclipse.smila.solr/code/为什么要对实例管理 ...