我有一个简单的类我建模代表一个电源,我将通过telnet会话远程连接到该电源。该类有多种方法,包括连接电源,打开/关闭电源,以及调整其电压。我试图添加一种方法,只要它打开并建立连接,我就可以收集输出电压数据。作为尝试,我实施了GetAllData
方法。该方法使用while循环,其中条件是运行while循环,直到电源的状态设置为on。我希望能够在通过TurnOff()
方法关闭电源后突破方法/ while循环。不幸的是,一旦while循环盯着它,它就不可能爆发。任何人都可以推荐一种更好的方法来实现我想要做的事情吗?这是我的课程:
import os
import telnetlib
class PowerSupply():
# ---------------------------------- #
def __init__(self,port_no,my_name):
self.Status = "Off"
self.Port = port_no
self.Name = my_name
self.tn = None
self.HOST = "192"
# ---------------------------------- #
def TurnOn(self):
onCommand = "OUT 1\r"
if self.Status == "ON":
print "I'm already on"
else:
self.tn = telnetlib.Telnet(self.HOST,self.Port)
self.tn.write(onCommand)
self.Status = "ON"
print "I am now on"
# ---------------------------------- #
def TurnOff(self):
offCommand = "OUT 0\r"
self.tn.write(offCommand)
self.tn.close()
print "I am now off"
# ---------------------------------- #
def SetVoltage(self,volts):
voltageCommand = "PV" + " " + str(volts) + "\r"
self.tn.write(voltageCommand)
# ---------------------------------- #
def GetAllData(self):
while(self.Status == "ON"):
self.tn.write("DVC?\r")
all_data = self.tn.read_some()
vdc = all_data.split(',')
vdc = vdc[0]
print vdc