使用的Class实例抱怨没有属性

时间:2019-04-25 14:38:44

标签: python python-2.7

我对Python来说还很陌生,并且受命完成最近退出的某人的代码,并且出现以下错误:

  

Vaisala实例没有属性连接错误。

代码很长,但是我在这里包括了我认为最重要的部分。

BackTrace: 第1656行,位于read_instrument self.conn.sendall('CRH \ r \ n')

class Vaisala:

    def __init__(self, ip='192.168.5.49', port=23):

        self.l = re.compile(r"(\w+)=\s*(-?\d+.?\d+)\s([\'\\\/\%0-9a-zA-Z]+)")

        self.ip = ip
        self.port = int(port)

        self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.conn.connect((self.ip, self.port))

        # Retrieve Intrument Information
        self.conn.sendall('?\r\n')
        time.sleep(0.5)
        resp = self.conn.recv(1024)

        info = resp.split('\r\n')

        self.meta_data = {
            'model':info[3].split('/')[0].replace(' ',''),
            'firmware':info[3].split('/')[1].replace(' ',''),
            'serial_number': info[4].split(':')[1].replace(' ',''),
            'batch_number':info[5].split(':')[1].replace(' ',''),
            'cal_date':info[6].split(':')[1].replace(' ',''),
            'cal_location':info[7].split(':')[1].replace(' ',''),
            'pressure':info[14].split(':')[1].strip(),
        }

        print "\nVaisala: {0}".format(self.meta_data['model'])
        print "Serial Number: {0}".format(self.meta_data['serial_number'])
        print "Calibration Date: {0}".format(self.meta_data['cal_date'])
        print "Calibration Location: {0}\n".format(self.meta_data['cal_location'])

        self.cal_mode = False
        self.read()


    def ready_instrument(self, setpoints=[]):

        # Number of sample to determine if stable
        samples = 30

        if not self.cal_mode:

            self.conn.sendall('CRH\r\n')
            time.sleep(0.1)
            resp = self.conn.recv(1024)

        # Parse Response
        s = re.findall(r'\d+.?\d+', resp)

        stable = False
        start_time = time.time()

        while not stable:

            data =[]

            for i in range(samples):

                self.conn.sendall('c\r\n')
                time.sleep(1)

                resp = self.conn.recv(1024)

                data.append(float(re.findall(r'\d+.?\d+', resp)[0]))

            if stdev(data) <0.2:
                stable=True

            if time.time() - start_time > 3600:
                print "\nFailed to stabilize\n"
                raise

        self.ready_for_setpoint= True

0 个答案:

没有答案