Developer Blog

12/04/2018 by Lars-Göran Fredriksson

How to use Kvaser Virtual Device Channel and Bus

Dev_virt1

The purpose of this document is to show how to use the virtual devices created by Kvaser Drivers.

We will cover Virtual Device, Virtual Channel and Virtual Bus.

We will focus on the WINDOWS drivers, but some information can be used also for LINUX in appendix A.

Here you will find information how to install, modify and use them.

Key words:
Virtual Device Emulates a Kvaser CAN interface
Virtual Channel Emulates a Kvaser CAN Channel
Virtual Bus Provides a virtual cable connecting the virtual channels


Driver for Kvaser CAN hardware, version 5.22 or newer

The Kvaser Drivers are used in this guide.

  • Download and install version 5.22 or newer.

You can find the drivers here: https://www.kvaser.com/download/


Kvaser CANlib SDK

The CANlib Software Development Kit (Kvaser CANLib SDK) is used in this guide.

  • Download and install version 5.22 or newer.

You can find the SDK here: https://www.kvaser.com/download/


Hardware

  • No hardware (Kvaser Can interface) is needed to use this guide.

Installing Kvaser CAN Drivers on Windows

When installing Kvaser CAN Drivers, one virtual device is automatically installed.

Dev_virt2

Kvaser Device Guide

Use the “Kvaser Device Guide” to view installed devices.

Dev_virt3

Default is one device with two channels. The two channels are connected to each other with a virtual loopback cable (Virtual Bus).

It is possible to add up to 16 Virtual Devices which will be shown in the next section.


Adding Virtual Devices in Windows

Before we add more Virtual Devices, we must change two values in the registry.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\KVASER AB\CANDriver 1.0\Drivers\kcanv]
"MaxCards"=dword:00000002
[HKEY_LOCAL_MACHINE\SOFTWARE\KVASER AB\CANDriver 1.0\Drivers\kcanv]
"MaxCards"=dword:00000002

By default (2018-02-08) the value is set to 2 Virtual Devices, change this to 16 Virtual Devices or whatever you need. Windows allows maximum 16 devices.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\KVASER AB\CANDriver 1.0\Drivers\kcanv]
"MaxCards"=dword:00000010
[HKEY_LOCAL_MACHINE\SOFTWARE\KVASER AB\CANDriver 1.0\Drivers\kcanv]
"MaxCards"=dword:00000010

Values are in HEX! (More information on hexadecimal format here.)

Copy the text above to a textfile, name it to “Allow16KvaserVirtualDevices.REG”, and “run” it by double clicking on it. You can also do the changes directly in the registry with Regedit.EXE.

Dev_virt4

I feel lucky! Pressing Yes!

Dev_virt5

Ok, check the value in RegEdit:

Dev_virt6

Done!


Adding Virtual Devices

Start the “Hardware Wizard”, by pressing “WinButton + R”, and type HDWWIZ and click OK.

(Before you start creating 15 more devices, scroll down and read the section “Change number of channels”!)

Dev_virt7

Can also be started from a command prompt.

Dev_virt8

Press Next.

Dev_virt9

Select Manual Install (Advanced) and press Next.

Dev_virt10

Select “CAN Hardware (Kvaser)”, and press Next.

Dev_virt11

Scroll down to “Kvaser Virtual CAN Driver”, and press Next.

Dev_virt12

O yes! Press Next!

Dev_virt13b

It is possible to add 16 Kvaser Virtual CAN Drivers, if you try to add one more, Windows will show you a message like this:

Dev_virt14

And in the Device Manager you will see:

Dev_virt15

But let us assume that 16 Virtual Devices with 2 Virtual Channels each are enough for this blog.


Verifying setup

Dev_virt16

We shall now see the number of Devices we selected to install and also the CANLib channel numbers. In this example, there are 16 Virtual Devices and 32 Virtual Channels.


Change number of channels

It is possible to change the number of channels on the virtual devices. By default, the driver creates two channels on each Virtual Device.

You must edit a setting in the registry and reboot your computer to be able to change the number of channels.

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\kcanv\Parameters]
"ChannelCount"=dword:00000005

It is possible to create maximum 8 Virtual Channels on one Virtual Device.

A reboot of the system is required after changing this value.

My intention right now is to create two Virtual Devices with five Virtual Channels each. I need them for my tests with Virtual Buses in next section.

Dev_virt17

Virtual Buses

Each default Virtual Device has by default all Virtual Channels connected to one private Virtual Bus (normally Virtual Bus #0). When we create two devices with 5 channels, all five channels on each device is connected to Virtual Bus #0. However, there is no connection between the two Virtual Devices, even if they are connected to the same Virtual Bus number! Virtual Channels can only communicate with Virtual Channels located on the same Virtual Device.

Each Virtual Device, creates a private set of 32 Virtual Buses.


Private Virtual Bus

Dev_virt18

There are some errors in the example above. Channel 2 can not communicate with channel 5, and channel 3 can not communicate with channel 4. (Channel 0 and channel 1 can communicate).

The Virtual Buses are not shared between the Virtual Interfaces!


Using Virtual Device with CANLib

You can open the Virtual Channel in CANLib, with the same commands as a physical CAN interface. The only modification needed is that we must add a FLAG.

MyHnd[ChNo] := canOpenChannel(ChNo, flags OR canOPEN_ACCEPT_VIRTUAL);

We must add the flag canOPEN_ACCEPT_VIRTUAL  when we use the command canOpenChannel(). You can keep this flag in your software, because it does not stop you from opening a regular channel.

 

There are 32 Virtual Buses on each Virtual Device, and you can connect any Virtual Channel to any Virtual Bus on the same Virtual Device.


Disconnect a Virtual Channel from a Virtual Bus

We can disconnect a channel by using the command canIoCtl().

Var
 BusNo:INT32;
Begin
 BusNo  := 0;
 canBusOff(MyHnd);
 canIoCtl(MyHnd,canIOCTL_DISCONNECT_FROM_VIRTUAL_BUS,@BusNo,4);
End;

It seems like BusOff() and BusOn() is not needed, but I think it looks a bit better if we use them before modifying the buses, even if they are virtual.

But we must know the Virtual Bus number! If you have lost track of the bus number, then use something like this (disconnects all):

// This routine disconnects the channel from ALL virtual buses!
Var
  BusNo:INT32;
Begin
 BusNo  := 0;
 canBusOff(MyHnd);
 For BusNo := 0 to 31 do
 begin
  canIoCtl(MyHnd,canIOCTL_DISCONNECT_FROM_VIRTUAL_BUS, @BusNo,4);
 end;
End;

Now we are pretty sure that the Virtual Channel is disconnected from the Virtual Bus.


Connect a Virtual Channel to a Virtual Bus

We can connect a channel by using the command canIoCtl().

(Always disconnect the channel first, sometimes it is possible to connect to multiple buses.)

Var
  BusNo:INT32;
Begin
  BusNo  := 7;
  canIoCtl(MyHnd,canIOCTL_CONNECT_TO_VIRTUAL_BUS,@BusNo, 4);
  canBusOn(MyHnd);
End;

Now we have connected to Virtual Bus no 7.

Important! You must disconnect a channel!

The channel is not disconnected from the “old” Virtual Bus when connecting to a “new” Virtual Bus. All disconnection and connections must be initiated by your software.

Example, two Virtual Devices with five Virtual Channels

Dev_virt17

Let us set up two devices with five channels each (the code is not complete).

Begin
  BusNo0  := 1;  BusNo1  := 1;
  BusNo2  := 3;  BusNo3  := 3;  BusNo4  := 3;
  BusNo5  := 1;  BusNo6  := 1;  BusNo7  := 1;  BusNo8  := 1;  BusNo9  := 1;


  canIoCtl(MyHnd0, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo0, 4);
  canIoCtl(MyHnd1, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo1, 4);
  canIoCtl(MyHnd2, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo2, 4);
  canIoCtl(MyHnd3, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo3, 4);
  canIoCtl(MyHnd4, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo4, 4);
  canIoCtl(MyHnd5, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo5, 4);
  canIoCtl(MyHnd6, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo6, 4);
  canIoCtl(MyHnd7, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo7, 4);
  canIoCtl(MyHnd8, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo8, 4);
  canIoCtl(MyHnd9, canIOCTL_CONNECT_TO_VIRTUAL_BUS, @BusNo9, 4);
End;

Virtual Device 0
CH0 and CH1 can communicate on VB1 (Virtual Bus 1).
CH2,CH3 and CH4 can communicate on VB3 (Virtual Bus 3).

Virtual Device 1
CH5-Ch9 can communicate on VB1.

But CH0 and CH1 can not communicate with CH5-CH9, even if they seems to be on the same bus, because they are on different Virtual Devices. Each Virtual device creates a private set of Virtual Buses.

Appendix A, LINUX

Can I use Virtual Devices with LINUX?

Yes, in Linux you make a change in the source code (virtualcan.h):

#define NR_CHANNELS         2
#define MAX_CHANNELS        NR_CHANNELS

#define NR_VIRTUAL_DEV      1
#define VIRTUAL_MAX_DEV     NR_VIRTUAL_DEV

And start the devices with:
/usr/sbin/virtualcan.sh start

In all probability you need to build something before starting the virtual devices, but that is beyond my knowledge!

If you are a Linux user and need more help at this point, please get in touch with our support team.

Author Image

Lars-Göran Fredriksson

Lars-Göran Fredriksson is a Field Application Engineer for Kvaser AB. His background is in geographic information system (GIS) and Remote Sensing and his current focus is on connecting the deep knowledge of Kvaser's developers with the practical questions of our end users. If you doubt his passion for CAN, just know that his first week in the office he created an interactive CAN Trivia game that sent the office scouring the halls for the correct answers. He is a passionate fisherman who would like to develop new environmentally friendly fishing methods. Biggest catch and release fish is for the moment a Bluefin Tuna at appr 325kg / 715lbs.