在linux下,can卡被作为ip设备调用,下面写出ubuntu16.04下启用pcie can卡流程,需要装好爱泰can卡驱动和can-utils(sudo apt-get install can-utils)
in driver dir
sudo insmod dev.ko
sudo insmod sja1000.ko
sudo insmod hello_pcie.ko
lspci -n
ls /sys/class/net/can*
ip link show can0
sudo ip link set can0 down
sudo ip link set can0 type can bitrate 500000
sudo ip link set can0 up
candump can0
cangen can0 -g 4 -I 42A -L 1 -D i -v -v
Candump 为接收数据,是canutils工具包的功能模块
cangen为发送指定格式数据。
下面摘录can-utils的github的readme及常用功能包help
官方readme
SocketCAN userspace utilities and tools
This repository contains some userspace utilities for Linux CAN subsystem (aka SocketCAN):
Basic tools to display, record, generate and replay CAN traffic
candump : display, filter and log CAN data to files
canplayer : replay CAN logfiles
cansend : send a single frame
cangen : generate (random) CAN traffic
cansniffer : display CAN data content differences (just 11bit CAN IDs)
CAN access via IP sockets
canlogserver : log CAN frames from a remote/local host
bcmserver : interactive BCM configuration (remote/local)
socketcand : use RAW/BCM/ISO-TP sockets via TCP/IP sockets
cannelloni : UDP/SCTP based SocketCAN tunnel
CAN in-kernel gateway configuration
cangw : CAN gateway userpace tool for netlink configuration
CAN bus measurement and testing
canbusload : calculate and display the CAN busload
can-calc-bit-timing : userspace version of in-kernel bitrate calculation
canfdtest : Full-duplex test program (DUT and host part)
ISO-TP tools ISO15765-2:2016 for Linux
isotpsend : send a single ISO-TP PDU
isotprecv : receive ISO-TP PDU(s)
isotpsniffer : 'wiretap' ISO-TP PDU(s)
isotpdump : 'wiretap' and interpret CAN messages (CAN_RAW)
isotpserver : IP server for simple TCP/IP <-> ISO 15765-2 bridging (ASCII HEX)
isotpperf : ISO15765-2 protocol performance visualisation
isotptun : create a bi-directional IP tunnel on CAN via ISO-TP
Log file converters
asc2log : convert ASC logfile to compact CAN frame logfile
log2asc : convert compact CAN frame logfile to ASC logfile
log2long : convert compact CAN frame representation into user readable
Serial Line Discipline configuration (for slcan driver)
slcan_attach : userspace tool for serial line CAN interface configuration
slcand : daemon for serial line CAN interface configuration
slcanpty : creates a pty for applications using the slcan ASCII protocol
CMake Project Generator
Place your build folder anywhere, passing CMake the path. Relative or absolute.
Some examples using a build folder under the source tree root:
Android : cmake -DCMAKE_TOOLCHAIN_FILE=~/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 -DANDROID_ABI=armeabi-v7a .. && make
Android Studio : Copy repo under your project's app folder, add add_subdirectory(can-utils) to your CMakeLists.txt file after cmake_minimum_required(). Generating project will build Debug/Release for all supported EABI types. ie. arm64-v8a, armeabi-v7a, x86, x86_64.
Raspberry Pi : cmake -DCMAKE_TOOLCHAIN_FILE=~/rpi/tools/build/cmake/rpi.toolchain.cmake .. && make
Linux : cmake -GNinja .. && ninja
Linux Eclipse Photon (Debug) : CC=clang cmake -G"Eclipse CDT4 - Unix Makefiles" ../can-utils/ -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_VERSION=4.8.0
To override the base installation directory use: CMAKE_INSTALL_PREFIX ie. CC=clang cmake -DCMAKE_INSTALL_PREFIX=./out .. && make install
Additional Information:
SocketCAN Documentation (Linux Kernel)
Elinux.org CAN Bus Page
Debian Package Description
下面是翻译
SocketCAN用户空间实用程序和工具
该存储库包含一些用于Linux CAN子系统(又名SocketCAN)的用户空间实用程序:
显示,记录,生成和重放CAN流量的基本工具
candump:显示,过滤和记录CAN数据到文件
canplayer:重播CAN日志文件
cansend:发送单个帧,仅仅是单帧
cangen:生成(随机)CAN流量
cansniffer:显示CAN数据内容的差异(仅11位CAN ID)
通过IP插座进行CAN访问
canlogserver:记录来自远程/本地主机的CAN帧
bcmserver:交互式BCM配置(远程/本地)
socketcand:通过TCP / IP套接字使用RAW / BCM / ISO-TP套接字
cannelloni:基于UDP / SCTP的SocketCAN隧道
CAN内核网关配置
cangw:用于netlink配置的CAN网关userpace工具
CAN总线测量与测试
canbusload:计算并显示CAN总线负载
can-calc-bit-timing:内核空间比特率计算的用户空间版本
canfdtest:全双工测试程序(DUT和主机部分)
适用于Linux的ISO-TP工具ISO15765-2:2016
isotpsend:发送单个ISO-TP PDU
isotprecv:接收ISO-TP PDU
isotpsniffer:“窃听” ISO-TP PDU
isotpdump:“窃听”并解释CAN消息(CAN_RAW)
isotpserver:用于简单TCP / IP <-> ISO 15765-2桥接(ASCII HEX)的IP服务器
isotpperf:ISO15765-2协议性能可视化
isotptun:通过ISO-TP在CAN上创建双向IP隧道
日志文件转换器
asc2log:将ASC日志文件转换为紧凑型CAN框架日志文件
log2asc:将紧凑型CAN框架日志文件转换为ASC日志文件
log2long:将紧凑的CAN帧表示形式转换为用户可读
串行线路规范配置(用于slcan驱动程序)
slcan_attach:用于串行线CAN接口配置的用户空间工具
slcand:用于串行线CAN接口配置的守护程序
slcanpty:使用slcan ASCII协议为应用程序创建一个pty
CMake项目生成器
将构建文件夹放置在任何地方,并通过CMake路径。相对或绝对。
使用源树根目录下的构建文件夹的一些示例:
Android:cmake -DCMAKE_TOOLCHAIN_FILE =〜/ Android / Sdk / ndk-bundle / build / cmake / android.toolchain.cmake -DANDROID_PLATFORM = android-21 -DANDROID_ABI = armeabi-v7a .. && make
Android Studio:在项目的应用程序文件夹下复制存储库,在cmake_minimum_required()之后将add_subdirectory(can-utils)添加到CMakeLists.txt文件中。生成项目将为所有受支持的EABI类型构建调试/发布。即。 arm64-v8a,armeabi-v7a,x86,x86_64。
树莓派:cmake -DCMAKE_TOOLCHAIN_FILE =〜/ rpi / tools / build / cmake / rpi.toolchain.cmake .. && make
Linux:cmake -GNinja .. && ninja
Linux Eclipse Photon(调试):CC = clang cmake -G“ Eclipse CDT4-Unix Makefiles” ../can-utils/ -DCMAKE_BUILD_TYPE = Debug -DCMAKE_ECLIPSE_VERSION = 4.8.0
要覆盖基本安装目录,请使用:CMAKE_INSTALL_PREFIX,即。 CC = clang cmake -DCMAKE_INSTALL_PREFIX =。/ out .. &&进行安装
附加信息:
SocketCAN文档(Linux内核)
Elinux.org CAN总线页面
Debian软件包描述
Usage: candump [options] +
(use CTRL-C to terminate candump)
Options: -t (timestamp时间戳: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)
-c (increment color mode level增量色阶)
-i (binary output - may exceed 80 chars/line二进制输出-可能超过80字符/行)
-a (enable additional ASCII output启用附加ASCII输出)
-S (swap byte order in printed CAN data[]交换打印CAN数据的字节顺序[] - marked with ‘`’ )
-s (silent mode - 0: off关闭 (default) 1: animation激活 2: silent沉默,沉默应该是不显示出来)
-b (birdge mode - send received frames to 转发模式,这里需要写can0还是can1)
-B (bridge mode - like ‘-b’ with disabled loopback转发模式)
-u (delay bridge forwarding by microseconds带延迟的转发)
-l (log CAN-frames into file. Sets ‘-s 2’ by default将数据保存文件)
-L (use log file format on stdout标准输出)
-n (terminate after receiption of CAN frames只接受几帧数据)
-r (set socket receive buffer to 设置接收缓存)
-d (monitor dropped CAN frames监视丢弃帧)
-e (dump CAN error frames in human-readable format显示错误帧)
-x (print extra message infos, rx/tx brs esi打印额外信息,如接收/发送,BRS位,esi位,好像和CAN-FD有关)
-T (terminate after without any reception一定时间后关闭)
Up to 16 CAN interfaces with optional filter sets can be specified
on the commandline in the form: [,filter]*
命令行上最多可以指定16个带有可选过滤器集的CAN接口,格式为:[,filter]*
Comma separated filters can be specified for each given CAN interface:
可以为每个给定的CAN接口指定逗号分隔的过滤器:
<can_id>:<can_mask> (matches when <received_can_id> & mask == can_id & mask)
<can_id>~<can_mask> (matches when <received_can_id> & mask != can_id & mask)
#<error_mask> (set error frame filter, see include/linux/can/error.h)
[j|J] (join the given CAN filters - logical AND semantic)
CAN IDs, masks and data content are given and expected in hexadecimal values.
CAN ID,掩码和数据内容以十六进制值给出。
当can_id和can_mask均为8位数字时,假定它们为29位EFF。
When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.
当can_id和can_mask均为8位数字时,假定它们为29位EFF。
Without any given filter all data frames are received (‘0:0’ default filter).
如果没有任何给定的过滤器,则将接收所有数据帧(默认过滤器为“ 0:0”)。
Use interface name ‘any’ to receive from all CAN interfaces.
使用接口名称“ any”从所有CAN接口接收。
Examples:
candump -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8
candump -l any,0~0,#FFFFFFFF (log only error frames but no(!) data frames)
candump -l any,0:0,#FFFFFFFF (log error frames and also all data frames)
candump vcan2,92345678:DFFFFFFF (match only for extended CAN ID 12345678)
candump vcan2,123:7FF (matches CAN ID 123 - including EFF and RTR frames)
candump vcan2,123:C00007FF (matches CAN ID 123 - only SFF and non-RTR frames)
===============================================================================
cangen: generate CAN frames
Usage: cangen [options]
Options: -g (gap in milli seconds - default: 200 ms间隔时间,毫秒)
-e (generate extended frame mode (EFF) CAN frames生成扩展帧模式(EFF)CAN帧)
-f (generate CAN FD CAN frames生成CAN FD帧)
-R (send RTR frame发送RTR帧,RTR位为扩展帧内容)
-m (mix -e -f -R frames 混合发送几种类型)
-I (CAN ID generation mode - see below id生成方式,详情见下面)
-L (CAN data length code (dlc) generation mode - see below CAN数据长度代码(dlc)生成模式-参见下文(这里是设置以帧报文长度))
-D (CAN data (payload) generation mode - see below CAN数据(有效载荷)生成模式-参见下文(这里设置一帧报文内容))
-p (poll on -ENOBUFS to write frames with ms 在-ENOBUFS上轮询以使用 ms写入帧)
-n (terminate after CAN frames - default infinite 在个CAN帧后终止-默认为无限)
-i (ignore -ENOBUFS return values on write() syscalls 忽略-ENOBUFS返回write()系统调用的值)
-x (disable local loopback of generated CAN frames 禁用生成的CAN帧的本地环回)
-v (increment verbose level for printing sent CAN frames 增加详细级别以打印发送的CAN帧)
Generation modes:
‘r’ => random values (default) 随机值
‘i’ => increment values 递增
=> fix value using 使用固定值
When incrementing the CAN data the data length code minimum is set to 1.
当增加CAN数据时,数据长度代码最小值设置为1。
CAN IDs and data content are given and expected in hexadecimal values.
CAN ID和数据内容以十六进制值给出和期望。
Examples:
cangen can0 -g 4 -I 42A -L 1 -D i -v -v (fixed CAN ID and length, inc. data固定的CAN ID和长度,包括 数据)
cangen can0 -e -L i -v -v -v (generate EFF frames, incr. length 生成EFF帧,增量模式。 长度)
cangen can0 -D 11223344DEADBEEF -L 8 (fixed CAN data payload and length 固定的CAN数据有效载荷和长度)
cangen can0 -g 0 -i -x (full load test ignoring -ENOBUFS 满负荷测试忽略-ENOBUFS)
cangen can0 -g 0 -p 10 -x (full load test with polling, 10ms timeout 带有轮询的满负载测试,超时10ms)
cangen can0 (my favourite default 😃
cangen can0 -g 10 -I 101 -D 11223344DEADBEEF -L 8 (间隔10ms,id为101,数据为11223344DEADBEEF,长度为8)
cangen can0 -n 1 -I 101 -D 11223344DEADBEEF -L 8 (id为101,数据为11223344DEADBEEF,长度为8,只发送一次)
cangen can0 -e -g 10 -I 0c000101 -D 011bfb0000000000 -L 8 (对应agv小车,发送扩展帧,10ms一次,id为0c000101,数据为011bfb0000000000,01表示can控制,小车转向正向共7FFF级(32767),共分100级,则一级为147(hex),所以1bfb/147=15,表示向右转向15%,一帧长度为8)