linux平台模拟生成CAN设备

前言

使用socketCan的过程中有时候没有can接口设备,但是需要测试一下can接口程序是否有问题,

此时需要系统模拟生成can设备,本文介绍linux平台模拟生成CAN设备的方法。

实现步骤

1.安装socketCan的驱动和can-utils的源码,具体可参考here(2-3-6);

2.加载虚拟CAN设备;

操作方法如下:

1) sudo modprobe vcan

2) sudo ip link add dev vcan0 type vcan

3) sudo ip link set up vcan0

You can use the following bash program to create a virtual CAN port (vcan) in Linux:

setup_vcan.sh

#!/bin/bash

# Using vcan0 as an example
CAN_PORT="vcan0"

sudo modprobe vcan
sudo ip link add dev $CAN_PORT type vcan  # 创建一个虚拟can接口并命名
sudo ip link set up $CAN_PORT

移除vcan接口

ip link del $CAN_PORT

完整版

sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
ifconfig -a
sudo apt install can-utils
cansend vcan0 123#abcd1234
candump vcan0

3.测试;

3.1使用命令行ifconfig查看是否加载虚拟CAN设备;

ifconfig -a

3.2测试能否正确发送和接收数据;

cansend vcan0 123#abcd1234
or 
candump vcan0

 

参考

1.linux模拟生成CAN设备

2.stack-overflow-How to create virtual CAN port on linux

3.测试收发功能

posted on 2018-04-18 17:01  鹅要长大  阅读(1031)  评论(0编辑  收藏  举报

导航