Linux下 USBCAN、socketcan使用

一、安装Kvaser Linux Drivers and SDK(不安装驱动,则可以使用kvaser的socketcan模式)

https://www.kvaser.com/download/

 放在home目录下(其他目录安装有问题,20221110测试其他目录也可以),在home目录下右键打开终端

解压

tar -xvzf linuxcan.tar.gz

下载并创建编译程序和用的内核头文件

sudo apt install build-essential

进入linuxcan目录

cd linuxcan

编译驱动程序

make

安装驱动程序(安装驱动的时候,请拔下kvaser)

sudo make install

 CAN 发送代码写好后,编译加上

-lcanlib

如果是ros包使用,linuxcan驱动安装后,不会生成.cmake文件,不需要用find命令查找头文件和库,只需在链接时加上canlib即可 

kvaser产品中没有终端电阻,因为终端电阻所需位置是基于特定的网络拓扑。终端电阻通常安装在CANbus主干网的最远处。CAN总线终端电阻的两个作用:1、提高抗干扰能力,确保总线快速进入隐性状态;2、提高信号质量

卸载kvaser驱动

在安装包linuxcan文件夹下打开终端

sudo make uninstall
sudo apt update

如果你安装后删除了安装包,要想删除驱动,再下载安装包到home,同样运行上述卸载命令,适用于kvaser、peakcan 

二、图莫斯CAN&LIN Analyser

1、runme.sh这个文件是window下编写的,需要在Linux下重新编码,文件类型改为Unix(Linux下新建即可)。

2、例子中用的头文件和库文件都替换为sdk文件下的

三、socketcan设置

   #! /bin/bash

sudo -S ip link    set can0    type can bitrate 500000 <<  /code >< /code >

四、vcan设置

#! /bin/bash
#加载模块
sudo modprobe vcan
sudo modprobe can-gw
 
#添加vcan,取名can0 can1 can2也行
sudo ip link add dev can0 type vcan
sudo ip link add dev can1 type vcan
sudo ip link add dev can2 type vcan
 
#启动vcan
sudo ip link set dev can0 up
sudo ip link set dev can1 up
sudo ip link set dev can2 up
 
#将多个vcan连接起来
sudo cangw -A -s can0 -d can1 -e
sudo cangw -A -s can1 -d can0 -e
sudo cangw -A -s can0 -d can2 -e
sudo cangw -A -s can2 -d can0 -e
sudo cangw -A -s can1 -d can2 -e
sudo cangw -A -s can2 -d can1 -e
 
#关闭/删除vcan
#sudo ip link del dev vcan0
#sudo ip link del dev vcan1
#sudo ip link del dev vcan2

五、socketcan自动配置

可以将下面脚本写成.sh脚本,也可以加入~.bashrc中

#!/bin/bash
#下方123456是密码,自行修改

ifconfig -a | grep can0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo -e "\n\033[32mNo can0 this device\033[0m"
else
 
      ifconfig | grep can0 > /dev/null 2>&1
      if [ $? -ne 0 ]; then
sudo -S ip link  set can0 type can bitrate 500000 <<EOF
123456
EOF
        sudo ip link  set up can0
        ifconfig | grep can0 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
          echo -e "\n\033[32msetup can0 done\033[0m"
        else
          echo -e "\n\033[31msetup can0 failed\033[0m"
 
        fi
      else
        echo -e "\n\033[32mcan0 is ready\033[0m"
      fi
 
 
fi

 

#enable socketcan0
ifconfig -a | grep can0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo -e "\n\033[32mNo can0 this device\033[0m"
else
 
      ifconfig | grep can0 > /dev/null 2>&1
      if [ $? -ne 0 ]; then
sudo -S ip link  set can0 type can bitrate 500000 <<EOF
123456
EOF
        sudo ip link  set up can0
        ifconfig | grep can0 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
          echo -e "\n\033[32msetup can0 done\033[0m"
        else
          echo -e "\n\033[31msetup can0 failed\033[0m"
 
        fi
      else
        echo -e "\n\033[32mcan0 is ready\033[0m"
      fi
 
 
fi

#enable socketcan1
ifconfig -a | grep can1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo -e "\n\033[32mNo can1 this device\033[0m"
else
 
      ifconfig | grep can1 > /dev/null 2>&1
      if [ $? -ne 0 ]; then
sudo -S ip link  set can1 type can bitrate 500000 <<EOF
123456
EOF
        sudo ip link  set up can1
        ifconfig | grep can1 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
          echo -e "\n\033[32msetup can1 done\033[0m"
        else
          echo -e "\n\033[31msetup can1 failed\033[0m"
 
        fi
      else
        echo -e "\n\033[32mcan1 is ready\033[0m"
      fi
 
 
fi

#enable socketcan2
ifconfig -a | grep can2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
    echo -e "\n\033[32mNo can2 this device\033[0m"
else
 
      ifconfig | grep can2 > /dev/null 2>&1
      if [ $? -ne 0 ]; then
sudo -S ip link  set can2 type can bitrate 500000 <<EOF
123456
EOF
        sudo ip link  set up can2
        ifconfig | grep can2 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
          echo -e "\n\033[32msetup can2 done\033[0m"
        else
          echo -e "\n\033[31msetup can2 failed\033[0m"
 
        fi
      else
        echo -e "\n\033[32mcan2 is ready\033[0m"
      fi
 
 
fi

 

 

posted @ 2020-05-15 16:29  chenjian688  阅读(2931)  评论(0编辑  收藏  举报