Linux 下Can的配置

CAN总线配置

在Linux系统中,CAN总线接口设备作为网络设备被系统进行统一管理。在控制台下, CAN总线的配置和以太网的配置使用相同的命令。

在控制台上输入命令:

 
 
  1. ifconfig –a 

可以得到以下结果:

 
 
  1. can0   Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
  2.        NOARP  MTU:16  Metric:1  
  3.        RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  4.        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  5.        collisions:0 txqueuelen:10   
  6.        RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)  
  7.        Interrupt:18   
  8. eth0   Link encap:Ethernet  HWaddr 00:50:c2:22:3b:0e    
  9.        UP BROADCAST MULTICAST  MTU:1500  Metric:1  
  10.        RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  11.        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  12.        collisions:0 txqueuelen:1000   
  13.        RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)  
  14. eth1   Link encap:Ethernet  HWaddr 00:50:c2:22:3b:60    
  15.        UP BROADCAST MULTICAST  MTU:1500  Metric:1  
  16.        RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
  17.        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
  18.        collisions:0 txqueuelen:1000   
  19.        RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)  
  20.        Interrupt:41 Base address:0xe000   
  21. lo     Link encap:Local Loopback    
  22. inet addr:127.0.0.1  Mask:255.0.0.0  
  23. inet6 addr: ::1/128 Scope:Host  
  24. UP LOOPBACK RUNNING  MTU:16436  Metric:1  
  25. RX packets:256 errors:0 dropped:0 overruns:0 frame:0  
  26. TX packets:256 errors:0 dropped:0 overruns:0 carrier:0  
  27. collisions:0 txqueuelen:0   
  28. RX bytes:19952 (19.9 KB)  TX bytes:19952 (19.9 KB)  

在上面的结果中,eth0和eth1设备为以太网接口,can0设备为CAN总线接口。接下来使用ip命令来配置CAN总线的位速率:

 
 
  1. ip link set can0 type can tq 125 prop-seg 6phase-seg1 7 phase-seg2 2 sjw 1 

也可以使用ip命令直接设定位速率:

 
 
  1. ip link set can0 type can bitrate 125000 

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

 
 
  1. ip -details link show can0 

当设置完成后,可以使用下面的命令使能can0设备:

 
 
  1. ifconfig can0 up 

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

 
 
  1. ifconfig can0 down 

###########################################

1、
  首先配置can0
  ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1
  这时dmesg可以看到sja1000_fpga_pci 0000:07:04.0: setting BTR0=0x01 BTR1=0x1c
  2、
  ip -details link show can0 查看一下
  can0: mtu 16 qdisc pfifo_fast state UNKNOWN qlen 10
    link/can 
    can state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 
    bitrate 500000 sample-point 0.875 
    tq 125 prop-seg 6 phase-seg1 7 phase-seg2 2 sjw 1
    sja1000: tseg1 1..16 tseg2 1..8 sjw 1..4 brp 1..64 brp-inc 1
    clock 16000000
3、接收测试,接收测试软件发送的帧:
# ./candump can0
interface = can0, family = 29, type = 3, proto = 1
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 
<0x00000002> [8] 70 01 02 03 04 05 06 07 

4、发送测试
./cansend can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
interface = can0, family = 29, type = 3, proto = 1
测试软件上能看到接收的帧

5、重启
使用内核文档说的ip link set can0 type can restart-ms 100 会报
RTNETLINK answers: Device or resource busy
使用ifconfig can0 down ;ip link set can0 up type can
即可

并没有掌握sja1000波特率的配置。摸索出4种常见波特率:
250kbps:
ip link set can0 type can tq 125 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1   
125kbps:                                    
ip link set can0 type can tq 250 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1                                       
500kbps:
ip link set can0 type can tq 75 prop-seg 6  phase-seg1 7 phase-seg2 2 sjw 1 
1000kbps:
ip link set can0 up type can bitrate 2000000
常见用法:
ip -details link show can0
ifconfig can0 down ;ip link set can0 up type can
./candump can0 
./cansend   can0 -e 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6

   2>. 使用loopback模式测试:
使用loopback模式可测试本机的can驱动是否正常工作.
# ip link set can0 down
# ip link set can0 type can bitrate 50000 loopback on
xilinx_can e0008000.can can0: bitrate error 0.0%
# ip link set can0 up
# candump can0 &
# cansend can0 123#11223344556677

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux中,可以使用SocketCAN工具配置和管理CAN及CAN FD接口。以下是一些常用的Shell命令,用于配置CAN FD接口: 1. `ip link set canX type can bitrate <bitrate> dbitrate <data_bitrate>`:设置CAN FD接口的比特率。将`canX`替换为实际的接口名称(如`can0`),`<bitrate>`是CAN数据帧的比特率,`<data_bitrate>`是CAN FD数据帧的比特率。 2. `ip link set canX up`:启用CAN FD接口。将`canX`替换为实际的接口名称。 3. `ip link set canX down`:禁用CAN FD接口。将`canX`替换为实际的接口名称。 4. `ip link show canX`:显示CAN FD接口的详细信息和状态。将`canX`替换为实际的接口名称。 5. `canconfig canX bitrate <bitrate> dbitrate <data_bitrate>`:设置CAN FD接口的比特率(使用can-utils工具)。将`canX`替换为实际的接口名称,`<bitrate>`是CAN数据帧的比特率,`<data_bitrate>`是CAN FD数据帧的比特率。 6. `cansend canX <can_id>#<data>`:发送CAN数据帧到指定的CAN FD接口。将`canX`替换为实际的接口名称,`<can_id>`是CAN标识符,`<data>`是发送的数据。 7. `candump canX`:监听CAN FD接口接收到的数据帧。将`canX`替换为实际的接口名称。 请注意,上述命令中的`canX`是指具体的CAN FD接口名称,可以根据实际情况进行替换。此外,使用SocketCAN工具还可以进行更高级的配置和管理,如设置过滤规则、接收回调等,可以根据需求查阅相关文档和资料进行更深入的学习和使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值