我正在尝试修改此python应用程序{ - https://github.com/kliment/Printrun/blob/master/pronsole.py 用于控制3D打印机。基本上我正在尝试使用该应用程序的命令行版本并添加udp接收,所以我可以从我制作的iPhone应用程序控制它。我有python应用程序接收udp罚款,解密收到的不同消息等。 这个python脚本中有一个类,它定义了所有使用的方法。这是一个非常精简的版本,方法给我带来了问题 -
class pronsole(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
if not READLINE:
self.completekey = None
self.p = printcore.printcore()
self.p.recvcb = self.recvcb
self.recvlisteners = []
self.prompt = "PC>"
self.p.onlinecb = self.online
self.f = None
...
def do_connect(self, l):
a = l.split()
p = self.scanserial()
port = self.settings.port
if (port == "" or port not in p) and len(p)>0:
port = p[0]
baud = self.settings.baudrate or 115200
if(len(a)>0):
port = a[0]
if(len(a)>1):
try:
baud = int(a[1])
except:
print "Bad baud value '"+a[1]+"' ignored"
if len(p) == 0 and not port:
print "No serial ports detected - please specify a port"
return
if len(a) == 0:
print "No port specified - connecting to %s at %dbps" % (port, baud)
if port != self.settings.port:
self.settings.port = port
self.save_in_rc("set port", "set port %s" % port)
if baud != self.settings.baudrate:
self.settings.baudrate = baud
self.save_in_rc("set baudrate", "set baudrate %d" % baud)
self.p.connect(port, baud)
类中还有另一个方法叫做'do_move'来移动打印机,我在收到udp时调用该方法。我想我正确地称之为 -
a = pronsole()
a.do_move("X 29")
它试图调用它,但由于我尚未连接到打印机而无法执行此操作。 所以我试着打电话 -
a = pronsole()
a.do_connect("")
在课程结束时,但是我收到错误消息 - “设置端口保存失败:pronsole实例没有属性'rc_filename'”
尝试使用'rc_filename'的方法是 -
def save_in_rc(self, key, definition):
"""
Saves or updates macro or other definitions in .pronsolerc
key is prefix that determines what is being defined/updated (e.g. 'macro foo')
definition is the full definition (that is written to file). (e.g. 'macro foo move x 10')
Set key as empty string to just add (and not overwrite)
Set definition as empty string to remove it from .pronsolerc
To delete line from .pronsolerc, set key as the line contents, and definition as empty string
Only first definition with given key is overwritten.
Updates are made in the same file position.
Additions are made to the end of the file.
"""
rci, rco = None, None
if definition != "" and not definition.endswith("\n"):
definition += "\n"
try:
written = False
if os.path.exists(self.rc_filename):
import shutil
shutil.copy(self.rc_filename, self.rc_filename+"~bak")
rci = codecs.open(self.rc_filename+"~bak", "r", "utf-8")
rco = codecs.open(self.rc_filename, "w", "utf-8")
if rci is not None:
overwriting = False
for rc_cmd in rci:
l = rc_cmd.rstrip()
ls = l.lstrip()
ws = l[:len(l)-len(ls)] # just leading whitespace
if overwriting and len(ws) == 0:
overwriting = False
if not written and key != "" and rc_cmd.startswith(key) and (rc_cmd+"\n")[len(key)].isspace():
overwriting = True
written = True
rco.write(definition)
if not overwriting:
rco.write(rc_cmd)
if not rc_cmd.endswith("\n"): rco.write("\n")
if not written:
rco.write(definition)
if rci is not None:
rci.close()
rco.close()
#if definition != "":
# print "Saved '"+key+"' to '"+self.rc_filename+"'"
#else:
# print "Removed '"+key+"' from '"+self.rc_filename+"'"
except Exception, e:
print "Saving failed for", key+":", str(e)
finally:
del rci, rco
我已经尝试通过命令行调用do_connect方法,这样可行,所以我无法理解为什么我不能以同样的方式在python脚本中调用它。我猜这与我在做这个时引用一个pronsole实例的事实有关 -
a = pronsole()
a.do_connect("")
我尝试将其设为静态方法,但这会产生其他问题。 所以我的问题是,我怎么能用python脚本调用这个'do_connect'方法? 就像我说的,我是一个蟒蛇菜鸟;我有一些很好的成功,弄清楚如何让udp工作和集成,但我坚持这一件事,我有一种感觉非常简单。 任何帮助将不胜感激。
答案 0 :(得分:1)
我最近也想这样做,并通过将python脚本的输出传递到pronsole.py程序来解决它。
因此,我没有修改pronsole代码,而是在另一个python程序中实现了我的所有套接字功能,并通过stdout将命令发送到pronsole。我的程序被称为'pronsole_interface',所以从命令行我这样调用它:
python pronsole_interface.py | pronsole.py