CAN Bus Bridge

CAN bus is a standardized electrical bus for vehicles (cars and trucks). All CAN nodes (devices) are connected to each other through a physically conventional two wire bus. The wires are a twisted pair with a 120 Ω (nominal) characteristic impedance. CAN messages are sent and received by CAN nodes over the CAN bus. The CAN bus is a broadcast domain, in which all connections to the bus can reach each other by broadcast.

Example CAN Bus

This example replaces the electrical CAN bus with a bridge Thing connected to CAN nodes (devices) over Wifi. The bridge provides a broadcast domain for the CAN messages, just like the original electrical bus.

Beside being a simple example of a Merle bridge, the example has some practical advantages over the traditional electrical CAN bus:

  • The bridge electrically isolates nodes. Sometimes this is desirable if the nodes are on different ground domains or are otherwise electrically noisy.

  • The bridge sees all messages and can provide a tap to a network sniffer such as Wireshark to view/capture message traces during run-time, which is useful for debugging CAN messages.

  • Nodes can be added and removed readily without having to physically wire each to the electrical bus. CAN specification limits the wire from a node to the bus to no more than 0.5m in length. With this example, the wire length is effectively the range of the Wifi.

  • The bridge can bridge nodes running at different CAN bus speeds.

  • The traffic between a node and the bridge is encrypted inside an SSH tunnel. If a node doesn't have the SSH key, it's not going to be able to connect to the bridge.

Hardware Setup

CAN Node Parts List

  • Any model Rapsberry Pi with Wifi (except Pico)

  • PICAN hat or other CAN hat


CAN Bridge Parts List

  • A laptop or other system on the local Wifi network or on the Internet

The CAN node is wired to its CAN bus using the CAN hat and Wifi is connected.

Example CAN node using PiCAN FD Zero for Raspberry Pi Zero and a Raspberry Pi Zero 2 W

Software Setup

Files for this example are located in examples/can.

examples/can/

├── bridge.go // CAN Bridge Thing

├── node.go // CAN Node Thing

└── cmd

├── can_node

│   └── main.go // CAN Node application

└── can_bridge

└── main.go // CAN Bridge application

Enable CAN Hat and SocketCAN on Can Node

Follow the instructions for your CAN hat to enable the hat. Example setup for the PiCAN FD Zero for Raspberry Pi Zero shown above.

On CAN Node, bring up the can0 interface.

$ sudo /sbin/ip link set can0 up type can bitrate 500000

Building

Two binaries are built: CAN node (~go/bin/can_node) and CAN bridge (~go/bin/can_bridge)


$ cd merle

$ ./build examples/can

Running

CAN Node


$ cd merle

$ ~/go/bin/can_node -h

Usage of /home/merle/go/bin/can_node:

-iface string

CAN interface (default "can0")

-rhost string

Remote host

-ruser string

Remote user (default "merle")


CAN Bridge


$ cd merle

$ ~/go/bin/can_bridge