from qcodes import VisaInstrument, InstrumentChannel
from typing import Union, List, Tuple, Optional
class KeysightSubModule(InstrumentChannel):
def __init__(
self,
parent: Union[VisaInstrument, InstrumentChannel],
name: str,
slot: int
) -> None:
super().__init__(parent, name)
self.slot = slot
class KeysightSwitchMatrixSubModule(KeysightSubModule):
def validate_value(self, row: int, column: int) -> None:
raise NotImplementedError("Please subclass this")
def to_channel_list(
self,
paths: List[Tuple[int, int]],
wiring_config: Optional[str] = None
) -> str:
raise NotImplementedError("Please subclass this")
def is_open(self, row: int, column: int) -> bool:
self.validate_value(row, column)
channel = self.to_channel_list([(row, column)])
message = self.ask(f'ROUT:OPEN? {channel}')
return bool(int(message))
def is_closed(self, row: int, column: int) -> bool:
self.validate_value(row, column)
channel = self.to_channel_list([(row, column)])
message = self.ask(f'ROUT:CLOSe? {channel}')
return bool(int(message))
def connect(self, row: int, column: int) -> None:
self.validate_value(row, column)
channel = self.to_channel_list([(row, column)])
self.write(f'ROUT:CLOSe {channel}')
def disconnect(self, row: int, column: int) -> None:
self.validate_value(row, column)
channel = self.to_channel_list([(row, column)])
self.write(f'ROUT:OPEN {channel}')
def connect_paths(self, paths: List[Tuple[int, int]]) -> None:
for row, column in paths:
self.validate_value(row, column)
channel_list_str = self.to_channel_list(paths)
self.write(f"ROUTe:CLOSe {channel_list_str}")
def disconnect_paths(self, paths: List[Tuple[int, int]]) -> None:
for row, column in paths:
self.validate_value(row, column)
channel_list_str = self.to_channel_list(paths)
self.write(f"ROUTe:OPEN {channel_list_str}")
def are_closed(self, paths: List[Tuple[int, int]]) -> List[bool]:
for row, column in paths:
self.validate_value(row, column)
channel_list_str = self.to_channel_list(paths)
messages = self.ask(f"ROUTe:CLOSe? {channel_list_str}")
return [bool(int(message)) for message in messages.split(',')]
def are_open(self, paths: List[Tuple[int, int]]) -> List[bool]:
for row, column in paths:
self.validate_value(row, column)
channel_list_str = self.to_channel_list(paths)
messages = self.ask(f"ROUTe:OPEN? {channel_list_str}")
return [bool(int(message)) for message in messages.split(',')]
# Example usage
from qcodes import VisaInstrument
# Connect to the Keysight 34980A main instrument
main_instrument = VisaInstrument("main_instrument_name")
# Create an instance of the KeysightSwitchMatrixSubModule
submodule = KeysightSwitchMatrixSubModule(main_instrument, "submodule_name", 1)
# Check if a channel is open
is_open = submodule.is_open(1, 1)
print(f"Channel is open: {is_open}")
# Connect a channel
submodule.connect(1, 1)
# Check if the channel is closed
is_closed = submodule.is_closed(1, 1)
print(f"Channel is closed: {is_closed}")
# Disconnect the channel
submodule.disconnect(1, 1)
# Check if the channel is open again
is_open = submodule.is_open(1, 1)
print(f"Channel is open: {is_open}")