import os import sys import pyvisa address = None dev_list = [] devstr = "" inst = None rm = pyvisa.ResourceManager() def terminate(): print("") exit() def help(): print("") print(f" Usage: python {sys.argv[0]}") print(" Command:") print(" list : show device list") print(" addr 18 : set GPIB address to 18") print(" >string : write 'string' to the GPIB address") print(" 0: inst = open_device(dev_list[0]) while 1: print("# ", end = '') instr = input().strip() str = instr.lower() aa = str.split(' ', 1) cmd = aa[0] if len(aa) >= 2: argline = aa[1] else: argline = '' # print("cmd=", cmd, argline) if str == "": continue elif str == 'list': list_device() elif str[:4] == 'addr': if argline == '': print(" Error: addr command takes one integer as GPIB address") continue v = pint(argline) if v is None: print(" Error: Invalid address [{argline}]") continue address = v print(f" set GPIB address to {address}") devstr = get_devstr(address) if devstr is None: print(f" Error: GPIB address [{address}] is not connected") list_device() else: inst = open_device(devstr) elif str[:8] == 'timeout': if largline == '': print(f" Error: timeout is not given. specify by ms") continue v = pint(argline) if v is None: print(f" Error: Invalid timeout [{argline}]. specify by ms") continue inst.timeout = v elif str[:5] == 'delay': if argline == '': print(f" Error: delay is not given. specify by seconds") continue v = pint(argline) if v is None: print(f" Error: Invalid delay [{argline}]. specify by seconds") continue inst.delay = v elif str[0] == '>': query =instr[1:].strip() if inst is None: print(f" Error: Instrument is not specified. Use addr command") continue print(f" write [{query}] to {devstr}") try: inst.write(query) except: print(f" Error: Illeagal query [{query}]") elif str[0] == '<': if inst is None: print(f" Error: Instrument is not specified. Use addr command") continue print(f" reading from {devstr}") try: ret = inst.read() print(f" Return: [{ret.strip()}]") except: print(f" Error: read timeout") elif str[0] == '?' or cmd[-1] == '?': if str[0] == '?': query = instr[1:].strip() else: query = instr.strip() if inst is None: print(f" Error: Instrument is not specified. Use addr command") continue print(f" write [{query}] to {devstr} and waiting query") try: ret = inst.query(query) print(f" Return: [{ret.strip()}]") except: print(f" Error: Illeagal query [{query}]") else: ret = other_command(instr) if not ret: print(f" Warning: Ivvalid command [{instr}]") close_device() if __name__ == "__main__": main()