For my latest project, the Mariannenbahn, I needed to find a way to control a Märklin model railway from different rooms. While this is obviously possible using multiple Märklin Central or Mobile Stations, it was not an option at the time as the project already hit its budgetary ceiling.
Thanks to an outstanding blog post on http://www.ifoedit.com as well as Stefan Krauß’ homepage , a solid groundwork was already present.
The Märklin Digital Connector Box is a device containing a signal processor that generates the digital signal on the rails. It communicates with controllers, such as the Mobile Station , via CAN bus using a protocol defined and thankfully published by Märklin. It is therefore possible to use any device capable of transmitting data over CAN to control the model railway. Using a Raspberry PI together with a ready-made CAN extension module seemed like a good and safe solution, as various people already proved that this setup usually works.
Since it is usually common to use the Raspberry PI via SSH instead of a display connected, a piece of software taking commands via Ethernet and hand them over to the CAN bus is required. That way, it is possible to install the PI somewhere close to the Märklin Digital Connector Box and the railway layout. Connected to the LAN, it will take commands from any Ethernet-capable device such as personal computers or even smartphones. Gerhard Bertelsmann has written a wonderful C program called
can2udp for exactly that purpose. It will hand over commands between Ethernet/UDP and the CAN bus.
As a final step, software to produce the actual commands to be transmitted to the Digital Connector Box is required. The most obvious solution available for this task is Rocrail.
After some research, I decided to get a PiCAN2 board form SK Pang Electronics . Getting it running is quite straightforward. On the downside, I found it inevitable to solder a 2-way header pin as jumper bridge to the board myself to be able to use the 120Ω terminator already present on the board.
Activating the terminator resistance is described in the manual. Mounting the board is easy, it only fits one direction. It is possible to either use the DB9 Connector or the screw terminal on the PiCAN2 to connect it to the Märklin Connector Box. The pin assignment on the Märklin Connector Box is described by Stefan Krauß.
I was using a Raspberry PI 2 running the latest Raspbian. I wasn’t able to get the PiCAN2 running with the latest (4.4.16) firmware though. I used
rpi-update to go back to 4.1.21 which worked for me some time ago. I did not do any investigation on which exact version introduces the breaking changes. If you find out, please let me know. To install a working firmware,
rpi-update can be used with the version’s commit hash:
sudo rpi-update dea25fa62132365c11087e51e416df656db28bf3
Following the manual, the following three lines have to be appended to
After a reboot, the CAN interface can be brought up.
sudo ip link set can0 up type can bitrate 250000
sudo ifconfig can0 up
See the troubleshooting section on information about how to verify the interface is up and running properly.
can2udp is a beautiful C program that hands over messages from Ethernet/UDP to CAN and back. The usage is:
Usage: can2udp -l <port> -d <port> -i <can interface>
-l <port> listening UDP port for the server - default 15731
-d <port> destination UDP port for the server - default 15730
-b <broadcast_addr> broadcast address - default 255.255.255.255
-i <can int> can interface - default can0
-f running in foreground
-v verbose output (in foreground)
Given the default setup, it is sufficient to call
./can2udp -f -v, which will bring up
can2udp in foreground with verbose output. It will send a certain initialization package to the Märklin Digital Connector Box on startup.
Almost any literature on the topic is using Rocrail as client software. Due to the lack of any real alternative, it is what I am using too at the moment.
The setup described in this post simulates the use of a Märklin Mobile Station 2, so the setup for Rocrail is identically to that scenario and can be found on their webpage or on http://www.ifoedit.com.
Where to buy
I was able to get a Märklin Digital Connector Box (60113) together with the required Märklin 36VA Switched Mode Power Pack (66361) from a local retailer. Märklin appears not to be selling the Digital Connector Box anymore, but still its quite easy to get one on eBay or Amazon.
If the CAN interface is set up correctly, it will be listed in the output of
can0. Both the
bcm2835 and the
mcp251x controllers must be listed in the output of
lsmod. Errors in the initialization process will also show up in the output of
dmesg. It might be wise to