In short
An ECU on the bench needs the messages it would get in the car, at the right cycle times, with valid counters and CRCs, or it sets timeout DTCs and goes into a degraded mode. Load the DBC, send every cyclic message of the other nodes on schedule, recalculate protection fields per frame, and expose the signal values to your tests.
Best for: Test and integration engineers who need an ECU to run on the bench without the rest of the vehicle
Put an ECU on a bench with just power and a CAN interface, and it usually does not behave as it would in the car. It expects wheel speeds from the ABS module, engine data from the engine ECU and status from the body controller. When they do not arrive, it stores timeout DTCs, switches to substitute values, or refuses to enable functions. A restbus simulation fixes that: it plays every other ECU on the bus, so the ECU under test sees a complete vehicle network.
What the restbus needs from the DBC
| From the DBC | Used for |
|---|---|
Messages and their transmitters (BO_ ... ESP) | Which messages to send: all those not sent by the ECU under test |
GenMsgCycleTime attribute | How often to send each message |
| Signals with factor and offset | Turning test values such as 50 km/h into bytes |
| Counter and CRC signals | Which fields must change on every frame |
GenMsgCycleTime is not part of the core DBC syntax; it is a widely used attribute convention, and cantools reads it into message.cycle_time. Check that your DBC has it for every cyclic message. This guide uses a small example DBC with three simulated nodes and one ECU under test:
BO_ 416 ESP_Speed: 8 ESP 20 ms CRC, counter, VehicleSpeed
BO_ 688 Engine_Data: 8 ECM 20 ms EngineSpeed, CoolantTemp
BO_ 960 Body_Status: 8 BCM 100 ms DoorOpen_FL, OutsideTemp
BO_ 1280 DUT_Status: 8 DUT 50 ms sent by the ECU under test, not simulated
The restbus
One scheduler thread keeps a queue of due times. It sends each message when it is due and plans the next send from the planned time, not from when the send actually happened, so the cycle does not drift. Signal values live in a dictionary that tests can change at any time.
import heapq
import threading
import time
import can
import cantools
class Restbus:
"""Send every cyclic message of the simulated nodes, as the DBC defines them."""
def __init__(self, bus, db, nodes, protect=None):
self.bus, self.protect = bus, protect or {}
self.messages = [m for m in db.messages if m.cycle_time and set(m.senders) & set(nodes)]
self.values = {m.name: {s.name: 0 for s in m.signals} for m in self.messages}
self.counters = {m.name: 0 for m in self.messages}
self.silent = set()
self.lock, self.stop_event = threading.Lock(), threading.Event()
self.thread = threading.Thread(target=self._run, daemon=True)
def set(self, message, **signals):
with self.lock:
self.values[message].update(signals)
def silence(self, node, silent=True):
"""Stop (or restart) all messages of one simulated ECU, e.g. to test timeouts."""
(self.silent.add if silent else self.silent.discard)(node)
def _frame(self, message):
with self.lock:
data = bytearray(message.encode(self.values[message.name], strict=False))
if message.name in self.protect: # counter and CRC, calculated per frame
self.protect[message.name](data, self.counters[message.name])
self.counters[message.name] += 1
return can.Message(arbitration_id=message.frame_id, is_extended_id=message.is_extended_frame, data=data)
def _run(self):
start = time.monotonic()
queue = [(start, i) for i in range(len(self.messages))]
heapq.heapify(queue)
while not self.stop_event.is_set():
due, i = heapq.heappop(queue)
self.stop_event.wait(max(0.0, due - time.monotonic()))
message = self.messages[i]
if not set(message.senders) & self.silent:
self.bus.send(self._frame(message))
heapq.heappush(queue, (due + message.cycle_time / 1000, i)) # no drift: next slot from the plan
def start(self):
self.thread.start()
def stop(self):
self.stop_event.set()
self.thread.join()
Three details make the difference between a restbus that works and one that only looks right:
strict=Falselets cantools encode a message even if a test sets only some of its signals; the rest stay at 0.- Protection fields per frame. A frozen counter or a stale CRC looks like a faulty sender to the ECU under test, and it reacts exactly as it would to a real fault. The
protectcallbacks recalculate them for every frame. silence()stops one simulated ECU. That is how you test what the ECU under test does when a sender disappears, which the fault injection guide takes further.
Running it
The ESP message uses an AUTOSAR E2E Profile 2 style counter and CRC-8H2F, the layout explained in the counters and CRC guide. Its Data ID list is the manufacturer's secret on a real vehicle; recover the per-counter constants from a log as that guide shows, or use the values from the specification you have.
DATA_ID = [0x5D, 0x12, 0xA7, 0x3C, 0xE1, 0x48, 0x9B, 0x06, 0x71, 0xC4, 0x2F, 0x88, 0xD3, 0x6A, 0xB5, 0x19]
def crc8h2f(data):
crc = 0xFF
for byte in data:
crc ^= byte
for _ in range(8):
crc = ((crc << 1) ^ 0x2F) & 0xFF if crc & 0x80 else (crc << 1) & 0xFF
return crc ^ 0xFF
def protect_esp_speed(data, counter):
"""E2E Profile 2 style: counter in the low nibble of byte 1, CRC-8H2F in byte 0."""
data[1] = (data[1] & 0xF0) | (counter & 0x0F)
data[0] = crc8h2f(bytes(data[1:]) + bytes([DATA_ID[counter & 0x0F]]))
db = cantools.database.load_file("restbus.dbc")
with can.Bus(interface="socketcan", channel="vcan0") as bus:
restbus = Restbus(bus, db, nodes=["ESP", "ECM", "BCM"], protect={"ESP_Speed": protect_esp_speed})
restbus.set("ESP_Speed", VehicleSpeed=50.0)
restbus.set("Engine_Data", EngineSpeed=1500, CoolantTemp=90)
restbus.start()
time.sleep(10)
restbus.set("Body_Status", DoorOpen_FL=1) # the test step: open the door
time.sleep(5)
restbus.stop()
Before you have hardware, run it on a virtual SocketCAN interface (sudo ip link add dev vcan0 type vcan && sudo ip link set up vcan0) and watch it with candump -td vcan0, which prints the time between frames.
Checking the result
One test run on python-can's virtual bus, 1.5 seconds long, with the BCM silenced after 1 second:
| ID | Frames | Mean period |
|---|---|---|
0x1A0 ESP_Speed | 77 | 19.87 ms |
0x2B0 Engine_Data | 76 | 20.07 ms |
0x3C0 Body_Status | 11, then silent | 100.47 ms |
Frame 4 of 0x1A0 was cb 03 88 13 00 00 00 00: the counter is 3, the speed 88 13 = 5000 × 0.01 = 50.00 km/h, and the CRC cb matches. DUT_Status was never sent, because the DUT sends it itself.
On a normal operating system, Python's timing has jitter of its own. Measure the periods with candump -td before relying on them, and if the ECU under test monitors timing tightly, use a CAN interface that can send cyclic messages from its own hardware or a real-time system. python-can's bus.send_periodic() hands fixed cyclic frames to the driver, which depending on the interface schedules them in the kernel or in the interface hardware.
Making it part of a test
A restbus becomes useful when tests drive it:
- Start the restbus with realistic default values.
- Wait until the ECU under test has started and its own messages appear.
- Change one input, for example
restbus.set("ESP_Speed", VehicleSpeed=120.0). - Check the ECU's reaction in its own messages, using its signals from the DBC.
- Silence one sender, wait longer than its timeout, and check the reaction and the stored DTC.
The automated signal checks guide shows how to turn the checks in step 4 into pytest tests.
Frequently asked questions
What is restbus simulation?
Simulating all the other ECUs on a network, so the ECU under test receives every message it expects, with the right cycle times, values, counters and checksums, as if it were in the vehicle.
Where do the cycle times come from?
From the DBC, usually the GenMsgCycleTime attribute of each message. It is a common attribute convention rather than part of the core DBC format; cantools exposes it as message.cycle_time.
Why does my ECU set timeout DTCs on the bench even though I send the messages?
Check the cycle time, the counters and the CRCs. A message that arrives too slowly, with a frozen counter or with a wrong CRC is treated like a missing message by many receivers.
Can python-can send cyclic messages without a thread?
Yes. bus.send_periodic(message, period) hands a fixed message to the interface, which sends it on its own. It suits messages whose content does not change on every frame; counters and CRCs need a per-frame update.
References
DBC Utility
One desktop app for DBC files and CAN recordings.
One-time $34 · Windows & Linux · Files stay on your computer