前言
嵌入式linux下的CAN接口,如何方便地测试验证是一个问题。实际上Linux系统已经自带了测试验证的软件工具。
环境
硬件:NXP IMX6Solox,Kvaser Can Card
软件:嵌入式Linux,串口控制终端,Kvaser CanKing
使用2根线飞出CAN L和CAN H,连接上CAN卡。
操作方法
1. 查找CAN节点
使用ifconfig -a
# ifconfig -a
can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:30
can1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:31
eth0 Link encap:Ethernet HWaddr 72:5B:8C:6D:BF:77
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
sit0 Link encap:IPv6-in-IPv4
NOARP MTU:1480 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
如果驱动正常,就能看到can节点(如上图的can0和can1)。
2. 设置can节点
以can0节点为例
ifconfig can0 down
ip link set can0 type can bitrate 500000 triple-sampling on
ifconfig can0 up
3. 查看can节点状态
ip -details link show can0
# ip -details link show can0
2: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN mode DEFAULT group default qlen 10
link/can promiscuity 0
can state STOPPED (berr-counter tx 0 rx 0) restart-ms 0
flexcan: tseg1 4..16 tseg2 2..8 sjw 1..4 brp 1..256 brp-inc 1
flexcan: dtseg1 1..39 dtseg2 1..8 dsjw 1..8 dbrp 1..1024 dbrp-inc 1
clock 30000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535
4. 发送接收can消息
发送消息:cansend;
# cansend can0 123#000102030405060708
接收消息: candump
# candump can0
can0 001 [8] 01 02 03 04 05 06 07 08
can0 001 [8] 01 02 03 04 05 06 07 08
can0 001 [8] 01 02 03 04 05 06 07 08
can0 001 [8] 01 02 03 04 05 06 07 08
最后
Linux提供了一整套的can的软件工具,极大方便了can的测试验证。
遗留问题
1. Linux系统下的can消息的发送和读取采用的是什么API?
2. Linux系统下的can为什么被设计成这样?