Software Dev: CAN bus in Windows 11

How to enable CAN network driver support in Windows Subsystem for Linux - using Windows for embedded software development

Embedded
Hardeep
October 28, 2022

Working on automotive software development, being able to test CAN data transmission is essential. An easy way to test this without requiring CAN hardware is to use a Virtual Local CAN Interface (vcan). The Linux kernel comes with the ability to include the vcan driver (along with other can drivers) in the kernel itself or as a module that can be loaded. Linux itself is very friendly for software development and might be the OS of choice at work. As great as Linux is, sadly, during work away from development, there are a number of drawbacks. Here tools like Microsoft office and Microsoft Teams for collaboration offer benefits that bring Windows to the forefront. In an attempt to get the best of both worlds, Linux and Windows, we get vcan working on WSL2 (Windows Subsystem for Linux).

Microsoft have publically said that WSL2 has no plans on allowing vcan in the default kernel. With WSL2 making it a challenge to build modules that can be loaded after the initial kernel build, this leaves creating a custom kernel as the only option. Below are the steps taken from this thread, to get a custom kernel with vcan working in WSL2.

Step 1: Build the kernel

Firstly, we need to build a custom kernel from source.

This can be done with the help of a script we provide in beam-labs on Github 🚀 

https://github.com/Beam-Connectivity/beam-labs/tree/main/vcan-in-wsl2

Step 2: Bring up the interface

When you next launch wsl2, it should run with your custom kernel and allow you to load the vcan modules.

The following commands should run without any errors:

sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ifconfig vcan0 up

Step 3: Profit

With this module loaded you’ll be able to make use of it during testing. There are a number of great tools available such as can-utils for Linux and taking this a step further in Windows 11, USB devices can be forwarded to wsl2.

So not only is simulation testing possible from a host Windows PC, but connecting a USB to can transceiver allows the same test to run on real hardware with only an interface change from vcan to slcan required.