我正在开发一个项目,该项目包括使用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没有发送属性” 我该如何解决这个问题?
谢谢你的帮助!
答案 0 :(得分:0)
尝试
'send' in dir(tn)
如果它为False,则表示尚未实现send方法。
答案 1 :(得分:0)
问题是我的类定义的语法:
class ModTelnet:
而不是:
class ModTelnet():
这是无用的,因为我没有继承其他班级......:D
非常感谢!