热门标签 | 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;



推荐阅读
  • Ext JS MVC系列一:环境搭建与框架概览
    本文主要介绍了如何在项目中使用Ext JS 4作为前端框架,并详细讲解了Ext JS 4的MVC开发模式。文章将从项目目录结构、相关CSS和JS文件的引用以及MVC框架的整体认识三个方面进行总结。 ... [详细]
  • 深入解析Android 4.4中的Fence机制及其应用
    在Android 4.4中,Fence机制是处理缓冲区交换和同步问题的关键技术。该机制广泛应用于生产者-消费者模式中,确保了不同组件之间高效、安全的数据传输。通过深入解析Fence机制的工作原理和应用场景,本文探讨了其在系统性能优化和资源管理中的重要作用。 ... [详细]
  • Web开发框架概览:Java与JavaScript技术及框架综述
    Web开发涉及服务器端和客户端的协同工作。在服务器端,Java是一种优秀的编程语言,适用于构建各种功能模块,如通过Servlet实现特定服务。客户端则主要依赖HTML进行内容展示,同时借助JavaScript增强交互性和动态效果。此外,现代Web开发还广泛使用各种框架和库,如Spring Boot、React和Vue.js,以提高开发效率和应用性能。 ... [详细]
  • 在Java分层设计模式中,典型的三层架构(3-tier application)将业务应用细分为表现层(UI)、业务逻辑层(BLL)和数据访问层(DAL)。这种分层结构不仅有助于提高代码的可维护性和可扩展性,还能有效分离关注点,使各层职责更加明确。通过合理的设计和实现,三层架构能够显著提升系统的整体性能和稳定性。 ... [详细]
  • Spring框架的核心组件与架构解析 ... [详细]
  • 设计实战 | 10个Kotlin项目深度解析:首页模块开发详解
    设计实战 | 10个Kotlin项目深度解析:首页模块开发详解 ... [详细]
  • 如何使用 com.jme3.input.FlyByCamera 构造函数及其代码示例详解 ... [详细]
  • IOS Run loop详解
    为什么80%的码农都做不了架构师?转自http:blog.csdn.netztp800201articledetails9240913感谢作者分享Objecti ... [详细]
  • 深入解析Android GPS机制:第五部分 ... [详细]
  • C++ 异步编程中获取线程执行结果的方法与技巧及其在前端开发中的应用探讨
    本文探讨了C++异步编程中获取线程执行结果的方法与技巧,并深入分析了这些技术在前端开发中的应用。通过对比不同的异步编程模型,本文详细介绍了如何高效地处理多线程任务,确保程序的稳定性和性能。同时,文章还结合实际案例,展示了这些方法在前端异步编程中的具体实现和优化策略。 ... [详细]
  • 在Linux系统中,网络配置是至关重要的任务之一。本文详细解析了Firewalld和Netfilter机制,并探讨了iptables的应用。通过使用`ip addr show`命令来查看网卡IP地址(需要安装`iproute`包),当网卡未分配IP地址或处于关闭状态时,可以通过`ip link set`命令进行配置和激活。此外,文章还介绍了如何利用Firewalld和iptables实现网络流量控制和安全策略管理,为系统管理员提供了实用的操作指南。 ... [详细]
  • 经过两天的努力,终于成功解决了半平面交模板题POJ3335的问题。原来是在`OnLeft`函数中漏掉了关键的等于号。通过这次训练,不仅加深了对半平面交算法的理解,还提升了调试和代码实现的能力。未来将继续深入研究计算几何的其他核心问题,进一步巩固和拓展相关知识。 ... [详细]
  • 如何利用Java 5 Executor框架高效构建和管理线程池
    Java 5 引入了 Executor 框架,为开发人员提供了一种高效管理和构建线程池的方法。该框架通过将任务提交与任务执行分离,简化了多线程编程的复杂性。利用 Executor 框架,开发人员可以更灵活地控制线程的创建、分配和管理,从而提高服务器端应用的性能和响应能力。此外,该框架还提供了多种线程池实现,如固定线程池、缓存线程池和单线程池,以适应不同的应用场景和需求。 ... [详细]
  • 在洛谷 P1344 的坏牛奶追踪问题中,第一问要求计算最小割,而第二问则需要找到割边数量最少的最小割。通过为每条边附加一个单位权值,可以在求解最小割时优先选择边数较少的方案,从而同时解决两个问题。这种策略不仅简化了问题的求解过程,还确保了结果的最优性。 ... [详细]
  • 在本文中,我们将为 HelloWorld 项目添加视图组件,以确保控制器返回的视图路径能够正确映射到指定页面。这一步骤将为后续的测试和开发奠定基础。首先,我们将介绍如何配置视图解析器,以便 SpringMVC 能够识别并渲染相应的视图文件。 ... [详细]
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社区 版权所有