簡體   English   中英

如何在python腳本中調用此方法?

[英]How can I call this method within the python script?

我正在嘗試修改此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”方法? 就像我說的,我是python noob; 我在弄清楚如何使udp正常工作和集成方面取得了一些成功,但是我堅持這一點,我覺得這很簡單。 任何幫助將不勝感激。

我最近也想這樣做,並通過將python腳本的輸出傳遞到pronsole.py程序中來解決它。

因此,我沒有修改pronsole代碼,而是在另一個python程序中實現了我所有的套接字功能,並通過stdout將命令發送到了pronsole。 我的程序被稱為“ pronsole_interface”,因此在命令行中我將其命名為:

python pronsole_interface.py | pronsole.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM