Skip to content

Commit db5f1d4

Browse files
Minor refactoring of LSS
Use bytearray instead of list for messages time module not used Fix documentation for some methods Minor unpacking optimizations
1 parent 124feae commit db5f1d4

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

canopen/lss.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import logging
22
import struct
3-
import time
4-
53
try:
64
import queue
75
except ImportError:
@@ -55,10 +53,9 @@ def send_switch_mode_global(self, mode):
5553
:param int mode:
5654
CONFIGURATION_MODE or NORMAL_MODE
5755
"""
58-
# LSS messages are always a full 8 bytes long.
56+
# LSS messages are always a full 8 bytes long.
5957
# Unused bytes are reserved and should be initialized with 0.
60-
61-
message = [0]*8
58+
message = bytearray(8)
6259

6360
if self._mode_state != mode:
6461
message[0] = SWITCH_MODE_GLOBAL
@@ -68,19 +65,18 @@ def send_switch_mode_global(self, mode):
6865

6966
def __send_inquire_node_id(self):
7067
"""
71-
:return: int current node id
68+
:return: Current node id
69+
:rtype: int
7270
"""
73-
message = [0]*8
71+
message = bytearray(8)
7472
message[0] = INQUIRE_NODE_ID
7573
current_node_id, nothing = self.__send_command(message)
7674

7775
return current_node_id
7876

7977
def __send_configure(self, key, value1=0, value2=0):
80-
"""Send a message to set a key with values
81-
:return: int error_code, int error_extension
82-
"""
83-
message = [0]*8
78+
"""Send a message to set a key with values"""
79+
message = bytearray(8)
8480
message[0] = key
8581
message[1] = value1
8682
message[2] = value2
@@ -130,13 +126,13 @@ def store_configuration(self):
130126
def __send_command(self, message):
131127
"""Send a LSS operation code to the network
132128
133-
:param byte list message:
129+
:param bytearray message:
134130
LSS request message.
135131
"""
136132

137133
retries_left = self.MAX_RETRIES
138134

139-
message_str = "".join(["{:02x} ".format(x) for x in message])
135+
message_str = " ".join(["{:02x}".format(x) for x in message])
140136
logger.info(
141137
"Sending LSS message %s", message_str)
142138

@@ -165,17 +161,15 @@ def __send_command(self, message):
165161
raise LssError("No LSS response received")
166162
if retries_left < self.MAX_RETRIES:
167163
logger.warning("There were some issues while communicating with the node")
168-
res_command, = struct.unpack("B", response[0:1])
164+
res_command, message1, message2 = struct.unpack_from("BBB", response)
169165
if res_command != message[0]:
170-
raise LssError(abort_code)
171-
else:
172-
self._mode_state = self.CONFIGURATION_MODE
173-
message1, = struct.unpack("B", response[1:2])
174-
message2, = struct.unpack("B", response[2:3])
175-
return message1, message2
166+
raise LssError("Unexpected response (%d)" % res_command)
167+
self._mode_state = self.CONFIGURATION_MODE
168+
return message1, message2
176169

177170
def on_message_received(self, can_id, data, timestamp):
178171
self.responses.put(bytes(data))
179172

173+
180174
class LssError(Exception):
181175
"""Some LSS operation failed."""

0 commit comments

Comments
 (0)