0%

在linux系统中调试CAN总线笔记

CAN发送接收示例

CAN发送数据命令

1
cansend can0 123#11.22

cansend是命令,can0是参数,123是ID,#分隔符,11.22是发送数据。

CAN接收:接收需要另外开一个终端

1
candump can0

candump 是命令,can0是参数,表示哪个设备,这个命令是阻塞型的,会一直等待接收。

CAN工具安装调试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
uname -r                    # 应该能看到自己起的CONFIG_LOCALVERSION
lsusb # 要能看到插入的usb转can,是ttyACM0或者gs_usb之类的,不然是usbipd没装对

sudo apt install can-utils
sudo modprobe can
sudo modprobe can-raw

# 如果是slcan的固件

sudo modprobe slcan
sudo slcand -o -c -s0 /dev/ttyACM0 can0
sudo ifconfig can0 up
ifconfig # 应该能看到can0了

sudo ifconfig can0 txqueuelen 1000
cansend can0 999#DEADBEEF # Send a frame to 0x999 with payload 0xdeadbeef
candump can0 # Show all traffic received by can0

# 如果用的是candlelight

sudo modprobe gs_usb
ip link set can0 up type can bitrate 500000
ifconfig # 应该能看到can0了

1、配置can0,设置比特率

1
sudo ip link set can0 type can bitrate 250000

2、打开can0

1
sudo ip link set can0 up

3、查看信息

1
ip -details link show can0

4、ifconfig

关闭

1
sudo ifconfig can1 down

参考资料
https://blog.csdn.net/qq_41661878/article/details/129038794
https://blog.csdn.net/weixin_43849483/article/details/130454760
https://blog.csdn.net/gjy_skyblue/article/details/131322358
https://zhuanlan.zhihu.com/p/611724997
https://blog.csdn.net/u010443710/article/details/106834961?spm=1001.2014.3001.5501
https://blog.csdn.net/u010443710/article/details/107476570?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-5-107476570-blog-106834961.235^v38^pc_relevant_anti_vip&spm=1001.2101.3001.4242.4&utm_relevant_index=8
https://cloud.tencent.com/developer/article/1902433?areaSource=102001.19&traceId=-2_c0P3YX14MQMjVQtfcf