Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactoring changes to USBBinds.py #226

Merged
merged 6 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions basil/utils/USBBinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def find_usb_binds(rm, log,
instruments,
binds_to_skip=[],
memorized_binds=[],
timeout=1000 * 4,
verbose=False,
timeout=1000 * 4
):
"""
Finds the USB bind for each instrument in the given list of instruments.
Expand All @@ -59,8 +58,6 @@ def find_usb_binds(rm, log,
Defaults to an empty list.
timeout (int, optional): Timeout value in milliseconds.
Defaults to 4000.
verbose (bool, optional): Flag indicating whether to log verbose messages.
Defaults to False.

Returns:
dict: A dictionary mapping the identification strings of the instruments
Expand All @@ -73,35 +70,42 @@ def find_usb_binds(rm, log,
results = {}

for instrument in instruments:
# try first ASRL port
if instrument.get("port") and 'ASRL' not in instrument.get("port"):
instrument["port"] = f'ASRL{instrument["port"]}::INSTR'
resources = (instrument["port"],) + rm.list_resources() if instrument.get("port") else rm.list_resources()
log.info(f"Searching for {instrument['identification']}")

resources = rm.list_resources()

if "port" in instrument.keys():
port = instrument.get("port")

if port in resources:
if 'ASRL' not in port:
instrument["port"] = f'ASRL{port}::INSTR'

resources = (instrument["port"],) + resources

for i, res in enumerate(resources):
log.debug(f"[{i}] Trying {res}")

for res in resources:
if "USB" not in res: # Only search for USB devices
log.debug(f"Skipping non USB bind {res}")
continue

if any(bind in res for bind in skip_binds):
if verbose:
log.info(f"Skipping USB bind {res}")
log.debug(f"Skipping USB bind {res}")
continue

try:
if verbose:
log.info(f"Trying {res} with baud rate {instrument['baud_rate']}")
log.debug(f"Trying {res} with baud rate {instrument['baud_rate']}")

if any(res in bind for bind in memorized_binds):
if verbose:
log.info(f"Found memorized bind {res}")
log.debug(f"Found memorized bind {res}")
result = memorized_binds[res]
else:
result = query_identification(rm, res, instrument['baud_rate'], instrument['read_termination'], instrument['write_termination'], timeout=timeout, verbose=verbose)
result = query_identification(rm, res, instrument['baud_rate'], instrument['read_termination'], instrument['write_termination'], timeout=timeout)

memorized_binds.append({res, result})

if verbose:
log.info(f"Found {result.strip()}")
log.debug(f"Found {result.strip()}")

if result.lower().strip() in [inst["identification"].lower().strip() for inst in instruments]:
substring = res.split("/")[2].split("::")[0]
Expand All @@ -114,6 +118,10 @@ def find_usb_binds(rm, log,
if len(results) == len(instruments):
return results

log.debug(f"Found {len(results)} out of {len(instruments)}")

break

except pyvisa.VisaIOError:
pass

Expand Down Expand Up @@ -174,7 +182,7 @@ def load_yaml_with_comments(conf):
return conf_dict


def modify_basil_config(conf, log, skip_binds=[], save_modified=None, verbose=False):
def modify_basil_config(conf, log, skip_binds=[], save_modified=None):
"""
Modifies the basil configuration file by finding USB binds for devices.

Expand All @@ -183,7 +191,6 @@ def modify_basil_config(conf, log, skip_binds=[], save_modified=None, verbose=Fa
log: The logger object for logging messages.
skip_binds (list, optional): List of USB binds to skip. Defaults to [].
save_modified (str, optional): Path to save the modified basil configuration file. Defaults to None.
verbose (bool, optional): Flag to enable verbose logging. Defaults to False.

Returns:
dict: The modified basil configuration dictionary.
Expand All @@ -202,8 +209,7 @@ def modify_basil_config(conf, log, skip_binds=[], save_modified=None, verbose=Fa
or "read_termination" not in tf["init"].keys()
or not any(e in tf["init"].keys() for e in ["baud_rate", "baudrate"])
):
if verbose:
log.debug(f"Skipping {tf['type']} transfer layer with name {tf['name']}")
log.debug(f"Skipping {tf['type']} transfer layer with name {tf['name']}")
continue

instrument = tf["init"]["identification"]
Expand All @@ -222,7 +228,7 @@ def modify_basil_config(conf, log, skip_binds=[], save_modified=None, verbose=Fa

insts_idx_map[instrument.lower().strip()] = i

found_binds = find_usb_binds(rm, log=log, instruments=instruments, binds_to_skip=skip_binds, verbose=verbose)
found_binds = find_usb_binds(rm, log=log, instruments=instruments, binds_to_skip=skip_binds)

for inst in found_binds.keys():
if found_binds[inst] is None:
Expand Down
Loading