From 71a3985cae1cf9834b442e552db7efd52d07de91 Mon Sep 17 00:00:00 2001 From: drunsinn Date: Wed, 13 Jan 2021 17:25:42 +0100 Subject: [PATCH 1/4] add better error messages and support for pgm stack --- pyLSV2/client.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pyLSV2/client.py b/pyLSV2/client.py index 333db30..14e28b5 100644 --- a/pyLSV2/client.py +++ b/pyLSV2/client.py @@ -327,40 +327,40 @@ def _configure_connection(self): logging.info('setting connection settings for %s and block length %s', control_type, max_block_length) - self.set_system_command(LSV2.SYSCMD_SET_BUF512) - if max_block_length >= 4096: if self.set_system_command(LSV2.SYSCMD_SET_BUF4096): self._buffer_size = 4096 else: - raise Exception('error in communication') + raise Exception('error in communication while setting buffer size to 4096') elif 3072 <= max_block_length < 4096: if self.set_system_command(LSV2.SYSCMD_SET_BUF3072): self._buffer_size = 3072 else: - raise Exception('error in communication') + raise Exception('error in communication while setting buffer size to 3072') elif 2048 <= max_block_length < 3072: if self.set_system_command(LSV2.SYSCMD_SET_BUF2048): self._buffer_size = 2048 else: - raise Exception('error in communication') + raise Exception('error in communication while setting buffer size to 2048') elif 1024 <= max_block_length < 2048: if self.set_system_command(LSV2.SYSCMD_SET_BUF1024): self._buffer_size = 1024 else: - raise Exception('error in communication') + raise Exception('error in communication while setting buffer size to 1024') elif 512 <= max_block_length < 1024: if self.set_system_command(LSV2.SYSCMD_SET_BUF512): self._buffer_size = 512 else: - raise Exception('error in communication') + raise Exception('error in communication while setting buffer size to 512') elif 256 <= max_block_length < 512: self._buffer_size = 256 else: + logging.error('could not decide on a buffer size for maximum message length of %d', max_block_length) raise Exception('unknown buffer size') if not self.set_system_command(LSV2.SYSCMD_SECURE_FILE_SEND): - raise Exception('error in communication') + raise Exception('error in communication while enabling secure file send') + self.login(login=LSV2.LOGIN_FILETRANSFER) logging.info( 'successfully configured connection parameters and basic logins. selected buffer size is %d', self._buffer_size) @@ -437,9 +437,9 @@ def get_system_parameter(self, force=False): message_length = len(result) info_list = list() # as per comment in eclipse plugin, there might be a difference between a programming station and a real machine - if message_length == 120: # programming station? + if message_length == 120: info_list = struct.unpack('!14L8B8L2BH4B2L2HL', result) - elif message_length == 124: # real machine? not tested! + elif message_length == 124: #raise NotImplementedError('this case for system parameters is unknown, please send log messages to add: {}'.format(result)) logging.warning('messages with length 124 might not be decoded correctly!') info_list = struct.unpack('!14L8B8L2BH4B2L2HLL', result) @@ -570,8 +570,8 @@ def get_program_status_text(code): LSV2.PGM_STATE_IDLE: 'No Program running', LSV2.PGM_STATE_UNDEFINED: 'Program state undefined'}.get(code, 'Unknown Program state') - def get_selected_program(self): - """reads the path of the currently active program. + def get_selected_program_stack(self): + """reads the path of the currently active programs and the line number of the execution. See https://github.com/tfischer73/Eclipse-Plugin-Heidenhain/issues/1""" self.login(login=LSV2.LOGIN_DNC) @@ -581,10 +581,14 @@ def get_selected_program(self): result = self._send_recive( LSV2.COMMAND_R_RI, LSV2.RESPONSE_S_RI, payload=payload) if result: - pgm_path = result.decode().strip('\x00').replace('\\', '/') + current_line = struct.unpack('!L', result) + pgm_stack = list() + for entry in result[:4].split(b'\x00'): + if len(entry) > 1: + pgm_stack.append(entry.decode().strip('\x00').replace('\\', '/')) logging.debug( - 'succesfully read path of active program: %s', pgm_path) - return pgm_path + 'succesfully read active program stack and line number: %d @ %s', current_line, pgm_stack) + return pgm_stack else: logging.error( 'an error occurred while querying active program state') From 30ac62e16fa5bfe5e6de5a015d21ba17ca589a59 Mon Sep 17 00:00:00 2001 From: drunsinn Date: Wed, 13 Jan 2021 17:26:38 +0100 Subject: [PATCH 2/4] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 80525ce..21c7085 100644 --- a/.gitignore +++ b/.gitignore @@ -124,3 +124,4 @@ dmypy.json # Pyre type checker .pyre/ .vscode/settings.json +debug.py From c37efba81dfc21daccecb324c4c621cde195c062 Mon Sep 17 00:00:00 2001 From: drunsinn Date: Wed, 13 Jan 2021 17:45:56 +0100 Subject: [PATCH 3/4] change parsing of active pgm to complete pgm stack --- pyLSV2/client.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pyLSV2/client.py b/pyLSV2/client.py index 14e28b5..ede4785 100644 --- a/pyLSV2/client.py +++ b/pyLSV2/client.py @@ -570,7 +570,7 @@ def get_program_status_text(code): LSV2.PGM_STATE_IDLE: 'No Program running', LSV2.PGM_STATE_UNDEFINED: 'Program state undefined'}.get(code, 'Unknown Program state') - def get_selected_program_stack(self): + def get_program_stack(self): """reads the path of the currently active programs and the line number of the execution. See https://github.com/tfischer73/Eclipse-Plugin-Heidenhain/issues/1""" self.login(login=LSV2.LOGIN_DNC) @@ -581,14 +581,15 @@ def get_selected_program_stack(self): result = self._send_recive( LSV2.COMMAND_R_RI, LSV2.RESPONSE_S_RI, payload=payload) if result: - current_line = struct.unpack('!L', result) - pgm_stack = list() - for entry in result[:4].split(b'\x00'): - if len(entry) > 1: - pgm_stack.append(entry.decode().strip('\x00').replace('\\', '/')) + stack_info = dict() + stack_info['Line'] = struct.unpack('!L', result[:4])[0] + print(len(result[4:].split(b'\x00'))) + stack_info['Main_PGM'] = result[4:].split(b'\x00')[0].decode().strip('\x00').replace('\\', '/') + stack_info['Current_PGM'] = result[4:].split(b'\x00')[1].decode().strip('\x00').replace('\\', '/') logging.debug( - 'succesfully read active program stack and line number: %d @ %s', current_line, pgm_stack) - return pgm_stack + 'succesfully read active program stack and line number: %s', stack_info) + + return stack_info else: logging.error( 'an error occurred while querying active program state') From 446819ecdf0ebb5981929363f591dadaf2d827c8 Mon Sep 17 00:00:00 2001 From: drunsinn Date: Wed, 13 Jan 2021 17:50:03 +0100 Subject: [PATCH 4/4] remove debug print --- pyLSV2/client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyLSV2/client.py b/pyLSV2/client.py index ede4785..91a06f0 100644 --- a/pyLSV2/client.py +++ b/pyLSV2/client.py @@ -583,12 +583,10 @@ def get_program_stack(self): if result: stack_info = dict() stack_info['Line'] = struct.unpack('!L', result[:4])[0] - print(len(result[4:].split(b'\x00'))) stack_info['Main_PGM'] = result[4:].split(b'\x00')[0].decode().strip('\x00').replace('\\', '/') stack_info['Current_PGM'] = result[4:].split(b'\x00')[1].decode().strip('\x00').replace('\\', '/') logging.debug( 'succesfully read active program stack and line number: %s', stack_info) - return stack_info else: logging.error(