热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

linuxcan测试程序,linuxsocketcan测试

标签:1.OverviewWhatisSocketCAN--------------------------------Thesocketcanpackageisan

标签:

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: 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 &#61; can0, family &#61; 29, type &#61; 3, proto &#61; 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种常见波特率&#xff1a;

250kbps&#xff1a;

ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

125kbps&#xff1a;

ip link set can0 type can tq 250 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

500kbps&#xff1a;

ip link set can0 type can tq 75 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1

1000kbps&#xff1a;

ip link set can0 up type can bitrate 2000000

常见用法&#xff1a;

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

标签&#xff1a;



推荐阅读
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
author-avatar
sasame
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有