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

Support of USB bind identification for iseq power supplies #235

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 24 additions & 8 deletions basil/utils/USBBinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ruamel.yaml


def query_identification(rm, resource, baud_rate, read_termination=None, write_termination=None, timeout=1000 * 5):
def query_identification(rm, resource, baud_rate, read_termination=None, write_termination=None, timeout=1000 * 5, indentifer_cmd="*IDN?", read_after_query=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in indentifier

"""
Queries the identification of the instrument connected via USB.

Expand All @@ -31,18 +31,22 @@ def query_identification(rm, resource, baud_rate, read_termination=None, write_t
inst.read_termination = read_termination
inst.write_termination = write_termination
try:
reply = inst.query("*IDN?", delay=0.1)
reply = inst.query(indentifer_cmd, delay=0.1)
except pyvisa.VisaIOError:
# This retries the query a second time, since some devices do not answer the first time.
# If a second exception arrises, it will be handled in calling function.
reply = inst.query("*IDN?", delay=0.1)
return reply
reply = inst.query(indentifer_cmd, delay=0.1)

if read_after_query:
return inst.read()
else:
return reply
Comment on lines +40 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the SHQ 122m returns as query response the command string as check if the command was executed successfully. To get the return value of an command (or in this case the identification string) one has to additionally perform a read operation.



def find_usb_binds(rm, log,
instruments,
binds_to_skip=[],
memorized_binds=[],
memorized_binds={},
timeout=1000 * 4
):
"""
Expand Down Expand Up @@ -101,13 +105,16 @@ def find_usb_binds(rm, log,
try:
log.debug(f"Trying {res} with baud rate {instrument['baud_rate']}")

if any(res in bind for bind in memorized_binds):
if any(res in bind for bind in memorized_binds.keys()):
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)
if instrument["type"].lower() == "iseg_hv":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make a global "devices_with_non_default_values" dict, so that it is easier to potentially add more devices there instead of blowing up an if clause?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will do!

result = query_identification(rm, res, instrument['baud_rate'], instrument['read_termination'], instrument['write_termination'], timeout=timeout, indentifer_cmd="#", read_after_query=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same typo as above

else:
result = query_identification(rm, res, instrument['baud_rate'], instrument['read_termination'], instrument['write_termination'], timeout=timeout)

memorized_binds.append({res, result})
memorized_binds.update({res: result})

log.debug(f"Found {result.strip()}")

Expand Down Expand Up @@ -228,6 +235,8 @@ def modify_basil_config(conf, log, skip_binds=[], save_modified=None):
port = None

instruments.append({
"name": tf["name"],
"type": "", # Standard instruments with *IQN? command
"identification": instrument,
"baud_rate": baud_rate,
"read_termination": read_termination,
Expand All @@ -237,6 +246,13 @@ def modify_basil_config(conf, log, skip_binds=[], save_modified=None):

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

for hw_driver in conf["hw_drivers"]:
interface = hw_driver["interface"]
for instrument in instruments:
if instrument["name"].lower().strip() == interface.lower().strip():
instrument["type"] = hw_driver["type"].lower()
break

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

for inst in found_binds.keys():
Expand Down
Loading