2011-05-10 138 views
0

我正在开发一个项目,其中包括测试与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) 

问题是,当我试图用发送,接收和停止ohter模块命令这样做:

>>> 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没有发送属性” 我该如何解决这个问题?

感谢您的帮助!

+3

这不应该是'tn.send'和'tn.stop'吗? – 6502 2011-05-10 17:34:20

+0

是的,不好意思,但那是行不通的。我怎样才能使我的类ModTelnet在有效的Python语法? – user732663 2011-05-10 17:56:27

+0

请不要理会我关于语法的(删除的)注释。我正在看一个旧的Python版本。对不起泥泞的水域。 – slowdog 2011-05-10 18:29:22

回答

0

尝试

'send' in dir(tn) 

,如果是假,你还没有实现的发送方法。

+0

oki谢谢我明天会尝试,我没有在家陪伴我! – user732663 2011-05-10 20:15:59

0

问题是我的类定义的语法:

class ModTelnet: 

而不是:

class ModTelnet(): 

至极是无用的,因为我没有从其他类继承...:d

无论如何,谢谢!