Canbus_query.py after Klipper start (OR detecting UUID's another way?)

I’ll start with what I’m trying to do in case I’m barking up a completely wrong tree… I’d like to be able to detect what UUID’s are attached to a CAN bus serviced by Klipper’s USB-CAN passthrough function. I need to be able to do so both before & after Klipper has started (and indeed when Klipper might not be able to start). My observation is that once Klipper starts, any CAN-connected MCU’s are claimed by Klipper and no longer respond to the canbus_query.py script. I get “Total 0 uuids found” even if Klipper is shutdown again.

The reason I want this is because I’d like to be able to detect from the OS whether a specific MCU is connected by searching for its CAN UUID. Depending on whether it’s present or absent, I’d like to adjust a symlink to a config file that’s included by printer.cfg to be either to a config that includes functionality for that MCU or an empty dummy config that doesn’t reference it.

All of this is because I have an ERCF setup I’d like to be able to run the printer without sometimes. The ERCF MCU is attached via CAN with an easy four-pin connector and held onto the top of the printer with clips. I can unplug the connector, unclip, and easily remove the ERCF. Obviously if I do that while Klipper is running, it’ll fault out when the MCU goes missing. What I’d like is to be able to run a script in the systemd ExecStartPre phase that can check for the ERCF’s UUID, update the symlink to the real or dummy config file, then allow Klipper to start. That way, each time I re-start Klipper, the ERCF can be auto-detected and the macros, etc. for it can be loaded or not accordingly.

Is there a better way to check if an MCU is attached to the passthrough CAN bus? Is there some other way to make Klipper conditionally start and in-/exclude config files if it can’t find an MCU?

Any direction appreciated!

For what it is worth, I don’t know of anyway to automate that from the command line.

If you wanted to manually check if the mcu is present, you could run console.py -c can0 <uuid> and see if it connects. (See Debugging - Klipper documentation )

-Kevin

1 Like

Thank you! That got me close enough. Not the most elegant solution perhaps, but it works…

~/try-ercf-mcu.sh:

#!/bin/bash
UUID=YOUR_UUID_HERE
KH=/home/ubuntu
CFG=$KH/klipper_config
OUT=/tmp/klipper-attempt

rm -f $OUT
timeout 5s $KH/klippy-env/bin/python $KH/klipper/klippy/console.py -c can0 $UUID > $OUT 2>&1

if grep 'MCU config' $OUT ; then
	echo "Found ERCF MCU.  Enabling" | logger
	ln -sf $CFG/ercf_enabled.cfg $CFG/ercf.cfg
else
	echo "ERCF MCU not found.  Disabling" | logger
	ln -sf $CFG/ercf_disabled.cfg $CFG/ercf.cfg
fi

rm -rf $OUT

And /etc/systemd/system/klipper.service:

[Service]
...
ExecStartPre=/home/ubuntu/try-ercf-mcu.sh
ExecStart=/home/ubuntu/klippy-env/bin/python [....]
1 Like