Tutorial: Industrial Fieldbuses — hal.can (TWAI/CAN FD), hal.rs485 & DMX512

PylotOS provides deterministic C11 fieldbus drivers for hal.can (native ESP-IDF TWAI + external SPI MCP2515/MCP2518FD) and hal.rs485 (hardware DE/RE UART_MODE_RS485_HALF_DUPLEX + microsecond-accurate DMX512 breaks), paired with zero-copy asyncio helpers in pylotos.wired.


1. Unified hal.can Submodule (HAL_OWNER_APP)

Unlike raw GPIO polling, hal.can runs with hardware acceptance filtering, a static C RX ringbuffer (up to 64-byte CAN FD frames), pin-guard collision checks, bus-off recovery (recover()), and automatic teardown when your app stops.

import asyncio
import hal
import pylotos.wired as wired

# Native TWAI controller on Grove B (500 kbps OBD-II)
hal.can.open("twai", tx=26, rx=36, bitrate=500000, port="grove_b")

# Hardware/C-ring acceptance filter: accept only ECU responses 0x7E8..0x7EF
hal.can.set_filter(0x7E8, 0x7F8)

# Request Engine RPM (SAE J1979 Mode 0x01 PID 0x0C)
hal.can.send(0x7DF, bytes((2, 0x01, 0x0C, 0, 0, 0, 0, 0)))

async def read_obd():
    rx = bytearray(64)  # pre-allocated zero-copy buffer
    res = await wired.recv_can_into(rx, timeout_ms=250)
    if res is not None:
        arb_id, n, ext = res
        print(wired.format_can_frame(arb_id, rx[:n], ext))
        print(wired.decode_obd2_pid(rx[:n]))

Bus-Off Recovery & External SPI CAN FD

# Check drop/error counters and recover from electrical bus-off
st = hal.can.stats()
if st["bus_off"] > 0:
    hal.can.recover()

# External SPI MCP2518FD (CAN FD up to 64 B payloads)
hal.can.attach("mcp2518fd", spi_host=1, cs=10, irq=11, bitrate=500000)

2. Hardware DE/RE hal.rs485 (Modbus RTU & DMX512)

Software toggling of RS-485 DE/RE pins in Python causes microsecond collisions on multi-drop industrial buses. PylotOS configures the ESP32 UART hardware state machine in UART_MODE_RS485_HALF_DUPLEX so the silicon drives DE/RE automatically on the exact stop-bit boundary.

Modbus RTU Half-Duplex Transaction

import hal
import pylotos.modbus as modbus
import pylotos.wired as wired

hal.rs485.open(uart=1, tx=17, rx=16, de=21, baudrate=9600, parity=0, stop_bits=1)

req = modbus.build_rtu_frame(1, 0x03, modbus.build_read_holding_pdu(0x0010, 4))
hal.rs485.write(req, 50)

rx = bytearray(64)
n = hal.rs485.read_into(rx, 40)

ANSI E1.11 DMX512 Stage Lighting

import hal
import pylotos.wired as wired

# DMX512 standard: 250000 baud, 8N2
hal.rs485.open(uart=1, tx=17, rx=16, de=21, baudrate=250000, stop_bits=2)

# Build 0x00 Start Code + channel slots
universe = wired.pack_dmx512_frame([255, 128, 0, 64])

# Hardware-timed 92 us LOW break + 12 us Mark-After-Break (MAB)
hal.rs485.tx_break(92, 12)
hal.rs485.write(universe, 50)

3. Clean App Teardown (HAL_OWNER_APP)

Both hal.can and hal.rs485 are HAL_OWNER_APP. When your app exits or soft-resets, PylotOS automatically runs hal.wired_reset_app_handles(), uninstalling TWAI/UART drivers and releasing pin-guard slots so the next app starts with a clean bus.


4. Run the Interactive Simulator Example

pylotos-sim examples/rs485_modbus_and_dmx512.py --board m5stack_core2

See the full runnable source in examples/rs485_modbus_and_dmx512.py.