Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

PyVLCB Python Library for CBUS / VLCB

As part of my Pi Signalbox model railway controller project I created Python code for use with VLCB and CBUS. This provides a simple interface to the CANUSB4 module with the ability to control locos and accessories.

I’ve split the project so that pyvlcb remains the powerful underlying Python library for CBUS communication, while Pi SignalBox becomes the dedicated graphical application you see running on the Raspberry Pi today. The PyVLCB library is now available through the normal Python libraries through PyPi and the pip install command.

The library includes the following core classes:

  • VLCB: The core library module which provides simple commands to generate and interpret the CBUS / VLCB command strings.
  • CanUSB4: A class to simplify USB communications to the CAN USB 4 module.
  • VLCBFormat: A class to provide simple access to convert the data string into it's key components and values.
  • VLCBOpcode: Lookup tables for various CBUS / VLCB parameters. Primarily this provides a comprehensive list of the CBUS op-codes, with additional VLCB codes to be added in future. This is used by the VLCBFormat class for op-code lookup.

For more details see the PyVLCB library reference site.

Demonstration Code Examples

Demonstration examples are included in the demo folder of the PyVLCB Github page. This includes the following demonstration.

  • discover.py: Send a discover command to the VLCB bus and return a list of all modules found.
  • moveloco.py: Send commands to a loco, including turning sound on and off, and moving the loco forward.

Creating the Python software library

In the video below I explain the steps taken to create the Python library and to publish it on PyPi.

For more details see the guide to creating a Python library for PyPi.

Learning the CBUS protocol

In this video I explain about the steps I took to understand the CBUS protocol. This involved some reverse engineering through reading source code files, capturing traffic with Wireshark (see: using wireshark to monitor USB packet data), and finding useful documentation that explained the protocols.

Based on this I was able to create a GUI application that can send and receive messages to CBUS and can switch points and control a DCC locomotive. See below for the source code.

I've provided a copy of the document mentioned in the video: CBUS Developer Guide Version 6B

Source Code for PyVLCB

Whilst this is currently work in progress I've made all the current source code available on GitHub.

About CBUS

The following information is from my MERG / CBUS model railway page.

DCC Digital Command and Control for Model Railways with Raspberry Pi

This video explains about the CBUS DCC model railway controller kit from MERG. In the video I look at three kits, including a DCC command station, handset controller and a RJ22 connector kit. This provides a way of controlling model railway DCC locomotives and accessories, using CBUS and a home made controller. Generally the kits are fairly straight forward, but the hand-held controller is significantly more challenging to solder. I’m fairly proficient with a soldering iron and I found some of it difficult, although it shouldn’t be beyond the abilities of most.There is an alternative if you are interested in computer control.

See the following link to understand more about rotary encoders.

More Information

Find out more about MERG or about my other model railway projects.

Related projects

Also see:

Future projects

For the latest updates please:
Subscribe to the PenguinTutor YouTube Channel
and
Follow @penguintutor on Twitter

Previous Using Raspberry Pi with JMRI
Using Raspberry Pi with JMRI
Next 3D / 2D Design and 3D printing
3D / 2D Design and 3D printing