第一步:在原厂的网址:http://www.peak-system.com/fileadmin/media/linux/version-history.html中寻找对应linux内核版本的driver(我当时使用的是内核4.15.0,使用的是8.5.1版本,不使用对应的版本,在编译时会出现内核版本的问题)。
第二步:解压编译
tar -xzf peak-linux-driver-x.x.tar.gz
cd peak-linux-driver-x.x.tar.gz
//使用chardev,适合peakcan
make all NET=NO PCI=NO IAS=NO DNG=NO PCC=NO
//或者使用netdev,适合socketcan
//make all NET=NETDEV_SUPPORT PCI=NO ISA=NO DNG=NO PCC=NO
sudo make install
sudo modprobe pcan
可能出现的错误:
1.其中,可能在make过程中出现错误,把226行dev->trans_start=jiffies和439行dev->last_rx=jiffies注释掉,写入netdev_get_tx_queue(dev,0)->trans_start=jiffies。
2.出现popt.h not found的错误,则输入
sudo apt-get install libpopt-dev
3.编译chardev出现内核版本问题。若出现/arch/x86/include/asm/uaccess.h:31:9: error: dereferencing pointer to incomplete type的错误,则将drivers中的pcan_main.c中的#include <asm/uaccess.h>更改为#include <linux/uaccess.h>
第三步:检测驱动安装是否成功
cat /proc/pcan
//显示这个表示成功
# *------------- PEAK-System CAN interfaces (www.peak-system.com) -------------
# *------------- Release_20130814_n (7.9.0) Jan 9 2014 11:17:31 --------------
# *---------------------------- [mod] [par] [usb] -----------------------------
# *--------------------- 2 interfaces @ major 251 found -----------------------
# *n -type- ndev --base-- irq --btr- --read-- --write- --irqs-- -errors- status
# 32 usb -NA- 00000002 001 0x001c 00000000 00000000 00000000 00000000 0x0000
进一步测试
//设定pcanusb32波特率
echo "i nom_bitrate=1M" > /dev/pcanusb32
//将ECAN与USB转CAN对接,USB转CAN连上windows上的USB_CANTOOLS进行收发测试。注意两个设备波特率要一致。
echo "m s 0x123 8 01 02 03 04 05 06 07 08" > /dev/pcanusb32
进一步测试
sudo modprobe can
sudo modprobe can-dev
sudo modprobe can-raw
//若使用虚拟CAN接口的话sudo modprobe vcan,下面的can0一般变成vcan0,can变成vcan
//然后就可通过ip指令设置CAN的波特率(此处是1M)
sudo ip link set can0 type can bitrate 1000000
//使能socketCAN,使能后该端口才能使用cansend
sudo ip link set up can0
//失效socketCAN
//sudo ip link set down can0
//可用ifconfig查看哪些端口正处于up状态,ifconfig -a是查看无论down还是up的端口
//安装can-utils用来测试
sudo apt install can-utils
//发送命令测试
cansend can0 123#1122334455667788
//显示接收到的消息
candump -x can0
电机转动测试
//SDO功能测试
cansend can0 000#0104
cansend can0 604#2B40600006000000
cansend can0 604#2B40600007000000
cansend can0 604#2B4060000F000000
cansend can0 604#2B40600000000000
//PDO功能测试
cansend can0 000#0104
cansend can0 604#2B40600006000000
cansend can0 604#2B40600007000000
cansend can0 604#2B4060000F000000
cansend can0 404#EB03
cansend can0 080#
cansend can0 404#FB01
cansend can0 080#
cansend can0 604#2B40600000000000
另外附上Pcanview在Linux下的安装:
wget -q http://www.peak-system.com/debian/dists/`lsb_release -cs`/peak-system.list -O- | sudo tee /etc/apt/sources.list.d/peak-system.list
wget -q http://www.peak-system.com/debian/dists/wheezy/peak-system.list -O- | sudo tee /etc/apt/sources.list.d/peak-system.list
wget -q http://www.peak-system.com/debian/peak-system-public-key.asc -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get install pcanview-ncurses
//运行pcanview
pcanview