Skip to content
Advertisement

Close bus in python-can

I am using python-can to send CAN messages like this:

JavaScript

The script works fine, but when I run it for a 2nd time, it results in an error, because the bus is still open from the previous time. I think I need something like this at the end of my script:

JavaScript

However, this does not exist and I can’t seem to find the proper way to do it in the python-can documentation. How can I properly close the bus in order to be able to use it again the next time?

Advertisement

Answer

The Bus object should be used inside a with statement to be closed properly.

JavaScript

You should be able to send more messages inside this open block, but after this block the bus will be closed properly

See the docs

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement