一、下载并安装Kvaser Linux Driver和SDK
$ cd ~
$ # We need tools for building (gcc compiler, make, etc)
$ sudo apt install build-essential
$ # Download latest version of linuxcan, Kvaser Linux Driver and SDK
$ wget --content-disposition "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130980754&utm_status=latest"
$ tar xf linuxcan.tar.gz
$ cd linuxcan
$ make
$ sudo make install
二、验证硬件
现在我们将Kvaser接口插入USB端口并运行listChannels示例程序以验证驱动程序是否已正确加载并且我们的设备已被识别:
$ cd canlib/examples
$ ./listChannels
Found 2 channel(s).
ch 0: Kvaser USBcan Pro 2xHS v2 73-30130-00752-9, s/n 1014, v3.10.0.496 (mhydra v8.24.533)
ch 1: Kvaser USBcan Pro 2xHS v2 73-30130-00752-9, s/n 1014, v3.10.0.496 (mhydra v8.24.533)
三、安装依赖包
Kvaser Linux驱动程序和SDK仅包含驱动程序,canlib和LINlib,其余库(kvaMemoLibXML,kvmlib,kvlclib和kvaDbLib)1放在Kvaser Linux SDK库kvlibsdk中。 我们以类似于Kvaser Linux Driver和SDK的方式构建和安装它们,但我们首先需要安装一些依赖包。
$ cd ~
$ # The package libxml2-dev is needed in order to build kvamemolibxml.
$ sudo apt install libxml2-dev
$ # The packages libssl-dev and zlib1g-dev are needed to build kvlclib.
$ sudo apt install libssl-dev zlib1g-dev
$ # Download latest version of kvlibsdk, Kvaser Linux SDK Library
$ wget --content-disposition "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130981966&utm_status=latest"
$ tar xf kvlibsdk.tar.gz
$ cd kvlibsdk
$ make
$ sudo make install
四、安装python canlib
$ cd ~
$ wget --content-disposition "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130981911&utm_status=latest"
$ unzip -q pycanlib.zip
$ cd pycanlib
$ pip install canlib-1.6.615*.whl
五、测试收发小程序
编写一个小的Python程序,它在通道1上发送一条CAN消息,并在通道0上接收相同的CAN消息:
# 01_sendReceiveSingleCanMsg.py
from canlib import canlib, Frame
def setUpChannel(channel=0,
openFlags=canlib.Open.ACCEPT_VIRTUAL,
bitrate=canlib.canBITRATE_500K,
outputControl=canlib.Driver.NORMAL):
ch = canlib.openChannel(channel, openFlags)
print("Using channel: %s, EAN: %s" % (
canlib.ChannelData(channel).device_name,
canlib.ChannelData(channel).card_upc_no))
ch.setBusOutputControl(outputControl)
ch.setBusParams(bitrate)
ch.busOn()
return ch
def tearDownChannel(ch):
ch.busOff()
ch.close()
print("canlib dll version:", canlib.dllversion())
ch0 = setUpChannel(channel=0)
ch1 = setUpChannel(channel=1)
frame = Frame(id_=100, data=[1, 2, 3, 4], flags=canlib.MessageFlag.EXT)
ch1.write(frame)
while True:
try:
frame = ch0.read()
print(frame)
break
except (canlib.canNoMsg) as ex:
pass
except (canlib.canError) as ex:
print(ex)
tearDownChannel(ch0)
tearDownChannel(ch1)
参考内容:https://www.kvaser.com/developer-blog/running-python-wrapper-linux/