Hello all,
I am not new to TD, but I am new to extensions. I thought it would be a great learning project to create my own component that manages the hardware connection of the Canable CAN interface and uses the Python-CAN library.
So far I have:
- Installed Python 3.7.7 (python-3.7.7-amd64-webinstall.exe)
- Installed Python-CAN (pip install python-can)
- Created my own custom component & Made my own extension for that component.
I am now trying to create one PulseButton that initiates the connection, then different functions for checking connection status and sending and receiving.
Short Version of the problem:
When I successfully create the connection, there is a Python object returned that represents the connection (with methods that I can call on that connection like status). But when I then create a different function to check the status, it doesnt know anything about the connection⌠âUnboundLocalError: local variable âbusâ referenced before assignmentâ
How do I maintain this Python Object âgloballyâ to reference in different functions?
Supporting Info:
This successfully connects and returns a python object representing the connection:
bus = can.interface.Bus(bustype=âslcanâ, channel=âCOM5â, bitrate=250000)
I can then immediately do this:
print(bus.state)
Which prints this:
BusState.ACTIVE
A few things I have tried based on forum research and previous suggestions:
I tried the suggestion of some of the developers from old forum post threads that suggested storage:
vcan.store(âbusobjectâ, bus)
bus=vcan.fetch(âbusobjectâ)
âŚbut the problem here is that when I run bus.state it shows the status at the time of the store (so if I unplug the USB port, it still shows Active).
I also tried modifying âslcan.pyâ to use
class slcanBus(ThreadSafeBus):
âŚinstead of the usualâŚ
class slcanBus(BusABC):
âŚbut that didnt help either.
https://python-can.readthedocs.io/en/master/bus.html#thread-safe-bus
Any suggestions would be appreciatedâŚI am starting to wonder if I should try to use the serial CHOP somehowâŚpresumably because it would maintain the connection state for meâŚnot sure if that is the right way to go.
My current TOE file is attachedâŚI have tried lots of different things.
vCan.6.toe (6.1 KB)
Thx!
Steve French (aka frenchy) (aka VoltVisionFrenchy)