Warning: Can not open [/home/conf/public_html/cgi-bin/show_python.log]. Ignore
No title
Download script from GPIB_comm.py
Related files:
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("
print(" ?string : write string and read response from GPIB address")
print(" string? : write string and read response from GPIB address")
print(" timeout t: set timeout to t [ms]")
print(" delay t : set delay to t [s]")
print(" exit|quit|bye: exit this program")
print(" d2mate|qiita|scpi|pyvisa|2450: show web pages")
print(" help: show this help")
print("")
def pint(s, defval = None):
try:
return int(s)
except:
return defval
def other_command(str):
if str == "help":
help()
elif str in ["exit", "quit", "bye"]:
terminate()
elif str == 'd2mate':
url1 = "http://conf.msl.titech.ac.jp/D2MatE/D2MatE_programs.html"
os.system(f"start {url1}")
elif str == 'scpi':
url1 = "https://rfmw.em.keysight.com/spdhelpfiles/33500/webhelp/jp/content/__I_SCPI/00%20scpi_introduction.htm"
url2 = "https://rfmw.em.keysight.com/bihelpfiles/aps/webhelp-mobile/JP/Advanced/Content/__H_SCPI%20Programming%20Reference/20%20-%20Command_Quick_Reference.htm"
os.system(f"start {url1}")
os.system(f"start {url2}")
elif str == 'qiita':
url1 = 'https://qiita.com/go_yu_/items/2c03b7c2570628059b18'
url2 = 'https://qiita.com/YujiMatsu/items/8e0437b33555647b0fc4'
os.system(f"start {url1}")
os.system(f"start {url2}")
elif str == 'pyvisa':
url1 = 'https://pyvisa.readthedocs.io/en/latest/'
url2 = 'https://www.texio.co.jp/uploads/WebExpo/Study/Study_0001/Python_VISA.pdf'
url3 = 'https://rightcode.co.jp/blog/information-technology/python-oscilloscope-remote-control-screen-capture'
os.system(f"start {url1}")
os.system(f"start {url2}")
os.system(f"start {url3}")
elif str == '2450':
url1 = 'https://download.tek.com/manual/2450-900-02E_Aug_2019_User.pdf'
os.system(f"start {url1}")
else:
return False
return True
def list_device(is_print = True):
global dev_list
dev_list = rm.list_resources()
if is_print:
print(" Device list:", dev_list)
return dev_list
def get_devstr(addr):
global dev_list
dev_list = list_device(is_print = False)
# print("dev_list=", dev_list)
for s in dev_list:
aa = s.split('::')
# print("s=", s, aa)
if len(aa) < 2:
continue
if pint(aa[1]) == addr:
return s
return None
def close_device():
global inst
if inst:
inst.close()
inst = None
return True
return False
def open_device(devname):
global devstr, address, inst
close_device()
inst = rm.open_resource(devname)
if inst:
print(f" found device [{devname}]. assigned to current instrument handle")
devstr = devname
aa = devname.strip().split('::')
address = pint(aa[1])
else:
print(f" Error: Can not open device [{devname}]")
return inst
def main():
global rm, dev_list, devstr, address, inst
dev_list = list_device()
if len(dev_list) > 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()