Listening to Multiple Busses

102 views
Skip to first unread message

Brent Gardner

unread,
Sep 20, 2018, 10:10:38 PM9/20/18
to python-can
Before I was aware of python-can, I wrote my own simple python module (https://github.com/bggardner/canopen-rpi/blob/master/CAN.py) where my Bus class simply extended socket.socket (since I only cared about the socketcan bustype).  This allows me to use the following code to listen to multiple busses.  While I haven't dug into python-can very much, is there a way to accomplish this, as I can't put the python-can "bus" through a select, as it does not have a fileno() method.  I would like to stop maintaining my module and just use python-can.  Here's what it looks like with my module:

```
import CAN
import select

bus0 = CAN.Bus("vcan0")
bus1 = CAN.Bus("vcan1")
rlist, _, _ = select.select([bus0, bus1], [], [])
for bus in rlist:
    msg = bus.recv()  # Returns CAN.Message
```

My feeble attempt to do this with python-can is below:

```
import can
import select

bus0 = can.interface.Bus(channel="vcan0", bustype="socketcan")
bus1 = can.interface.Bus(channel="vcan1", bustype="socketcan")
rlist, _, _ = select.select([bus0.socket, bus1,socket], [], [])
for s in rlist:
    if s == bus0.socket:
        msg = bus0.recv() # Returns can.Message
    elif s == bus1.socket:
        msg = bus1.recv() # Returns can.Message

```
As you can see, this is quite messy, especially if more busses (or regular sockets) were involved.  This is important, as I make a lot of protocol adapters to and from CAN, and need to monitor multiple busses/sockets "simultaneously".  Suggestions?

Thanks!

Christian Sandberg

unread,
Sep 21, 2018, 2:34:59 AM9/21/18
to python-can
In the develop branch you can use the bus instances directly in the select() function instead of using the socket attribute.

Alternatively you can use can.Notifier class which supports multiple buses as well.

Bill Hass

unread,
Apr 11, 2019, 4:17:19 PM4/11/19
to python-can
This issue came up in Caring Caribou here: https://github.com/CaringCaribou/caringcaribou/issues/52 

It is nice to use the config file for a single interface. This allows a tool such as CaringCaribou to simply call `can.Bus()` and not worry about interface configuration; the user can figure it out.

For multiple interfaces, what is the best way for a tool that utilizes python-can to handle multiple interfaces (e.g. socketcan and a non-socketcan interface)? Is there a can.config similar way of handling this? An API I am not seeing similar to `can.Bus()`?

Thank you!
Reply all
Reply to author
Forward
0 new messages