AttributeError:<class>实例没有属性&lt;&gt; </class>

时间:2013-10-14 13:01:18

标签: python pyserial

我是python的新手,所以需要你的帮助以下错误信息。我有两个文件,第一个是“test1.py”,我正在运行并具有以下代码。

import sys, time, re, os, pickle

from ComLib import *

obj = Com()
obj.ComOpen()
obj.ComReset()
obj.ComClose()

,第二个文件是“ComLib.py”并具有以下代码

import serial, sys, re, pickle, time


class Com:
    def ComOpen(self):
        self = serial.Serial()
        self.port = "COM1"
        self.baudrate = 9600
        self.bytesize = serial.EIGHTBITS     #number of bits per bytes
        self.parity = serial.PARITY_NONE     #set parity check: no parity
        self.stopbits = serial.STOPBITS_ONE  #number of stop bits
        self.timeout = 1                     #non-block read
        self.xonxoff = True                  #disable software flow control
        self.rtscts = False                  #disable hardware (RTS/CTS) flow control
        self.dsrdtr = False                  #disable hardware (DSR/DTR) flow control
        self.writeTimeout = 2                #timeout for write

        self.open()
        return


    def ComClose(self):
        self.close()
        return

    def ComReset(self):
       print "Executing ComReset function...!!"

       self.write("~~~~~~~~~~\r")

       i = 0
       while i<10 :
          response = self.readline()
          print "Inside first while loop...!!"
          print "response = "+response

          if (response == ':'):
             print "-->colon found...ready for next input<---"
             break

          i=i+1
          time.sleep(0.5)

       return

执行上述操作时,我收到以下错误

"Traceback (most recent call last):
  File "C:\Users\vgupta\Desktop\KeyAT\final\WDEAutomationTestSuite\WDETestSuite\Bootguard\TC#001.py", line 17, in <modul
e>
    obj.ComReset()
  File "C:\Users\vgupta\Desktop\KeyAT\final\WDEAutomationTestSuite\APILib\ComLib.py", line 52, in ComReset
    self.write("~~~~~~~~~~\r")
AttributeError: Com instance has no attribute 'write'"

任何人都可以帮助我找出问题所在。

谢谢, VIPUL

1 个答案:

答案 0 :(得分:2)

  1. 你的声音应该是:
  2. self.sSerial = serial.Serial() 
    self.sSerial.port = "COM1"
    self.sSerial.baudrate = 9600
    .........
    

    然后你可以self.sSerial.write("~~~~~~~~~~\r")

    1. class Com缺少__init__