JMRI Hardware Support - CAN Network
Author: Andrew Crosland Oct 2020 CBUS has it's own trademark and should not be referred to as MERG CBUS

# Hardware Support: CAN: Controller Area Networks

- Hardware
- Connecting
- JMRI CAN Tools
- Documentation

There's an emerging trend to develop model-railroad control systems that are based on standard CAN technology. JMRI provides support for the development and use of these. Two systems are described below.

## CAN Implementations

### OpenLCB

OpenLCB is a group of hobbyists and manufacturers who have come together to produce a future-oriented layout control bus, also called OpenLCB.
JMRI includes basic support for OpenLCB, and more is being developed along with OpenLCB itself.

The NMRA has decided to adopt OpenLCB and standardize it as Layout Command Control (LCC, formerly NMRAnet).

### CBUS®

Originally named CBUS by co-founder Gil Fuchs, CBUS is an 'open' protocol for Model Railway use. The protocol and other useful information is hosted on the MERG public website (see further support links). CBUS is not owned or controlled by MERG but the co-founders Gil Fuchs and Mike Bolton are active members. MERG members are able to purchase a range of CBUS kits.

CBUS adopted the Gridconnect scheme but with a slight modification.
The header is always HEX bytes and maps directly to the PIC CAN registers, left justified.

JMRI CBUS support is available for linking JMRI with CBUS, aimed at users.
Extensive details of the CBUS protocol are available to developers, see the CBUS Wiki hosted on the MERG public website.

There are many MERG module kits available to help running a layout.
Modules can be thought of as

- Consumers eg. a CANSOL solenoid module listening on the CANBUS for a taught event to throw a turnout
- Producers, eg. a CANACE3 Control Panel Switch sensing module, sending an event when a physical control panel button is pressed or a toggle switch changed ( multiplexing allows 128 switches on 1 module)
- Or both, eg a CANMIO Servo module listening for taught events to change turnout positions, then sending its own events on servo start moving, servo mid-point, also sending an event when completing moving the servo to its final position.

The 3,000+ MERG members are encouraged to to build and design their own modules, and with MERG being over 50 years old, there is a strong peer review process behind CBUS.
Module firmware updates can be performed over the CBUS network

JMRI includes support for CBUS events, also DCC over CBUS via a CANCMD kit (a fully featured DCC Command Station, mini booster and track programmer). This interfaces to JMRI and / or MERG handheld controllers over a CBUS network.

CBUS events are sent as one-to-many, enabling multiple modules to listen and change status on 1 single event. eg.

- Event sent from a turnout ( UK - point ) switch monitoring module, sending an "off event" when the point is thrown, an "on event" when closed.
- This event is received by a Relay module, switching the frog polarity of the physical point.
- Event also received by a LED module, changing the LEDs on a mimic panel representing the point position.
- Event also received by JMRI to use in Layout Panels et al.

JMRI can send and receive events for any module node and any module event.

## Supported Hardware

### Computer Interfaces

#### Various CAN-USB adapters are available:

TCH Technology
  TCH Technology CAN/USB provides a high-speed connection for model railroad use (site not accessible as of 2019)
grid connect
  GridConnect
lawicell
  LAWICEL AB CANUSB and CAN232 adapters.
MERG
  CANUSB4 A CBUS to USB interface, available in kit form to MERG members.
SPROG DCC Ltd
  CANISB A galvanically isolated CBUS to USB interface.
  USB-LCC A galvanically isolated LCC to USB interface.
CANable
  CANable An Open-Source USB to CAN Adapter, these use the CAN via Lawicell CANUSB System Connection.

#### Raspberry Pi Adapters

SPROG DCC Ltd
  PI-LCC Raspberry Pi adapter for OpenLCB/LCC (New product).

#### Other CAN Hardware Interfaces

Rocrail
  The CAN-GC1e Ethernet module can connect to JMRI via MERG Network Interface

MERG
  The CANPICAP enables direct CBUS connection to a Raspberry Pi via its 40 pin I/O connector, available in kit form to MERG members.
  The CANPiWi is based on the Raspberry Pi Zero W, and is an interface between Engine Driver / WiThrottle and CBUS.

SPROG DCC Ltd
  SPROG DCC Pi-SPROG 3 Plus CBUS DCC Command Station for direct attachment to a Raspberry-Pi via its 40-pin I/O connector.
  SPROG DCC SPROG 3 Plus CBUS DCC Command Station with integrated USB interface.

## Connecting

JMRI can provide the usual Sensor and Turnout features, once configured for OpenLCB or CBUS "Events".

Example System Naming:

| Sensor | Turnout | Light
--- | --- | --- | ---
OpenLCB |  |  |
CBUS | `MS18` ( Event 18 Active / Inactive ) | `MT+N2E18;-N2E18` ( Node 2 Event 18 Thrown / Closed ) | `ML18` ( Event 18 On / Off )

JMRI can connect to CAN networks in several ways:

### Via USB

To use any of the above adapters, select the appropriate device/connection type in the JMRI Preferences. If using a GridConnect or Lawicel device, be sure to set the appropriate baud rate under Advanced Preferences.
The TCH Technology adapter does this automatically, so no setting is required.

To connect to a CANUSB4, select "MERG" as system manufacturer, then "CAN via MERG CAN-RS or CAN-USB" as system connection.

### TCP/IP Network

It's also possible to attach via a TCP/IP connection (EtherNet or WiFi). This can either go to another JMRI instance running e.g. an OpenLCB hub, directly to an IP-CAN adapter, or a CAN Server. To configure one of these, open the Connections tab in the JMRI Preferences.

#### OpenLCB

- Select as System Manufacturer "OpenLCB"
- For System Connection, select "CAN via GridConnect Network Interface"
- Under settings, enter the IP address and TCP/UDP port for the destination.

  OpenLCB CAN Adapters advertise under mDNS/Bonjour, so you can use that to specify the IP address if JMRI is on the same network segment as your adapter. In that case, put "openlcbcan.local." (note trailing period!) in the IP address field.

  The port number for OpenLCB adapters is 23 by default.

- Set the connection protocol as desired, e.g. "OpenLCB", the Connection Prefix (usually "M") and Connection Name as needed.
- Click Save, and restart.

#### MERG via CBUS

- Select as system manufacturer MERG.
- For system connection, select CAN via MERG Network Interface
- Under settings, enter the IP address or hostname of the device with the physical connection to your CBUS network, eg "192.168.0.5" or "localhost"
  The CANPi has a default IP of 192.168.45.1 .
- The TCP port is 5550 ( TCP 5550 is reserved for CBUS use by the IANA ).
- Connection protocol - CBUS
- Connection prefix is normally "M" for CBUS.
- Click Save, and restart.

CBUS Server is a Node.js program, licensed under MIT to allow multiple network connections to a MERG CANUSB4.

## JMRI CAN Tools

- JMRI provides a basic Monitor tool for watching traffic on a CAN bus.
- For development, there are CAN Scripting tools for sending and receiving CAN frames. These can be very useful in testing hardware, etc.
- OpenLCB Send Frame tool The "Send OpenLCB Frame" tool lets you directly send CAN frames, and certain kinds of OpenLCB messages, eg "Verify Node" and "Send Request Consumers"
- A CBUS connection provides a number of tools, depending on the features of the connected hardware, e.g.:
  - Console - Tool for viewing and sending CAN frames / events.
  - Event Table - Monitors events and presents them in a table full of statistics.
  - Node Manager - Node Table and node configuration tools
  - Command Station Monitor - Loco session monitoring and Command Station Tools.
  - Send Frame - Send CAN frames or CBUS events
  - Event Capture Tool - Capture CBUS events
  - Event Request Monitor - Request Event status monitoring tool
  - Network Item simulation - Simulates Cbus Command stations and responds to event requests
  - CBUS bootloader - to update CBUS module firmware

## Documentation

### JMRI Help

OpenLCB Help

CBUS Support

JMRI Scripting for CAN frames.

### Third Party info

OpenLCB.org

merg.org.uk

SPROG DCC Ltd

wikipedia.org/wiki/CAN_bus

CBUS is a registered trade mark of Dr Michael Bolton