0

我是 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'"

谁能帮我找出这里出了什么问题。

谢谢, 维普尔

4

1 回答 1

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

那么你可以做self.sSerial.write("~~~~~~~~~~\r")

  1. 类 Com 丢失__init__
于 2013-10-14T13:05:57.897 回答