Skip to content

Commit ea0efa4

Browse files
authored
nmt: Fix NmtSlave on a node without a heartbeat producer time. (#672)
The NMT state transition from INITIALISING to PRE-OPERATIONAL causes the heartbeat to start automatically if the parameter is non-zero in object 0x1017:00. This check however raises an exception if the object does not even exist. Cover this state in tests and explicitly ignore a KeyError when asking for the object.
1 parent 9aab2d4 commit ea0efa4

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

canopen/nmt.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ def send_command(self, code: int) -> None:
230230
# The heartbeat service should start on the transition
231231
# between INITIALIZING and PRE-OPERATIONAL state
232232
if old_state == 0 and self._state == 127:
233-
heartbeat_time_ms = self._local_node.sdo[0x1017].raw
234-
self.start_heartbeat(heartbeat_time_ms)
233+
try:
234+
heartbeat_time_ms = self._local_node.sdo[0x1017].raw
235+
self.start_heartbeat(heartbeat_time_ms)
236+
except KeyError:
237+
pass
235238
else:
236239
self.update_heartbeat()
237240

test/test_nmt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,14 @@ def test_heartbeat(self):
193193

194194
self.local_node.nmt.stop_heartbeat()
195195

196+
def test_heartbeat_no_producer_time(self):
197+
# Create a node without the producer heartbeat time parameter
198+
node = canopen.LocalNode(1, canopen.ObjectDictionary())
199+
with self.assertRaises(KeyError):
200+
node.sdo[0x1017].raw = 100
201+
# Should not fail because of missing 0x1017 object entry
202+
node.nmt.state = "PRE-OPERATIONAL"
203+
196204

197205
if __name__ == "__main__":
198206
unittest.main()

0 commit comments

Comments
 (0)