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

CAN,Controller Area Network,控制器局域网,一种高可靠性的现场总线。广泛用于汽车电子、工业控制通信。

canutils 工具包内含如下独立的程序,功能简述如下:

canconfig:用于配置 CAN 总线接口的参数,主要是波特率和模式。

candump:从 CAN 总线接口接收数据并以十六进制形式打印到标准输出,也可以输出到指定文件。

canecho:把从 CAN 总线接口接收到的所有数据重新发送到 CAN 总线接口。

cansend:往指定的 CAN 总线接口发送指定的数据。

cansequence:往指定的 CAN 总线接口自动重复递增数字,也可以指定接收模式并校验检查接收的递增数字。

canbusload: 用于查看CAN总线使用率

以下指令,若有两个指令,则两个指令均可。

使用 ip 命令来配置 CAN 总线的位速率:

复制代码
ip link set can0 type cantq 125 prop-seg 6phase-seg1 7 phase-seg2 2 sjw 1

ip 命令直接设定位速率:

复制代码
ip link set can0 type can bitrate 125000
echo 500000 > /sys/class/net/can0/can_bittiming/bitrate

当设置完成后,可以通过下面的命令查询 can0 设备的参数设置:

复制代码
ip -details link show can0

当设置完成后,可以使用下面的命令使能 can0 设备(两个指令均可):

复制代码
ifconfig can0 up
ip link set can0 type can restart-ms 100

使用下面的命令取消 can0 设备使能:

复制代码
ifconfig can0 down

在设备工作中,可以使用下面的命令来查询工作状态:

复制代码
ip -details -statistics link show can0

向can0设备发送数据:

复制代码
cansend can0 123#11223344556677
./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

发送默认ID为0x1的can标准帧,数据为0x11 22 33 44 55 66 77 88

每次最大8个byte

复制代码
./cansend can0 -i 0x800 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 -e

-e 表示扩展帧,CAN_ID最大29bit,标准帧CAN_ID最大11bit

-i表示CAN_ID

循环发送数据:

复制代码
cansend can0 -i 0x02 0x11 0x12 --loop=20

查看接收到的数据:

复制代码
candump can0

查看特定ID的CAN数据

candump can1,181:7FF,281:7FF // candump <can_devices>,<can_id>:<can_mask>

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

添加时间戳参数

candump can1 -tA

使用canconfig配置:

canconfig can0 stop

canconfig can0 bitrate 125000

canconfig can0 start

canbusload用法

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总线负载

    canbusload can1@500000

参考链接:

https://zhuanlan.zhihu.com/p/470681140

https://blog.csdn.net/sinolover/article/details/114833102

https://blog.csdn.net/xiaoyuanwuhui/article/details/136562983

相关推荐
老蒋新思维7 分钟前
创客匠人:工作流嵌入式智能体,重构知识变现的效率底层
大数据·服务器·人工智能·重构·创始人ip·创客匠人·知识变现
绵羊20238 分钟前
索拉非尼作用机制
linux
小张帅三代8 分钟前
华为昇腾服务器ubuntu安装Anaconda步骤
运维·服务器·ubuntu
杜子不疼.12 分钟前
【Linux】 进程控制(二):进程等待(wait/waitpid) 与 子进程获取(status)
linux·运维·服务器
秦少游在淮海13 分钟前
网络缓冲区 · 通过读写偏移量维护数据区间的高效“零拷贝” Buffer 设计
linux·开发语言·网络·tcp协议·muduo·网络缓冲区
炮院李教员14 分钟前
Ubuntu 24.04 安装common-extensions
linux·运维·ubuntu
YJlio16 分钟前
ZoomIt 学习笔记(11.9):绘图模式——演示时“手写板”:标注、圈画、临时白板
服务器·笔记·学习
满天星830357719 分钟前
【Linux】信号(下)
android·linux·运维·服务器·开发语言·性能优化
拾贰_C21 分钟前
【Ubuntu】怎么查询Nvidia显卡信息
linux·运维·ubuntu
濊繵22 分钟前
Linux网络--IP 分片和组装的具体过程
linux·网络·tcp/ip