How to verify if the bus is shut down properly. Because the bus.shutdown() method of the python-can library returns None

How to verify if the bus is shut down properly. Because the bus.shutdown() method of the python-can library returns None.

I am using python-can version 4.4.0 on my windows 11 machine.
The latest python-can library contains only three states for a bus those are ACTIVE, PASSIVE and ERROR.
Hence I don’t know if I can use the state of the bus to identify if the bus is shutdown or not.
I verified the state of the bus after it was shut down the state was still ACTIVE as the bus was initialized earlier at the beginning with an active state with the following python statement:

from can import *
bus = interface.Bus(interface='pcan', channel=USBChannel, bitrate=BaudRate, state=bus.BusState.ACTIVE)

I have embedded python code in c code which is making the bus shutdown and I want to ensure that the bus is shutdown then only I can continue with my next code.
Is there a way to ensure that a bus is shutdown or not in python-can library?

Does bus.state is BusState.PASSIVE mean it’s shutdown, or is more required?

https://python-can.readthedocs.io/en/stable/bus.html#can.BusABC.state

In the source code bus.shutdown is simply an idempotent wrapper around stop_all_periodic_tasks, so perhaps despite its name, the state is not changed?

Hello @JamesParrott that’s what I want to know. Because bus.shutdown() method does not return any value which would give me an indication. Earlier there used to be an OFF state in the bus.state now that is also not there hence I am confused what to look for?

Asking the general python community here is probably not going to lead to helpful answers unless a domain expert happens to stumble across this question. Better ask the maintainers of python-can, or maybe over on stackoverflow.

1 Like

I think Bus should be used as a context manager, in a with statement. It will then take care of itself.

@JamesParrott Bus can be used with with statement or without with statement it does not make a difference I have tried both ways.

@JamesParrott secondly the state of the bus remains ACTIVE even after shutting it down it does not change to PASSIVE. I have confirmed that. Because originally it has been set to ACTIVE.

It will make a difference if an Exception is raised (the bus may not then be shutdown).

Call close() on it to ‘shutdown’ the bus.

@JamesParrott I tried using bus.close() I got following output:
AttributeError: ‘PcanBus’ object has no attribute ‘close’

Oh sorry - that convention mustn’t be as common as I thought it was.

The base class’s __exit__ just calls shutdown.

The pcan one has a .status too. I can’t find the error codes PCAN Basic API - python-can 4.4.2 documentation

Try asking on a specialist CANbus forum.

Did you find a solution for this?

There is no general way to check if a Bus object is shut down in python-can, because python-can is a wrapper around several drivers which handle this differently. Some drivers provide no feedback on device state after a shutdown command.

For the specific case of PCAN devices, bus shutdown can be verified with

import can

bus = can.Bus(interface="pcan")
bus.shutdown()
assert bus.status & can.interfaces.pcan.basic.PCAN_ERROR_BUSOFF