我正在尝试修改这个 用于控制 3D 打印机的python 应用程序{- https://github.com/kliment/Printrun/blob/master/pronsole.py 。基本上我正在尝试获取应用程序的命令行版本并添加 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 工作和集成方面取得了一些成功,但我一直坚持这一点,我觉得这真的很简单。任何帮助将不胜感激。