我已经写了这个简短的脚本(我已经剥去了一些小的细节大小),我得到一个非常简单的错误,但是,我不明白为什么!我对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模块来控制串行电机的串行。不过,我想打包自己的“写入”功能,以便将默认值附加到电缆上的“发送”。例如,电机编号和总是在指令结尾附加返回。其他的东西,比如直接在同一个函数中读回响应,对于评估响应也是很有用的,所以把它包装到一个自定义函数中似乎是明智之举。
任何意见或上述帮助将不胜感激。
谢谢你亲切。
安迪
更新:我有地址不包括“自我”原来的问题,我已经成功地得到堆栈接受我通常使用这样的清洁剂来看看标签。也想注意我唯一删除的代码是简单的变量设置。但是,问题依然存在!
您的主题标题有点误导;这不是一个命令没有定义,但是变量'commands'没有被定义,在'send_command'方法中。我不确定为什么会发生这种情况。我会更深入地研究它。你有没有尝试使用不同的名称作为参数?也许用'foo'代替它,看它是否仍然运行。 – 2010-06-30 09:06:31
现在您更新了代码,* actual *错误是什么?它没有找到'nanotech'或'motor_no'吗? – 2010-06-30 09:24:27