Как вызвать метод из наследуемого класса - PullRequest
0 голосов
/ 10 мая 2011

Я работаю над проектом, который состоит в тестировании соединения платы с разъемом JTAG и сервером OpenOCD.

Вот класс соединения, который я кодировал, он просто использует pexpect:

"""
Communication with embedded board
"""
import sys 
import time
import threading
import Queue

import pexpect
import serial
import fdpexpect

from pexpect import EOF, TIMEOUT


class ModTelnet():
    def __init__(self):    

        self.is_running = False
        self.HOST = 'localhost'
        self.port = '4444'

    def receive(self):
        #receive data (= msg) from telnet stdout
        data = [ EOF, TIMEOUT, '>' ]
        index = self._tn.expect(data, 2)
        if index == 0:
            return 'eof', None
        elif index == 1:
            return 'timeout', None
        elif index == 2:
            print 'success', self._tn.before.split('\r\n')[1:]
            return 'success',self._tn.before

    def send(self, command):
        print 'sending command: ', command
        self._tn.sendline(command)


    def stop(self):
        print 'Connection stopped !'
        self._ocd.sendcontrol('c')

    def connect(self):
        #connect to MODIMX27 with JTAG and OpenOCD
        self.is_running = True
        password = 'xxxx'
        myfile = 'openocd.cfg'
        self._ocd = pexpect.spawn('sudo openocd -f %s' % (myfile))
        i = self._ocd.expect(['password', EOF, TIMEOUT])
        if i == 0:
            self._ocd.sendline(password)
            time.sleep(1.0)
            self._connect_to_tn()
        elif i == 1:
            print ' *** OCD Connection failed *** '
            raise Disconnected()
        elif i == 2:
            print ' *** OCD Connection timeout *** '
            raise Timeout()  

    def _connect_to_tn(self):
         #connect to telnet session @ localhost port 4444
         self._tn = pexpect.spawn('telnet %s %s' % (self.HOST, self.port))
         condition = self._tn.expect(['>', EOF, TIMEOUT])
         if condition == 0:
            print 'Telnet opened with success'
         elif condition == 1:
            print self._tn.before
            raise Disconnected()
         elif condition == 2: 
            print self._tn.before
            raise Timeout()

if __name__ =='__main__':
    try:
        tn = ModTelnet()
        tn.connect()   
    except :
            print 'Cannot connect to board!'
            exit(0)

Проблема в том, что я пытаюсь использовать команду send, receive и stop в других модулях, выполняя это:

    >>> from OCDConnect import *
>>> import time

>>> tn = ModTelnet()
>>> tn.connect()
Telnet opened with success

>>> time.sleep(2.0)
>>> self.send('soft_reset_halt')
MMU: disabled, D-Cache: disabled, I-Cache: disabled

>>> self.stop()

Это выдает ошибку вроде: "У ModTelnet нет атрибута отправки" Как я могу это исправить ??

Спасибо за помощь!

Ответы [ 2 ]

0 голосов
/ 12 мая 2011

Проблема заключалась в синтаксисе определения моего класса:

class ModTelnet:

А не:

class ModTelnet():

, который бесполезен, потому что я не наследую от другого класса ...: D

Все равно спасибо!

0 голосов
/ 10 мая 2011

попробуй

'send' in dir(tn)

если это False, вы не реализовали метод send.

...