diff --git a/IsegHVController.py b/IsegHVController.py index c4736f4..3ca5b2b 100755 --- a/IsegHVController.py +++ b/IsegHVController.py @@ -1,140 +1,16 @@ #!/usr/bin/python3 - + +import IsegLibrary as iseg import os -import subprocess import datetime import csv +import socket -cmd0Str = "-v 2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " - -def SendCmd(option,cmd): - if option == 0 : - cmdStr = "snmpget " + cmd0Str + cmd - elif option == 1: - cmdStr = "snmpset " + cmd0Str + cmd - elif option == 2: - cmdStr = "snmpwalk " + cmd0Str + cmd - else : - cmdStr = "echo option: 0 - get, 1 - set, 2 - walk" - #print(cmdStr) - result = str(subprocess.check_output(cmdStr, shell=True)) - return result.lstrip('b\'').rstrip('\'').rstrip('\n') - -#======== Get settings -def GetHV(ch): - haha = SendCmd(0, "outputVoltage.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetCurrent(ch): - haha = SendCmd(0, "outputCurrent.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetOutputHV(ch): - haha = SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def GetLI(ch): - haha = SendCmd(0, "outputMeasurementCurrent.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-3].strip()) - -def IsHVOn(ch): - return SendCmd(0, "outputSwitch.u"+str(ch)) - -def GetHVRiseRate(ch): - haha = SendCmd(0, "outputVoltageRiseRate.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-5].strip()) - -def GetHVFallRate(ch): - haha = SendCmd(0, "outputVoltageFallRate.u" + str(ch)) - aa = haha.find('Float:') - return float(haha[aa+6:-5].strip()) - -#======== Get All channel Setting using snmp walk - -def GetChList(): - haha = SendCmd(2, "outputName") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputName.'): - if len(k) > 0 : - aa = k.find("=") - k = k[0:aa].strip().lstrip('u') - kaka.append(int(k)) - return kaka - -def GetAllHV(): - haha = SendCmd(2, "outputVoltage") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputVoltage'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllCurrent(): - haha = SendCmd(2, "outputCurrent") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputCurrent'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllOutputHV(): - haha = SendCmd(2, "outputMeasurementSenseVoltage") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -def GetAllLC(): - haha = SendCmd(2, "outputMeasurementCurrent") - kaka = [] - for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'): - if len(k) > 0 : - aa = k.find("Float:") - k = k[aa+6: -3].strip() - kaka.append(float(k)) - return kaka - -#======== Set Settings -def SetHV(ch, val): - return SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val)) - -def SetCurrent(ch, val): - return SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val)) - -def SwitchOnHV(ch, onOff): - return SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff)) - -def SetHVRiseRate(ch, rate): - return SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate)) - -def SetHVFallRate(ch, rate): - return SendCmd(1, "outputVoltageFallRate.u" + str(ch) + " F " + str(rate)) - -#===================== SandBox - -#print( GetOutputHV(0) ) -#print( GetLI(0) ) -#print( GetHVRiseRate(0) ) -#print( GetHVFallRate(0) ) -# -#print( SendCmd(1, "outputCurrent.u1 F 0.0005")) - -#print( SendCmd(2, "outputCurrent")) - -#hvList = GetAllOutputHV() -#print(hvList) +#assign a port, to prevent the script run mulitple time +s = socket.socket() +host = socket.gethostname() +port = 4305 +s.bind((host,port)) #===================== GUI import PySimpleGUI as sg @@ -143,14 +19,14 @@ import PySimpleGUI as sg header = ["name", "HV [V]", "Current [mA]"] -chList = GetChList() -hvList = GetAllHV() # get all V -iList = GetAllCurrent() # get all current -outVList = GetAllOutputHV() -outIList = GetAllLC() -nChannel = len(chList) +chList = iseg.GetChList() +hvList = iseg.GetAllHV() # get all V +iList = iseg.GetAllCurrent() # get all current +outVList = iseg.GetAllOutputHV() +outIList = iseg.GetAllLC() -updateTime = 5 #sec +nChannel = 10 #len(chList) +updateTime = 60 #sec fileName = '' @@ -184,8 +60,15 @@ layout.append([sg.FileSaveAs('Save As', target = '-Save-', initial_folder='~', f sg.Input(default_text=fileName, expand_x = True, justification = "left", readonly = True, enable_events=True, key="-Save-" ), sg.Input('', visible = False, enable_events=True, key="-Load-" ) ]) + +comboList = ["Get Rise Rate [V/s]", "Get Fall Rate [V/s]", "Set Rise Rate [V/s]", "Set Fall Rate [V/s]"] +layout.append([sg.Combo(comboList, default_value = comboList[0], size = 20, enable_events=True, key="-VRateCombo-"), + sg.Text("Ch:", size = 3), + sg.Combo(chList, default_value = chList[0], size = 4, enable_events=True, key="-VRateCh-"), + sg.Input('', size = 6, justification = "right", enable_events=True, key="-VRate-") + ]) -window = sg.Window('Iseg HV Control', layout, finalize = True, keep_on_top = True) +window = sg.Window('Iseg HV Control & Monitor', layout, finalize = True, keep_on_top = True) for i in range(0, nChannel): window[("v%d" % chList[i])].bind("", "_Enter") @@ -208,7 +91,7 @@ while True: if event[0:1] == 'c' : ID = event[1:] - SwitchOnHV(int(ID), int(window[event].get())) + iseg.SwitchOnHV(int(ID), int(window[event].get())) if event == '-Save-' : fileName = values["Save As"] @@ -229,27 +112,42 @@ while True: window[("n%d" % chList[i])].update(row[0]) window[("v%d" % chList[i])].update(row[2]) window[("i%d" % chList[i])].update(row[3]) - SetHV(chList[i], float(row[2])) - SetCurrent(chList[i], float(row[3])/1000) + iseg.SetHV(chList[i], float(row[2])) + iseg.SetCurrent(chList[i], float(row[3])/1000) i += 1 + if event in ["-VRateCombo-", "-VRateCh-", "-VRate-"]: + item = window["-VRateCombo-"].get() + ch = window["-VRateCh-"].get() + val = window["-VRate-"].get() + if item == comboList[0]: + window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) + if item == comboList[1]: + window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) + if item == comboList[2]: + iseg.SetHVRiseRate(ch, val) + window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) + if item == comboList[3]: + iseg.SetHVFallRate(ch, val) + window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) + haha = event.find('_Enter') if haha > 0 : ID = event[:haha] ch = int(ID[1:]) if event[0:1] == 'v' : - SetHV(ch, float(window[ID].get())) - window[ID].update("%.3f" % GetHV(ch)) + iseg.SetHV(ch, float(window[ID].get())) + window[ID].update("%.3f" % iseg.GetHV(ch)) if event[0:1] == 'i' : - SetCurrent(ch, float(window[ID].get())/1000.) - window[ID].update("%.3f" % (GetCurrent(ch)*1000)) + iseg.SetCurrent(ch, float(window[ID].get())/1000.) + window[ID].update("%.3f" % (iseg.GetCurrent(ch)*1000)) if event == "_TIMEOUT_" : #hvList = GetAllHV() # get all V #iList = GetAllCurrent() # get all current - outVList = GetAllOutputHV() - outIList = GetAllLC() + outVList = iseg.GetAllOutputHV() + outIList = iseg.GetAllLC() tempFile = open("temp.dat", "w") diff --git a/IsegLibrary.py b/IsegLibrary.py new file mode 100755 index 0000000..2b42ff9 --- /dev/null +++ b/IsegLibrary.py @@ -0,0 +1,159 @@ +#!/usr/bin/python3 + +import os +import subprocess + +cmd0Str = "-v 2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " + +def SendCmd(option,cmd): + if option == 0 : + cmdStr = "snmpget " + cmd0Str + cmd + elif option == 1: + cmdStr = "snmpset " + cmd0Str + cmd + elif option == 2: + cmdStr = "snmpwalk " + cmd0Str + cmd + else : + cmdStr = "echo option: 0 - get, 1 - set, 2 - walk" + #print(cmdStr) + result = str(subprocess.check_output(cmdStr, shell=True)) + return result.lstrip('b\'').rstrip('\'').rstrip('\n') + +#======== Get settings +def GetHV(ch): + haha = SendCmd(0, "outputVoltage.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + +def GetCurrent(ch): + haha = SendCmd(0, "outputCurrent.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + +def GetOutputHV(ch): + haha = SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + +def GetLI(ch): + haha = SendCmd(0, "outputMeasurementCurrent.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-3].strip()) + +def IsHVOn(ch): + return SendCmd(0, "outputSwitch.u"+str(ch)) + +def GetHVRiseRate(ch): + haha = SendCmd(0, "outputVoltageRiseRate.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-5].strip()) + +def GetHVFallRate(ch): + haha = SendCmd(0, "outputVoltageFallRate.u" + str(ch)) + aa = haha.find('Float:') + return float(haha[aa+6:-5].strip()) + +#======== Get All channel Setting using snmp walk + +def GetChList(): + haha = SendCmd(2, "outputName") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputName.'): + if len(k) > 0 : + aa = k.find("=") + k = k[0:aa].strip().lstrip('u') + kaka.append(int(k)) + return kaka + +def GetAllHV(): + haha = SendCmd(2, "outputVoltage") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputVoltage'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + +def GetAllCurrent(): + haha = SendCmd(2, "outputCurrent") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputCurrent'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + +def GetAllOutputHV(): + haha = SendCmd(2, "outputMeasurementSenseVoltage") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + +def GetAllLC(): + haha = SendCmd(2, "outputMeasurementCurrent") + kaka = [] + for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'): + if len(k) > 0 : + aa = k.find("Float:") + k = k[aa+6: -3].strip() + kaka.append(float(k)) + return kaka + +#======== Set Settings +def SetHV(ch, val): + try : + int(ch) + float(val) + return SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val)) + except: + print("either ch is not int or val is not float") + +def SetCurrent(ch, val): + try : + int(ch) + float(val) + return SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val)) + except: + print("either ch is not int or val is not float") + +def SwitchOnHV(ch, onOff): + try : + int(ch) + int(onOff) + return SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff)) + except : + print("either ch or onOff is not int") + +def SetHVRiseRate(ch, rate): + try : + int(ch) + int(rate) + return SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate)) + except: + print("either ch is not int or rate is not float") + +def SetHVFallRate(ch, rate): + try : + int(ch) + int(rate) + return SendCmd(1, "outputVoltageFallRate.u" + str(ch) + " F " + str(rate)) + except: + print("either ch is not int or rate is not float") +#===================== SandBox + +#print( GetOutputHV(0) ) +#print( GetLI(0) ) +#print( GetHVRiseRate(0) ) +#print( GetHVFallRate(0) ) +# +#print( SendCmd(1, "outputCurrent.u1 F 0.0005")) + +#print( SendCmd(2, "outputCurrent")) + +#hvList = GetAllOutputHV() +#print(hvList)