Skip to content

Instantly share code, notes, and snippets.

@K4zuki
Last active November 26, 2022 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K4zuki/68197cefc314d767ad8d071348a1ccc0 to your computer and use it in GitHub Desktop.
Save K4zuki/68197cefc314d767ad8d071348a1ccc0 to your computer and use it in GitHub Desktop.
Keysight U2751A USB Modular Switch Matrix SCPI commands
import sys
if sys.version_info > (3, 6, 0):
from typing import Tuple, List
from collections import namedtuple
class ScpiKeyword(namedtuple("ScpiKeyword", ["long", "short"])):
"""
- long: `str`
- short: `str`
- param_callback: `function`
"""
def __str__(self):
return self.long
def match(self, candidate):
"""
:param str candidate:
:return Boolean:
"""
short = self.short.upper()
long = self.long.upper()
if isinstance(candidate, str):
candidate = candidate.upper()
return candidate.startswith(short) and long.startswith(candidate)
else:
return False
ScpiCommand = namedtuple("ScpiCommand", ["keywords", "param_callback"])
ScpiCommand.__doc__ = """ ScpiCommand
- :param list keywords: list of `ScpiKeyword`s
- :param object param_callback: `function pointer`
"""
kw = ScpiKeyword("KEYWord", "KEYW")
class MicroScpiDevice:
commands_write = [(ScpiCommand((kw, kw), None), ScpiCommand((kw, kw), None))] # type: List[ScpiCommand]
commands_query = [(ScpiCommand((kw, kw), None), ScpiCommand((kw, kw), None))] # type: List[ScpiCommand]
@staticmethod
def strip_semicolon(input_str: str):
out = input_str.split(";")[0]
return out
def mini_lexer(self, line: str):
""" Split `line` into tuple of (list of keywords) and a parameter string
:param str line: candidate command string
:return tuple: ([keywords], param)
"""
line = self.strip_semicolon(line)
command = line
param = None
if " " in line:
print("split")
command = line.split()[0]
param = line.lstrip(command)
command = command.split(":")
return command, param
def parse_and_process(self, line: str):
""" Parse `line` and process if it is valid
:param str line: candidate command string
"""
line = line.strip()
if len(line) == 0:
return
candidate_cmd, candidate_param = self.mini_lexer(line)
commands = self.commands_query if "?" in candidate_cmd[-1] else self.commands_write
candidate_cmd[-1] = candidate_cmd[-1].strip("?")
length_matched = [c for c in commands if len(c.keywords) == len(candidate_cmd)]
if len(length_matched) == 0:
print("{}: command not found".format(':'.join(candidate_cmd)))
else:
for command in commands:
if all(keyword.match(kw_candidate) for keyword, kw_candidate in zip(command.keywords, candidate_cmd)):
command.param_callback(candidate_param)
break
else:
print("{}: command not found".format(':'.join(candidate_cmd)))
  • DIAGnostic

    • DIAGnostic:RELay:CYCLes?
    • DIAGnostic:RELay:CYCLes:CLEar
  • ROUTe

    • ROUTe:CLOSe
    • ROUTe:CLOSe?
    • ROUTe:OPEN
    • ROUTe:OPEN?
  • SYSTem

    • SYSTem:CDEScription?
    • SYSTem:ERRor?
    • SYSTem:VERSion?
  • *CLS

  • *ESE/*ESE?

  • *ESR?

  • *IDN?

  • *OPC/*OPC?

  • *RST

  • *SRE/*SRE?

  • *STB?

  • *TST?

{
"urls": [
["MicroScpiDevice.md", "https://gist.github.com/K4zuki/68197cefc314d767ad8d071348a1ccc0#file-microscpidevice-py"]
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment