Linux下CAN调试工具can-utils的使用

文章目录

can-utils提供了一系列实用的命令行工具,可以方便的调试CAN总线数据。这里主要记录各个工具的用法

  • candump: 用于查看CAN数据
  • cansend: 用于发送数据到CAN总线
  • canbusload: 用于查看CAN总线使用率

candump

命令行中输入candump然后回车可以打印出该工具的详细用法

bash 复制代码
candump - dump CAN bus traffic.

Usage: candump [options] <CAN interface>+
  (use CTRL-C to terminate candump)

Options:
         -t <type>   (timestamp: (a)bsolute/(d)elta/(z)ero/(A)bsolute w date)
         -H          (read hardware timestamps instead of system timestamps)
         -c          (increment color mode level)
         -i          (binary output - may exceed 80 chars/line)
         -a          (enable additional ASCII output)
         -S          (swap byte order in printed CAN data[] - marked with '`' )
         -s <level>  (silent mode - 0: off (default) 1: animation 2: silent)
         -b <can>    (bridge mode - send received frames to <can>)
         -B <can>    (bridge mode - like '-b' with disabled loopback)
         -u <usecs>  (delay bridge forwarding by <usecs> microseconds)
         -l          (log CAN-frames into file. Sets '-s 2' by default)
         -L          (use log file format on stdout)
         -n <count>  (terminate after receiption of <count> CAN frames)
         -r <size>   (set socket receive buffer to <size>)
         -D          (Don't exit if a "detected" can device goes down.
         -d          (monitor dropped CAN frames)
         -e          (dump CAN error frames in human-readable format)
         -x          (print extra message infos, rx/tx brs esi)
         -T <msecs>  (terminate after <msecs> without any reception)

Up to 16 CAN interfaces with optional filter sets can be specified
on the commandline in the form: <ifname>[,filter]*

Filters:
 Comma separated filters can be specified for each given CAN interface.
<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.
When can_id and can_mask are both 8 digits, they are assumed to be 29 bit EFF.
Without any given filter all data frames are received ('0:0' default filter).

Use interface name 'any' to receive from all CAN interfaces.

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)

常用的几种情况

  1. 直接使用如下指令查看对应CAN设备总线上的所有数据:
bash 复制代码
candump can1
  1. 查看特定ID的CAN数据
bash 复制代码
candump can1,181:7FF,281:7FF // candump <can_devices>,<can_id>:<can_mask>

这里是只监听CANID为0x181,0x281的数据。(知识点:CAN标准帧ID为11位,对应的掩码为0x7FF。CAN扩展帧ID为29位,对应的掩码为0x1FFFFFFF)

cansend

bash 复制代码
Usage: cansend - simple command line tool to send CAN-frames via CAN_RAW sockets.
Usage: cansend <device> <can_frame>.
<can_frame>:
 <can_id>#{R|data}          for CAN 2.0 frames
 <can_id>##<flags>{data}    for CAN FD frames

<can_id>:
 can have 3 (SFF) or 8 (EFF) hex chars
{data}:
 has 0..8 (0..64 CAN FD) ASCII hex-values (optionally separated by '.')
<flags>:
 a single ASCII Hex value (0 .. F) which defines canfd_frame.flags

Examples:
  5A1#11.2233.44556677.88 / 123#DEADBEEF / 5AA# / 123##1 / 213##311
  1F334455#1122334455667788 / 123#R for remote transmission request.
  1. 发送数据到CAN总线
bash 复制代码
cansend can1 201#1122334455667788 //发送 11 22 33 44 55 66 77 88到总线
cansend can1 000#0100

canbusload

bash 复制代码
canbusload - monitor CAN bus load.

Usage: canbusload [options] <CAN interface>+
  (use CTRL-C to terminate canbusload)

Options:
         -t  (show current time on the first line)
         -c  (colorize lines)
         -b  (show bargraph in 5% resolution)
         -r  (redraw the terminal - similar to top)
         -i  (ignore bitstuffing in bandwidth calculation)
         -e  (exact calculation of stuffed bits)

Up to 16 CAN interfaces with mandatory bitrate can be specified on the
commandline in the form: <ifname>@<bitrate>

The bitrate is mandatory as it is needed to know the CAN bus bitrate to
calcultate the bus load percentage based on the received CAN frames.
Due to the bitstuffing estimation the calculated busload may exceed 100%.
For each given interface the data is presented in one line which contains:

(interface) (received CAN frames) (used bits total) (used bits for payload)

Examples:

user$> canbusload can0@100000 can1@500000 can2@500000 can3@500000 -r -t -b -c
  1. 查看CAN1总线负载
bash 复制代码
canbusload can1@500000
相关推荐
_extraordinary_17 分钟前
Linux基本指令(三)+ 权限
linux·运维·服务器
WalkingWithTheWind~25 分钟前
Linux搭建Nginx直播流媒体服务RTMP/RTSP转Http-flv视频浏览器在线播放/Vue/Java/ffmpeg
java·linux·nginx·ffmpeg·vue·http-flv·rtsp
和舒貌2 小时前
Linux系统编程基础详解
linux·运维·服务器·基础
Jack魏3 小时前
Linux MySQL 8.0.29 忽略表名大小写配置
linux·mysql·mysql8·mysql常见问题
ctrigger3 小时前
AI回答:Linux C/C++编程学习路线
linux·c语言·c++
蛊明5 小时前
下载CentOS 10
linux·运维·centos
北京-宏哥5 小时前
Linux系统安装MySQL5.7(其他版本类似)避坑指南
linux·运维·服务器
Aphelios3805 小时前
Linux 下 VIM 编辑器学习记录:从基础到进阶(下)
java·linux·学习·编辑器·vim
qw9495 小时前
Linux 高级篇 日志管理、定制自己的Linux系统、备份与恢复
linux·运维·服务器
丶只有影子5 小时前
【Nacos】从零开始启动Nacos服务(windows/linux)
linux·运维·windows·微服务·springcloud