Skip to content

Commit 6aac78c

Browse files
committed
Added loop to connect()
1 parent 09fe0a3 commit 6aac78c

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

README.rst

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ The :code:`n` is the PDO index (normally 1 to 4). The second form of access is f
113113
# network.connect(bustype='nican', channel='CAN0', bitrate=250000)
114114
115115
# Read a variable using SDO
116-
device_name = node.sdo['Manufacturer device name'].raw
117-
vendor_id = node.sdo[0x1018][1].raw
116+
device_name = node.sdo['Manufacturer device name'].get_raw()
117+
vendor_id = node.sdo[0x1018][1].get_raw()
118118
119119
# Write a variable using SDO
120-
node.sdo['Producer heartbeat time'].raw = 1000
120+
node.sdo['Producer heartbeat time'].set_raw(1000)
121121
122122
# Read PDO configuration from node
123123
node.tpdo.read()
@@ -141,8 +141,8 @@ The :code:`n` is the PDO index (normally 1 to 4). The second form of access is f
141141
142142
# Read a value from TPDO[1]
143143
node.tpdo[1].wait_for_reception()
144-
speed = node.tpdo[1]['Velocity actual value'].phys
145-
val = node.tpdo['Some group.Some subindex'].raw
144+
speed = node.tpdo[1]['Velocity actual value'].get_phys()
145+
val = node.tpdo['Some group.Some subindex'].get_raw()
146146
147147
# Disconnect from CAN bus
148148
network.sync.stop()
@@ -195,17 +195,15 @@ This library can be used with asyncio.
195195
196196
async def main():
197197
198-
# Open CAN bus
198+
# Start with creating a network representing one CAN bus
199+
network = canopen.Network()
200+
201+
# Connect to the CAN bus
199202
# Arguments are passed to python-can's can.Bus() constructor
200203
# (see https://python-can.readthedocs.io/en/latest/bus.html).
201-
bus = can.BUS(interface='pcan', bitrate=1000000)
202-
203-
# Create a network representing one CAN bus
204-
network = canopen.Network(bus)
205-
206-
# Start the notifier to enable canopen to respond to incoming CAN message
204+
# Note the loop parameter to enable asyncio operation
207205
loop = asyncio.get_event_loop()
208-
network.notifier = can.Notifier(bus, network.listeners, 1, loop=loop)
206+
network.connect(interface='pcan', bitrate=1000000, loop=loop)
209207
210208
# Create two independent tasks for two nodes 51 and 52 which will run concurrently
211209
task1 = asyncio.create_task(my_node(network, 51, '/path/to/object_dictionary.eds'))

canopen/network.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def connect(self, *args, **kwargs) -> "Network":
102102
for full list of supported interfaces.
103103
:param int bitrate:
104104
Bitrate in bit/s.
105+
:param loop:
106+
Optional, pass the loop parameter if running under asyncio
105107
106108
:raises can.CanError:
107109
When connection fails.
@@ -113,9 +115,14 @@ def connect(self, *args, **kwargs) -> "Network":
113115
if node.object_dictionary.bitrate:
114116
kwargs["bitrate"] = node.object_dictionary.bitrate
115117
break
118+
# The optional loop parameter goes to can.Notifier()
119+
kwargs_notifier = {}
120+
if "loop" in kwargs:
121+
kwargs_notifier["loop"] = kwargs["loop"]
122+
del kwargs["loop"]
116123
self.bus = can.interface.Bus(*args, **kwargs)
117124
logger.info("Connected to '%s'", self.bus.channel_info)
118-
self.notifier = can.Notifier(self.bus, self.listeners, 1)
125+
self.notifier = can.Notifier(self.bus, self.listeners, 1, **kwargs_notifier)
119126
return self
120127

121128
def disconnect(self) -> None:

0 commit comments

Comments
 (0)