#list_channels.py -- List all CANlib channel
#This script uses canlib.canlib to list all CANlib channels and information
#about the device that is using them.
import argparse
from canlib import canlib
def print_channels():
for ch in range(canlib.getNumberOfChannels()):
chdata = canlib.ChannelData(ch)
print("{ch}. {name} ({ean} / {serial})".format(
ch=ch,
name=chdata.device_name,
ean=chdata.card_upc_no,
serial=chdata.card_serial_no,
))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="List all CANlib channels and information about them.")
args = parser.parse_args()
print_channels()
#
sampe output:
- Kvaser Leaf Light v2 (channel 0) (73-30130-00685-0 / 41683)
- Kvaser Virtual CAN Driver (channel 0) (00-00000-00000-0 / 0)
- Kvaser Virtual CAN Driver (channel 1) (00-00000-00000-0 / 0)
from canlib import canlib, Frame
from canlib.canlib import ChannelData
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" % (ChannelData(channel).device_name,
ChannelData(channel).card_upc_no))
ch.setBusOutputControl(outputControl)
ch.setBusParams(bitrate)
ch.busOn()
return ch
def tearDownChannel(ch):
ch.busOff()
ch.close()
print("canlib 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)
sample output:
(‘canlib version:’, VersionNumber(major=8, minor=23, release=None, build=None))
Using channel: Kvaser Leaf Light v2 (channel 0), EAN: 73-30130-00685-0
Using channel: Kvaser Virtual CAN Driver (channel 0), EAN: 00-00000-00000-0
Frame(id=217056256, data=bytearray(b’\x00\x00\x00\x00\x00\x00\x00\x00’), dlc=8L, flags=<MessageFlag.EXT: 4>, timestamp=7L)