2010-06-30 15 views
2

我已经写了这个简短的脚本(我已经剥去了一些小的细节大小),我得到一个非常简单的错误,但是,我不明白为什么!我对Python很陌生,所以也许有人可以解释这个问题以及它为什么不起作用?未在Python中定义的命令 - 真实的基础,但困惑!

当我希望将完整的自定义串行写入字符串打印回控制台时,错误似乎下降,但它似乎无法识别我发送给函数的参数。

也许我误解了很简单的东西。应该简单的人,甚至与Python的最小的理解

干杯

验证码:

#! /usr/bin/env python 

# IMPORTS APPEAR HERE *** 

ser = serial.Serial(
    port='/dev/ttyUSB0', 
    baudrate=115200, 
    parity='N', 
    stopbits=1, 
    bytesize=8 
) 

# Sets motor number 
motor_no = "2" 

# Lets create our main GUI class 
class ArialApp(object): 
    # Default init stuff 
    def __init__(self): 
     # Create a builder object and create the objects from the .glade file 
     self.builder = gtk.Builder() 
     self.builder.add_from_file("../res/main.glade") 
     self.builder.connect_signals(self) 

     # Open the serial connection to the encoder and keep it open 
     ser.open() 

     # Custom function for sending commands down the serial. Needed to wrap defaults 
     # arround the custom 'serial.write' command. 
     self.send_command('A') 

     # Code removed for space..... 

    # Custom method for sending commands down serial with default ammendments 
    def send_command(self, nanotech): 
     # Send the command with the #, then motor number which should be global, then the command 
     # sent the the method followed by a return 
     ser.write("#" + motor_no + nanotech + '\r\n') 

     # Print to the console the full command sent down the pipe 
     # [[[ ERROR GOES HERE ]]] 
     print "#" + motor_no + nanotech + '\r\n' 

# Just to show its in here... 
if __name__ == "__main__": 
    app = ArialApp() 
    gtk.main() 

错误:

File "main.py", line 62, in ArialApp 
    print "#" + motor_no + commands + '\r\n' 
NameError: name 'commands' is not defined 

最后,刚刚洒在某些情况下情况:

我在Glade和Pytho上写了一个小的GUI应用程序n/PyGTK使用PySerial模块来控制串行电机的串行。不过,我想打包自己的“写入”功能,以便将默认值附加到电缆上的“发送”。例如,电机编号和总是在指令结尾附加返回。其他的东西,比如直接在同一个函数中读回响应,对于评估响应也是很有用的,所以把它包装到一个自定义函数中似乎是明智之举。

任何意见或上述帮助将不胜感激。

谢谢你亲切。

安迪

更新:我有地址不包括“自我”原来的问题,我已经成功地得到堆栈接受我通常使用这样的清洁剂来看看标签。也想注意我唯一删除的代码是简单的变量设置。但是,问题依然存在!

+0

您的主题标题有点误导;这不是一个命令没有定义,但是变量'commands'没有被定义,在'send_command'方法中。我不确定为什么会发生这种情况。我会更深入地研究它。你有没有尝试使用不同的名称作为参数?也许用'foo'代替它,看它是否仍然运行。 – 2010-06-30 09:06:31

+0

现在您更新了代码,* actual *错误是什么?它没有找到'nanotech'或'motor_no'吗? – 2010-06-30 09:24:27

回答

2

这可能是因为你缺少自我的说法:

def send_command(self, commands): 
1

您在高清send_command(命令)有一个压痕错误:

,你的第一个参数应该是“自我” :

class ArialApp(object): 

<snap> 

    def send_command(self, commands): 
     ser.write("#" + motor_no + commands + '\r\n') 
+0

奇怪的是,我的代码中使用的Tab键有一些奇怪的地方,一旦我从字面上重新输入它,它的工作正常!一定是间距问题。你能否推荐一个比Eclipse更好的Python IDE,这显然不好?! – Schodemeiss 2010-06-30 09:21:39

+0

我使用vim/gvim或textmate(在macos上)。你会发现一些关于这个网站上的Python IDE的问题:) – dzen 2010-06-30 22:02:36

1

首先,您应该使用多于一个空格进行缩进。 Python中的空白是很重要的,如果你只使用一个空格,很难发现你已经做对了。四是通常可接受的数额。

您的send_command方法的主要问题是您忘记了Python中的任何方法的第一个参数是(通过约定)self。所以它的签名应该是:

def send_command(self, commands): 

但是,你给不给的错误,你的状态代码:那就不是给这个:

TypeError: send_command() takes exactly 1 argument (2 given) 

另外,在你的方法是不是commands这是没有定义的,但是motor_no。这就是为什么总是显示您正在运行的代码实际总是很重要,减少到足以实际重现错误。