Network and nodes¶
The canopen.Network
represents a collection of nodes connected to the
same CAN bus. This handles the sending and receiving of messages and dispatches
messages to the nodes it knows about.
Each node is represented using the canopen.RemoteNode
or
canopen.LocalNode
class. It is usually associated with an
object dictionary and each service has its own attribute owned by this node.
Examples¶
Create one network per CAN bus:
import canopen
network = canopen.Network()
By default this library uses python-can for the actual communication. See its documentation for specifics on how to configure your specific interface.
Call the connect()
method to start the communication, optionally providing
arguments passed to a the can.BusABC
constructor:
network.connect(channel='can0', bustype='socketcan')
# network.connect(bustype='kvaser', channel=0, bitrate=250000)
# network.connect(bustype='pcan', channel='PCAN_USBBUS1', bitrate=250000)
# network.connect(bustype='ixxat', channel=0, bitrate=250000)
# network.connect(bustype='nican', channel='CAN0', bitrate=250000)
Add nodes to the network using the add_node()
method:
node = network.add_node(6, '/path/to/object_dictionary.eds')
local_node = canopen.LocalNode(1, '/path/to/master_dictionary.eds')
network.add_node(local_node)
Nodes can also be accessed using the Network
object as a Python dictionary:
for node_id in network:
print(network[node_id])
To automatically detect which nodes are present on the network, there is the
scanner
attribute available for this purpose:
# This will attempt to read an SDO from nodes 1 - 127
network.scanner.search()
# We may need to wait a short while here to allow all nodes to respond
time.sleep(0.05)
for node_id in network.scanner.nodes:
print(f"Found node {node_id}!")
Finally, make sure to disconnect after you are done:
network.disconnect()