ask IP at the beginning of the program

This commit is contained in:
dirac 2022-08-08 12:24:23 -04:00
parent ffc09c7278
commit 4aa05ba873
2 changed files with 218 additions and 178 deletions

View File

@ -12,6 +12,8 @@ host = socket.gethostname()
port = 4305 port = 4305
s.bind((host,port)) s.bind((host,port))
IP = input('Mpod IP address to connect : ')
#===================== GUI #===================== GUI
import PySimpleGUI as sg import PySimpleGUI as sg
@ -19,12 +21,17 @@ import PySimpleGUI as sg
header = ["name", "HV [V]", "Current [mA]"] header = ["name", "HV [V]", "Current [mA]"]
chList = iseg.GetChList() mpod = iseg.Mpod(IP)
hvList = iseg.GetAllHV() # get all V
iList = iseg.GetAllCurrent() # get all current if mpod.isConnected == False:
outVList = iseg.GetAllOutputHV() exit()
outIList = iseg.GetAllLC()
onOffList = iseg.GetAllOnOff() chList = mpod.GetChList()
hvList = mpod.GetAllHV() # get all V
iList = mpod.GetAllCurrent() # get all current
outVList = mpod.GetAllOutputHV()
outIList = mpod.GetAllLC()
onOffList = mpod.GetAllOnOff()
modChList = iseg.SplitChList(chList) modChList = iseg.SplitChList(chList)
@ -39,7 +46,6 @@ for k in range(0, nMod):
layoutTab.append([]) layoutTab.append([])
for k in range(0, nMod): for k in range(0, nMod):
baseI = 0 baseI = 0
for kk in range(0, k): for kk in range(0, k):
baseI += len(modChList[kk]) baseI += len(modChList[kk])
@ -56,9 +62,7 @@ for k in range(0, nMod):
]) ])
for j in range(0, len(modChList[k])) : for j in range(0, len(modChList[k])) :
i = baseI + j i = baseI + j
layoutTab[k].append( layoutTab[k].append(
[ [
sg.Input(default_text='', size = 8, justification = "left", key=("n%d" % chList[i])), sg.Input(default_text='', size = 8, justification = "left", key=("n%d" % chList[i])),
@ -77,6 +81,10 @@ for k in range(0, nMod):
layoutTabGroup[0].append(sg.Tab("Mod-%0d" % k, layoutTab[k])) layoutTabGroup[0].append(sg.Tab("Mod-%0d" % k, layoutTab[k]))
layout = [ layout = [
[
sg.Text("IP :", size = 27, justification = "right"),
sg.Input(IP, size = 16, justification = "right", readonly = True),
],
[ [
sg.Text("refresh period [sec] :", size = 27, justification = "right"), sg.Text("refresh period [sec] :", size = 27, justification = "right"),
sg.Input(updateTime, size = 8, justification = "right", enable_events=True, key=("-Refresh-")) sg.Input(updateTime, size = 8, justification = "right", enable_events=True, key=("-Refresh-"))
@ -123,7 +131,7 @@ while True:
if event[0:1] == 'c' : if event[0:1] == 'c' :
ID = event[1:] ID = event[1:]
iseg.SwitchOnHV(int(ID), int(window[event].get())) mpod.SwitchOnHV(int(ID), int(window[event].get()))
if event == '-Save-' : if event == '-Save-' :
fileName = values["Save As"] fileName = values["Save As"]
@ -144,8 +152,8 @@ while True:
window[("n%d" % chList[i])].update(row[0]) window[("n%d" % chList[i])].update(row[0])
window[("v%d" % chList[i])].update(row[2]) window[("v%d" % chList[i])].update(row[2])
window[("i%d" % chList[i])].update(row[3]) window[("i%d" % chList[i])].update(row[3])
iseg.SetHV(chList[i], float(row[2])) mpod.SetHV(chList[i], float(row[2]))
iseg.SetCurrent(chList[i], float(row[3])/1000) mpod.SetCurrent(chList[i], float(row[3])/1000)
i += 1 i += 1
if event in ["-VRateCombo-", "-VRateCh-", "-VRate-"]: if event in ["-VRateCombo-", "-VRateCh-", "-VRate-"]:
@ -153,33 +161,33 @@ while True:
ch = window["-VRateCh-"].get() ch = window["-VRateCh-"].get()
val = window["-VRate-"].get() val = window["-VRate-"].get()
if item == comboList[0]: if item == comboList[0]:
window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) window["-VRate-"].update("%.3f" % float(mpod.GetHVRiseRate(int(ch))))
if item == comboList[1]: if item == comboList[1]:
window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) window["-VRate-"].update("%.3f" % float(mpod.GetHVFallRate(int(ch))))
if item == comboList[2]: if item == comboList[2]:
iseg.SetHVRiseRate(ch, val) mpod.SetHVRiseRate(ch, val)
window["-VRate-"].update("%.3f" % float(iseg.GetHVRiseRate(int(ch)))) window["-VRate-"].update("%.3f" % float(mpod.GetHVRiseRate(int(ch))))
if item == comboList[3]: if item == comboList[3]:
iseg.SetHVFallRate(ch, val) mpod.SetHVFallRate(ch, val)
window["-VRate-"].update("%.3f" % float(iseg.GetHVFallRate(int(ch)))) window["-VRate-"].update("%.3f" % float(mpod.GetHVFallRate(int(ch))))
haha = event.find('_Enter') haha = event.find('_Enter')
if haha > 0 : if haha > 0 :
ID = event[:haha] ID = event[:haha]
ch = int(ID[1:]) ch = int(ID[1:])
if event[0:1] == 'v' : if event[0:1] == 'v' :
iseg.SetHV(ch, float(window[ID].get())) mpod.SetHV(ch, float(window[ID].get()))
window[ID].update("%.3f" % iseg.GetHV(ch)) window[ID].update("%.3f" % mpod.GetHV(ch))
if event[0:1] == 'i' : if event[0:1] == 'i' :
iseg.SetCurrent(ch, float(window[ID].get())/1000.) mpod.SetCurrent(ch, float(window[ID].get())/1000.)
window[ID].update("%.3f" % (iseg.GetCurrent(ch)*1000)) window[ID].update("%.3f" % (mpod.GetCurrent(ch)*1000))
if event == "_TIMEOUT_" : if event == "_TIMEOUT_" :
#hvList = GetAllHV() # get all V #hvList = GetAllHV() # get all V
#iList = GetAllCurrent() # get all current #iList = GetAllCurrent() # get all current
outVList = iseg.GetAllOutputHV() outVList = mpod.GetAllOutputHV()
outIList = iseg.GetAllLC() outIList = mpod.GetAllLC()
tempFile = open("temp.dat", "w") tempFile = open("temp.dat", "w")

View File

@ -3,60 +3,81 @@
import os import os
import subprocess import subprocess
#cmd0Str = "-v2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " class Mpod:
cmd0Str = "-v2c -Op .12 -m +WIENER-CRATE-MIB -c guru 128.186.111.101 " def __init__(self, ip):
self.IP = ip
#cmd0Str = "-v2c -m +WIENER-CRATE-MIB -c guru 128.186.111.101 "
self.cmd0Str = "-v2c -Op .12 -m +WIENER-CRATE-MIB -c guru %s " % self.IP
self.isConnected = False
print( "testing communication via " + self.IP)
cmdStr = "snmpwalk " + self.cmd0Str
try :
result = str(subprocess.check_output(cmdStr, shell=True))
print( result )
self.isConnected = True
except :
print("cannot establish communitation via " + self.IP)
def SendCmd(option,cmd): def SendCmd(self, option,cmd):
if (self.isConnected == False ) : return 0
if option == 0 : if option == 0 :
cmdStr = "snmpget " + cmd0Str + cmd cmdStr = "snmpget " + self.cmd0Str + cmd
elif option == 1: elif option == 1:
cmdStr = "snmpset " + cmd0Str + cmd cmdStr = "snmpset " + self.cmd0Str + cmd
elif option == 2: elif option == 2:
cmdStr = "snmpwalk " + cmd0Str + cmd cmdStr = "snmpwalk " + self.cmd0Str + cmd
else : else :
cmdStr = "echo option: 0 - get, 1 - set, 2 - walk" cmdStr = "echo option: 0 - get, 1 - set, 2 - walk"
#print(cmdStr) #print(cmdStr)
result = str(subprocess.check_output(cmdStr, shell=True)) result = str(subprocess.check_output(cmdStr, shell=True))
return result.lstrip('b\'').rstrip('\'').rstrip('\n') return result.lstrip('b\'').rstrip('\'').rstrip('\n')
#======== Get settings #======== Get settings
def GetHV(ch): def GetHV(self, ch):
haha = SendCmd(0, "outputVoltage.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputVoltage.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-3].strip()) return float(haha[aa+6:-3].strip())
def GetCurrent(ch): def GetCurrent(self, ch):
haha = SendCmd(0, "outputCurrent.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputCurrent.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-3].strip()) return float(haha[aa+6:-3].strip())
def GetOutputHV(ch): def GetOutputHV(self,ch):
haha = SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputMeasurementSenseVoltage.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-3].strip()) return float(haha[aa+6:-3].strip())
def GetLI(ch): def GetLI(self, ch):
haha = SendCmd(0, "outputMeasurementCurrent.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputMeasurementCurrent.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-3].strip()) return float(haha[aa+6:-3].strip())
def IsHVOn(ch): def IsHVOn(self, ch):
return SendCmd(0, "outputSwitch.u"+str(ch)) if (self.isConnected == False ) : return 0
return self.SendCmd(0, "outputSwitch.u"+str(ch))
def GetHVRiseRate(ch): def GetHVRiseRate(self, ch):
haha = SendCmd(0, "outputVoltageRiseRate.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputVoltageRiseRate.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-5].strip()) return float(haha[aa+6:-5].strip())
def GetHVFallRate(ch): def GetHVFallRate(self, ch):
haha = SendCmd(0, "outputVoltageFallRate.u" + str(ch)) if (self.isConnected == False ) : return 0
haha = self.SendCmd(0, "outputVoltageFallRate.u" + str(ch))
aa = haha.find('Float:') aa = haha.find('Float:')
return float(haha[aa+6:-5].strip()) return float(haha[aa+6:-5].strip())
#======== Get All channel Setting using snmp walk #======== Get All channel Setting using snmp walk
def GetChList(): def GetChList(self):
haha = SendCmd(2, "outputName") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputName")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputName.'): for k in haha.split('WIENER-CRATE-MIB::outputName.'):
if len(k) > 0 : if len(k) > 0 :
@ -65,8 +86,9 @@ def GetChList():
kaka.append(int(k)) kaka.append(int(k))
return kaka return kaka
def GetAllHV(): def GetAllHV(self):
haha = SendCmd(2, "outputVoltage") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputVoltage")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputVoltage'): for k in haha.split('WIENER-CRATE-MIB::outputVoltage'):
if len(k) > 0 : if len(k) > 0 :
@ -75,8 +97,9 @@ def GetAllHV():
kaka.append(float(k)) kaka.append(float(k))
return kaka return kaka
def GetAllCurrent(): def GetAllCurrent(self):
haha = SendCmd(2, "outputCurrent") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputCurrent")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputCurrent'): for k in haha.split('WIENER-CRATE-MIB::outputCurrent'):
if len(k) > 0 : if len(k) > 0 :
@ -85,8 +108,9 @@ def GetAllCurrent():
kaka.append(float(k)) kaka.append(float(k))
return kaka return kaka
def GetAllOutputHV(): def GetAllOutputHV(self):
haha = SendCmd(2, "outputMeasurementSenseVoltage") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputMeasurementSenseVoltage")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'): for k in haha.split('WIENER-CRATE-MIB::outputMeasurementSenseVoltage'):
if len(k) > 0 : if len(k) > 0 :
@ -95,8 +119,9 @@ def GetAllOutputHV():
kaka.append(float(k)) kaka.append(float(k))
return kaka return kaka
def GetAllLC(): def GetAllLC(self):
haha = SendCmd(2, "outputMeasurementCurrent") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputMeasurementCurrent")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'): for k in haha.split('WIENER-CRATE-MIB::outputMeasurementCurrent'):
if len(k) > 0 : if len(k) > 0 :
@ -105,8 +130,9 @@ def GetAllLC():
kaka.append(float(k)) kaka.append(float(k))
return kaka return kaka
def GetAllOnOff(): def GetAllOnOff(self):
haha = SendCmd(2, "outputSwitch") if (self.isConnected == False ) : return [0]
haha = self.SendCmd(2, "outputSwitch")
kaka = [] kaka = []
for k in haha.split('WIENER-CRATE-MIB::outputSwitch'): for k in haha.split('WIENER-CRATE-MIB::outputSwitch'):
if len(k) > 0 : if len(k) > 0 :
@ -115,41 +141,46 @@ def GetAllOnOff():
kaka.append(int(k)) kaka.append(int(k))
return kaka return kaka
#======== Set Settings #======== Set Settings
def SetHV(ch, val): def SetHV(self, ch, val):
if (self.isConnected == False ) : return 0
try : try :
int(ch) int(ch)
float(val) float(val)
return SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val)) return self.SendCmd(1, "outputVoltage.u" + str(ch) + " F " + str(val))
except: except:
print("either ch is not int or val is not float") print("either ch is not int or val is not float")
def SetCurrent(ch, val): def SetCurrent(self, ch, val):
if (self.isConnected == False ) : return 0
try : try :
int(ch) int(ch)
float(val) float(val)
return SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val)) return self.SendCmd(1, "outputCurrent.u" + str(ch) + " F " + str(val))
except: except:
print("either ch is not int or val is not float") print("either ch is not int or val is not float")
def SwitchOnHV(ch, onOff): def SwitchOnHV(self, ch, onOff):
if (self.isConnected == False ) : return 0
try : try :
int(ch) int(ch)
int(onOff) int(onOff)
SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(10)) self.SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(10))
return SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff)) return self.SendCmd(1, "outputSwitch.u" + str(ch) + " i " + str(onOff))
except : except :
print("either ch or onOff is not int") print("either ch or onOff is not int")
def SetHVRiseRate(ch, rate): def SetHVRiseRate(self, ch, rate):
if (self.isConnected == False ) : return 0
try : try :
int(ch) int(ch)
int(rate) int(rate)
return SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate)) return self.SendCmd(1, "outputVoltageRiseRate.u" + str(ch) + " F " + str(rate))
except: except:
print("either ch is not int or rate is not float") print("either ch is not int or rate is not float")
def SetHVFallRate(ch, rate): def SetHVFallRate(self, ch, rate):
if (self.isConnected == False ) : return 0
try : try :
int(ch) int(ch)
int(rate) int(rate)
@ -158,7 +189,6 @@ def SetHVFallRate(ch, rate):
print("either ch is not int or rate is not float") print("either ch is not int or rate is not float")
#===================== Auxliary function #===================== Auxliary function
def SplitChList(chList): def SplitChList(chList):
sep = list() sep = list()
for i in range(0, len(chList)): for i in range(0, len(chList)):
@ -176,8 +206,10 @@ def SplitChList(chList):
return newChList return newChList
#===================== SandBox #===================== SandBox
#print( GetOutputHV(0) ) #mpod = Mpod("128.186.111.101")
#print( GetLI(0) )
#print( mpod.GetOutputHV(0) )
#print( mpod.GetLI(0) )
#print( GetHVRiseRate(0) ) #print( GetHVRiseRate(0) )
#print( GetHVFallRate(0) ) #print( GetHVFallRate(0) )
# #
@ -188,6 +220,6 @@ def SplitChList(chList):
#hvList = GetAllOutputHV() #hvList = GetAllOutputHV()
#print(hvList) #print(hvList)
#chList = GetChList() #chList = mpod.GetChList()
#print( SplitChList(chList)) #print( SplitChList(chList))
#print( len(SplitChList(chList))) #print( len(SplitChList(chList)))