Skip to contentSkip to conversation

Several sockets on one interface in Python

DISCUSSION Joachim Jaus 3 participants 3 messages May 31, 2013 – Jun 3, 2013 CAN network drivers

lore
Share & tools
lore
#0 lore raw

Hi all,
this is my first email to this list, therefore I want start with a big "thank you" to all the people involved in the Linux CAN socket development, which has opened up a lot of interesting possibilities for sensor and machine control for me and my colleagues.

We would like to bind several sockets with different filter settings to one single CAN interface within the same application. Is that generally a safe thing to do (both in C and in Python)? Is there a way to set filters on a CAN socket in Python similar to the implementation in C?

Background:
We are implementing a machine control system with several sensors and actuators communicating over the same CAN bus using the CAN-open protocol.
There are several control loops running simultaneously, each controlled by a python sub-process.
It would be convenient if each subprocess had several sockets, with each socket receiving only messages sent by a certain CAN-open node or a group of CAN-open nodes. That way, we could most easily receive all relevant messages (SDOs, PDOs, ...) from a specific node or group of nodes.

Best regards,
Joachim

#1 lore raw

Hi Joachim,

Quoted 5 lines · Joachim Jaus

Hi all,
this is my first email to this list, therefore I want start with a big "thank you" to all the people involved in the Linux CAN socket development, which has opened up a lot of interesting possibilities for sensor and machine control for me and my colleagues.

Great! Mind-expansion without drugs but only through SocketCAN :-)

Quoted 4 lines

We would like to bind several sockets with different filter settings to one single CAN interface within the same application. Is that generally a safe thing to do (both in C and in Python)?

Yes, it's a safe thing to do.

E.g. the candump programm does it in this way.

You may create multiple sockets having each multiple filters.

The only potential drawback that you need to put a select() statement around it, to manage to read from all created sockets/filedescriptors.

Quoted 2 lines

Is there a way to set filters on a CAN socket in Python similar to the implementation in C?

I don't know exactly.
But "CAN_RAW_FILTER" is defined in the Python patch:

http://bugs.python.org/file23234/socketcan_v5.patch

http://libbits.wordpress.com/2012/05/22/socketcan-support-in-python/

so you can hope that the filters are configurable in some way.

Perhaps Yegor knows more about this.

Best ragards,
Oliver

#2 lore raw
Quoted 31 lines · Oliver Hartkopp

Hi Joachim,

Quoted 5 lines · Joachim Jaus

Hi all,
this is my first email to this list, therefore I want start with a big "thank you" to all the people involved in the Linux CAN socket development, which has opened up a lot of interesting possibilities for sensor and machine control for me and my colleagues.

Great! Mind-expansion without drugs but only through SocketCAN :-)

Quoted 4 lines

We would like to bind several sockets with different filter settings to one single CAN interface within the same application. Is that generally a safe thing to do (both in C and in Python)?

Yes, it's a safe thing to do.

E.g. the candump programm does it in this way.

You may create multiple sockets having each multiple filters.

The only potential drawback that you need to put a select() statement around it, to manage to read from all created sockets/filedescriptors.

Quoted 2 lines

Is there a way to set filters on a CAN socket in Python similar to the implementation in C?

I don't know exactly.
But "CAN_RAW_FILTER" is defined in the Python patch:

Quoted 7 lines

http://bugs.python.org/file23234/socketcan_v5.patch

http://libbits.wordpress.com/2012/05/22/socketcan-support-in-python/

so you can hope that the filters are configurable in some way.

Perhaps Yegor knows more about this.

No. Never tried filtering. Was just curious, how Python support feels like, tried the example and wrote the blog entry. Joachim, please try and share your experience. I'll then include this into our wiki:
http://elinux.org/Python_Can

You can also try to ask devs involved in this patch creation/reviewing: http://bugs.python.org/issue10141

Yegor