Skip to content
If this is your first visit, please click here to learn about shipping and delivery.
If this is your first visit, please touch here to learn about shipping and delivery.
Compact Raspberry Pi RS485 and CAN expansion board

Build a Raspberry Pi RS485-to-CAN Gateway: Message Mapping, Timing and Fault Handling

A board with RS485 and CAN connections can provide the hardware for a small gateway, but it does not automatically translate between the two networks. The software must understand what each message means, decide when to transmit it and report what happens when one side stops responding.

A useful first project is a read-only bridge: poll a bench sensor over RS485 and publish its measurement onto a separate CAN test network. Start with an explicit data contract and controlled test devices. Adding control commands later requires additional decisions about permissions, acknowledgements and safe failure behaviour.

Understand the two paths through the HAT

The RS485 CAN-BUS pHAT for Raspberry Pi in PMD Way's RS232 and RS485 HAT collection provides a UART-based RS485 interface and an SPI-connected MCP2515 CAN controller. The collection name does not mean this particular board supplies RS232.

Its documentation describes automatic RS485 transmit/receive direction control. That feature manages the electrical driver direction; it does not provide a sensor protocol, decide which node may transmit or translate data into CAN frames. The board also does not become an isolated interface merely because it has transient suppressors.

Check the exact board revision against the manufacturer's RS485 CAN HAT documentation. Current and older descriptions identify different CAN transceiver markings, so use the fitted part and matching schematic rather than assuming all revisions are identical. Do not apply the external-supply instructions for a larger isolated HAT to this compact model.

Write the application contract before coding

RS485 defines electrical signalling rather than a complete message protocol. The Texas Instruments RS485 design guide explains this distinction and the need to control access on a half-duplex bus. Obtain the sensor's actual protocol: address, command format, baud rate, parity, response length, checksum and timing.

On the CAN side, specify the identifier format, payload layout and meaning. The MCP2515 data sheet describes a Classical CAN controller, not a CAN FD controller. Do not choose a payload or traffic format that depends on CAN FD support.

Example fields for a gateway design record
Item Decision to record
Source measurement Sensor address, command and register or field definition
Conversion Signedness, byte order, scale, units and valid range
CAN message Agreed identifier, payload length and field positions
Freshness Maximum acceptable age and how stale data is represented
Failure Timeout, invalid-response and bus-fault behaviour

For a private bench example, a sensor value of 237 might represent 23.7 °C. The receiving CAN application must know that scale. Document whether negative values use a signed integer and which byte arrives first. Choose identifiers only within a network design you control; an arbitrary example identifier is not automatically free on an existing installation.

Commission each Linux interface independently

Prove the RS485 connection with one known device before enabling forwarding. Use the Pi model's documented UART mapping, remove any competing serial-console ownership and confirm the serial settings. With automatic direction control, do not add manual direction toggling unless the board configuration specifically requires it.

Linux provides RS485 configuration support only where the UART driver and hardware implement it. The kernel RS485 interface documentation describes supported flags and the configuration calls. Setting an option in a Python library cannot create missing hardware support or override a board's separate automatic-direction circuit.

Next commission CAN with a known working test node. Verify the controller oscillator, chip select, interrupt connection and bit rate against the actual board and driver configuration. Establish successful reception independently before introducing the gateway's conversion logic.

Under SocketCAN, CAN appears as a network interface rather than a UART device. The Linux SocketCAN documentation describes its interface and diagnostics. For an interface identified as can0, inspect its state and counters with:

ip -details -statistics link show can0

Save the result before and after a test. Controller errors, receive drops and a stopped interface need different responses from a valid frame containing the wrong application value.

Keep measurement acquisition separate from forwarding

Use separate logical stages: acquire a response, validate it, decode a measurement and publish an agreed representation. Attach a monotonic acquisition time and quality state when the response is accepted. A malformed response should never refresh the age of the last good sample.

For example, poll once per second and mark a measurement stale after five seconds without a valid reply, if those values suit the application. These are design choices, not protocol defaults. The CAN receiver should be able to distinguish a fresh 23.7 °C reading from the same value retained during a disconnected sensor.

Do not hide repeated failures by sending the last value forever without an age or validity indication. Conversely, do not substitute zero unless the agreed payload format explicitly defines zero as invalid; it may be a legitimate measurement.

Bound the work when one side slows down

Decide whether the application needs every sample or only the newest state. A display may prefer one replaceable pending measurement. An experiment collecting every observation may need a bounded queue and a clearly recorded overflow policy.

Suppose polling produces ten records per second but the downstream system accepts only two. The queue grows by eight records per second. A buffer of eighty records then fills in about ten seconds from empty. Increasing memory delays the problem; it does not resolve the rate mismatch.

Keep network waits out of the acquisition path where practical. Track queue depth, accepted responses, rejected responses and transmitted records separately. Rate-limit diagnostic printing so that a fault does not consume the resources needed to recover from it.

Test faults as part of normal commissioning

  1. Confirm a known source value appears with the correct units and byte order.
  2. Disconnect the sensor and verify the stale indication appears after the specified interval.
  3. Reconnect it and confirm that fresh data clears the condition without restarting the Pi.
  4. Interrupt the CAN test connection and observe queue limits and error reporting.
  5. Restart the gateway and check that it does not announce an old cached value as newly measured.

Document termination, reference-ground and isolation arrangements for both networks separately. A CAN connector and an RS485 terminal block must not be connected together as if they were interchangeable differential buses.

Finish with a compact commissioning record: hardware revision, Linux configuration, source protocol, payload definition and fault-test results. That record makes the gateway maintainable when a sensor, board or software version changes.

Previous article Battery Power for Arduino Projects: Voltage Range, Runtime and Regulator Heat