Skip to content

Commit

Permalink
reduce log levels, adress errors in reading directory's and drive
Browse files Browse the repository at this point in the history
add error message to notify user about problem with reading drive information
reduce log levels
  • Loading branch information
drunsinn committed Dec 13, 2023
1 parent 439db4d commit d9572e5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
35 changes: 24 additions & 11 deletions pyLSV2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def _send_recive(
self._logger.debug("no response expected")
return False

self._logger.warning("received unexpected response %s", self._llcom.last_response)
self._logger.info("received unexpected response %s", self._llcom.last_response)
return False

def _send_recive_block(
Expand Down Expand Up @@ -264,7 +264,7 @@ def _send_recive_block(
lsv_content = self._llcom.telegram(command=lc.RSP.T_OK)
return response_buffer

self._logger.warning(
self._logger.info(
"received unexpected response %s, with data %s",
self._llcom.last_response,
lsv_content,
Expand Down Expand Up @@ -419,7 +419,7 @@ def _read_parameters(self, force: bool = False) -> ld.SystemParameters:
raise LSV2DataException("expected boolean")
self._sys_par.turbo_mode_active = data
else:
self._logger.warning("an error occurred while querying system information on turbo mode")
self._logger.debug("could not read system information on turbo mode")

payload = struct.pack("!L", lc.ParRCI.DNC_ALLOWED)
result = self._send_recive(lc.CMD.R_CI, payload, lc.RSP.S_CI)
Expand All @@ -429,14 +429,14 @@ def _read_parameters(self, force: bool = False) -> ld.SystemParameters:
raise LSV2DataException("expected boolean")
self._sys_par.dnc_mode_allowed = data
else:
self._logger.warning("an error occurred while querying system information on dnc mode")
self._logger.debug("could not read system information on dnc mode")

payload = struct.pack("!L", lc.ParRCI.AXES_SAMPLING_RATE)
result = self._send_recive(lc.CMD.R_CI, payload, lc.RSP.S_CI)
if isinstance(result, (bytearray,)) and len(result) > 0:
self._sys_par.axes_sampling_rate = lm.decode_system_information(result)
else:
self._logger.warning("an error occurred while querying system information on axes samling rate")
self._logger.debug("could not read system information on axes samling rate")
return self._sys_par

def _read_version(self, force: bool = False) -> ld.VersionInfo:
Expand Down Expand Up @@ -616,13 +616,19 @@ def change_directory(self, remote_directory: str) -> bool:
return False

dir_path = remote_directory.replace("/", lc.PATH_SEP)

payload = lm.ustr_to_ba(dir_path)

result = self._send_recive(lc.CMD.C_DC, payload, lc.RSP.T_OK)
if isinstance(result, (bool,)) and result is True:
self._logger.debug("changed working directory to %s", dir_path)
return True
self._logger.warning("an error occurred while changing directory")

if remote_directory == self.directory_info().path:
self._logger.info("control responded as if the dir change did not work but path is still correct...")
return True

self._logger.warning("an error occurred while changing directory to %s", dir_path)
return False

def file_info(self, remote_file_path: str) -> Union[ld.FileEntry, None]:
Expand Down Expand Up @@ -690,6 +696,8 @@ def drive_info(self) -> List[ld.DriveEntry]:
"""
Read info all drives and partitions from the control.
Requires access level ``FILETRANSFER`` to work.
Might not work on older controls or on old windows programming stations?
"""

if not self.login(lc.Login.FILETRANSFER):
Expand All @@ -703,7 +711,7 @@ def drive_info(self) -> List[ld.DriveEntry]:
for entry in result:
drives_list.extend(lm.decode_drive_info(entry))

self._logger.debug(
self._logger.debug(
"successfully received %d packages for drive information %s",
len(result),
drives_list,
Expand All @@ -713,6 +721,10 @@ def drive_info(self) -> List[ld.DriveEntry]:
"an error occurred while reading drive info: '%s'",
lt.get_error_text(self.last_error),
)

if "TNC:" not in [d.name for d in drives_list]:
self._logger.warning("an error occured while parsing drive info. this might be either a problem with the decoding or the control does not support this function!")
return []
return drives_list

def make_directory(self, dir_path: str) -> bool:
Expand Down Expand Up @@ -1037,7 +1049,7 @@ def send_file(
else:
#if len(result) == 2:

self._logger.warning(
self._logger.info(
"could not send data, received unexpected response '%s' with data 0x%s",
self._llcom.last_response,
result.hex()
Expand Down Expand Up @@ -1572,8 +1584,9 @@ def _walk_dir(self, descend: bool = True) -> List[str]:
self._logger.warning("clould not log in as user FILE")
return []

current_path = self.directory_info().path
current_path = self.directory_info().path.replace("/", lc.PATH_SEP)
content: List[str] = []

for entry in self.directory_content():
if entry.name == "." or entry.name == ".." or entry.name.endswith(":"):
continue
Expand All @@ -1600,9 +1613,9 @@ def get_file_list(self, path: str = "", descend: bool = True, pattern: str = "")
return []

if self.change_directory(path) is False:
self._logger.warning("could not change to directory")
self._logger.warning("could not change to directory %s" % path)
return []

if len(pattern) == 0:
file_list = self._walk_dir(descend)
else:
Expand Down
48 changes: 31 additions & 17 deletions pyLSV2/scripts/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def comprehensive_demo():

print("Basics:")
print("# Connected to a '{:s}' running software version '{:s}'".format(con.versions.control, con.versions.nc_sw))
print("# Version as numeric values base:{:d} type:{:d} version:{:d} service pack:{:d}".format(con.versions.nc_sw_base, con.versions.nc_sw_type, con.versions.nc_sw_version, con.versions.nc_sw_service_pack))
print(
"# Using LSV2 version '{:d}' with version flags '0x{:02x}' and '0x{:02x}'".format(
con.parameters.lsv2_version,
Expand All @@ -58,12 +59,15 @@ def comprehensive_demo():
print("# Time and date: {:}".format(con.get_remote_datetime()))

# read error messages via LSV2, works only on iTNC controls
print("# read error messages, only available on iTNC530")
print("# read error messages, only available on some iTNC530 versions")
if con.versions.is_itnc():
e_m = con.get_error_messages()
print("## Number of currently active error messages: {:d}".format(len(e_m)))
for i, msg in enumerate(e_m):
print("### Error {:d} : {:s}".format(i, str(msg)))
if con.versions.nc_sw_base == 340490 and con.versions.nc_sw_version <= 2:
print("### control is iTNC but does not support this function")
else:
e_m = con.get_error_messages()
print("## Number of currently active error messages: {:d}".format(len(e_m)))
for i, msg in enumerate(e_m):
print("### Error {:d} : {:s}".format(i, str(msg)))
else:
print("## function 'get_error_messages()' not suportet for this control")

Expand Down Expand Up @@ -94,20 +98,26 @@ def comprehensive_demo():
print("## input: {}".format(con.read_plc_memory(0, MemoryType.INPUT, 5)))
print("## output: {}".format(con.read_plc_memory(0, MemoryType.OUTPUT_WORD, 5)))

print("# data values via data path, only available on iTNC530")
print("# data values via data path, only available on some iTNC530")
if con.versions.is_itnc():
print("## marker 0: {}".format(con.read_data_path("/PLC/memory/M/0")))
print("## marker 1: {}".format(con.read_data_path("/PLC/memory/M/1")))
print("## string 0: {}".format(con.read_data_path("/PLC/memory/S/0")))
print("## word 10908: {}".format(con.read_data_path("/PLC/memory/W/10908")))
if con.versions.nc_sw_base == 340490 and con.versions.nc_sw_version <= 2:
print("### control is iTNC but does not support this function")
else:
print("## marker 0: {}".format(con.read_data_path("/PLC/memory/M/0")))
print("## marker 1: {}".format(con.read_data_path("/PLC/memory/M/1")))
print("## string 0: {}".format(con.read_data_path("/PLC/memory/S/0")))
print("## word 10908: {}".format(con.read_data_path("/PLC/memory/W/10908")))
else:
print("## function 'read_data_path()' not suportet for this control")
print("# table values via data path, only available on iTNC530")
print("# table values via data path, only available on some iTNC530")
if con.versions.is_itnc():
print("## values from tool table for tool T1:")
print("## DOC column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/DOC")))
print("## L column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/L")))
print("## R column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/R")))
if con.versions.nc_sw_base == 340490 and con.versions.nc_sw_version <= 2:
print("### control is iTNC but does not support this function")
else:
print("## values from tool table for tool T1:")
print("## DOC column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/DOC")))
print("## L column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/L")))
print("## R column: {}".format(con.read_data_path("/TABLE/TOOL/T/1/R")))
else:
print("## function 'read_data_path()' not suportet for this control")

Expand All @@ -123,7 +133,7 @@ def comprehensive_demo():
if con.versions.is_tnc7():
print("UI Interface test not available on TNC7?")
else:
print("UI Interface")
print("UI Interface:")
print("# switch to mode manual")
con.set_keyboard_access(False)
con.send_key_code(pyLSV2.KeyCode.MODE_MANUAL)
Expand All @@ -135,7 +145,7 @@ def comprehensive_demo():
con.send_key_code(pyLSV2.KeyCode.MODE_PGM_EDIT)
con.set_keyboard_access(True)

print("File access")
print("File access:")
drv_info = con.drive_info()
print("# names of disk drives: {:s}".format(", ".join([drv.name for drv in drv_info])))
dir_info = con.directory_info()
Expand All @@ -153,9 +163,13 @@ def comprehensive_demo():
for file_entry in only_dir:
print("## directory name: {:s}, date {:}".format(file_entry.name, file_entry.timestamp))

#con.change_directory("TNC:/smartNC")
#print([c.name for c in con.directory_content()])

print("# file search")
h_files = con.get_file_list(path="TNC:", pattern=r"[\$A-Za-z0-9_-]*\.[hH]$")
print("## found {:d} klartext programs on TNC drive".format(len(h_files)))
print([f for f in h_files])
i_files = con.get_file_list(path="TNC:", pattern=r"[\$A-Za-z0-9_-]*\.[iI]$")
print("## found {:d} ISO programs on TNC drive".format(len(i_files)))

Expand Down

0 comments on commit d9572e5

Please sign in to comment.