Skip to content

Use canopen with externally provided bus and notifier #556

@sveinse

Description

@sveinse

I have a use-case where canopen have to live next to another protocol (using only extendedids). The (physical) can bus interface cannot have multiple instances and the same can.Bus() interface. It and and the can.Notifier() instance must be shared with both protocols. Network.connect() makes it a bit cumbersome to define bus and notifier from the outside.

https://github.com/christiansandberg/canopen/blob/ae71853be7fb42870bb5763066b0c3c50d015669/canopen/network.py#L83-L112

There are several things that can be done here to make it more consistent. I'm interested in hearing what opinions there might be for how to improve this:

Option 1

Be able to support a provided notifier, similar to what's done with bus:

# L111 in network.py
if self.notifier is None:
    self.notifier = can.Notifier(self.bus, [], self.NOTIFIER_CYCLE, **kwargs_notifier)
for listener in self.listeners:
    self.notifier.add_listener(listener)

To use your own:

network = canopen.Network()
network.bus = mybus
network.notifier = mynotifier
network.connect()

Option2

Extend arguments to connect():

bus = kwargs.pop("bus", None)
notifier = kwargs.pop("notifier", None)
if self.bus is None:
    self.bus = bus or can.Bus(*args, *kwargs)
logger.info("Connected to '%s'", self.bus.channel_info)
if self.notifier is None:
    self.notifier = notifier or can.Notifier(self.bus, [], self.NOTIFIER_CYCLE)
for listener in self.listeners:
    self.notifier.add_listener(listener)

This allows the usage:

network = canopen.Network()
network.connect(bus=mybus, notifier=mynotifier)

PS! Network.disconnect() doesn't really play nice with externally provided bus and notifier either and should be looked at.

* EDIT *

I think also the init of Network should be extended:

def __init__(self, bus: Optional[can.BusABC] = None, notifier: Optional[can.Notifier] = None):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions