Skip to content

Commit eaf3313

Browse files
authored
Merge pull request #1835 from mikefsq/master
worker_thread.start() inside _open_port(), and crc cleanup
2 parents 1c8b2c6 + 2ce3b14 commit eaf3313

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/artisanlib/aillio/aillio_r2.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ def __init__(self, debug:bool = False) -> None:
249249

250250
# Initialize USB connection and start worker thread
251251
self._open_port()
252-
self.worker_thread = threading.Thread(target=self.__updatestate,
253-
args=(self.child_pipe,))
254-
self.worker_thread.daemon = True
255-
self.worker_thread.start()
256252

257253
def __del__(self) -> None:
258254
if not self.simulated:
@@ -420,12 +416,7 @@ def __sendcmd(self, cmd:Union[List[int],bytes]) -> None:
420416
if isinstance(cmd, list):
421417
cmd = bytes(cmd)
422418

423-
# For R2, ensure command is properly formatted with CRC
424-
if len(cmd) == 4: # Basic command without CRC
425-
cmd_with_crc = self.prepare_command(cmd)
426-
self.ep_out.write(cmd_with_crc, timeout=self.TIMEOUT)
427-
else: # Command already has CRC
428-
self.ep_out.write(cmd, timeout=self.TIMEOUT)
419+
self.ep_out.write(cmd, timeout=self.TIMEOUT)
429420

430421
except Exception as e: # pylint: disable=broad-except
431422
raise OSError(f'Failed to send command: {str(e)}') from e
@@ -606,6 +597,8 @@ def __updatestate(self, p:'Connection') -> None:
606597
f"active_power: {self.active_power:.1f}, reactive_power: {self.reactive_power:.1f}, apparent_power: {self.apparent_power:.1f}, "
607598
f"voltage_rms: {self.voltage_rms:.1f}V, current_rms: {self.current_rms:.1f}A ")
608599

600+
else:
601+
pass #unparsed
609602

610603
# Check for and handle commands from the pipe
611604
try:
@@ -674,8 +667,6 @@ def prepare_command(self, cmd:Union[List[int],bytes]) -> bytes:
674667
if isinstance(cmd, list):
675668
cmd = bytes(cmd)
676669
cmd_with_crc = bytearray(cmd)
677-
while len(cmd_with_crc) < 60:
678-
cmd_with_crc.extend([0])
679670
cmd_with_crc.extend([0, 0, 0, 0])
680671
crc = self.calculate_crc32(bytes(cmd_with_crc))
681672
cmd_with_crc[-4:] = crc.to_bytes(4, 'little')

0 commit comments

Comments
 (0)