MeasKit/__init__.py
2025-11-21 21:00:41 +01:00

44 lines
1.9 KiB
Python

import json
import sys
def instrument(pyvisa_instrument_address, device_handle):
"""
Create an instance
Args:
instrument_handle (Pyvisa Object): Input Pyvisa Object linked to target device.
device_handle (String): Input String indicating the make of the instrument.
Returns:
Object: Object of the device with bare essential interface commands.
"""
# load the known devices json
handle_exists_flag = 0
module_obj = []
f = open('supported_devices.json')
allowed_handles = json.load(f)
for handle in allowed_handles['supported_instruments']:
# If handle found, create object
if handle["make"] == device_handle:
handle_exists_flag = 1
# load the right instrument module and save as global import
try:
sys.path.append(f"equipment/{handle["type"]}/py_generated/")
module_obj = __import__(f'{handle["make"]}')
module_obj = eval(f'module_obj.{handle["make"]}("{pyvisa_instrument_address}","{device_handle}")')
except:
raise ValueError(f'[MeasKit] Invalid \"{device_handle}\" instrument class.')
break
# If similar handle, warn user
elif handle["make"].lower() == device_handle.lower():
tmp = handle["make"]
print(f'[MeasKit] Similarely capitalized device handle found. Handle \"{device_handle}\" was queried for but \"{tmp}\" was found.')
f.close()
# If no exactly matching handles found error
if not handle_exists_flag:
raise ValueError(f'[MeasKit] Device handle \"{device_handle}\" not supported yet.')
# waste some resources in the mem heap, investigate removing these
return module_obj