命令未在Python中定义-真正的基础知识,但很混乱!
我已经写了这个简短的脚本(为了大小我去掉了一些小细节),我得到了一个非常简单的错误,但我不明白为什么!我对Python非常陌生,所以也许有人可以解释这个问题以及为什么它不起作用 当我希望将完整的自定义串行写入字符串打印回控制台时,错误似乎出现了,它似乎无法识别我发送给函数的参数 也许我误解了一些非常简单的事情。对任何人来说都应该是简单的,即使对Python只有一点点了解 干杯 守则:命令未在Python中定义-真正的基础知识,但很混乱!,python,eclipse,pygtk,pydev,glade,Python,Eclipse,Pygtk,Pydev,Glade,我已经写了这个简短的脚本(为了大小我去掉了一些小细节),我得到了一个非常简单的错误,但我不明白为什么!我对Python非常陌生,所以也许有人可以解释这个问题以及为什么它不起作用 当我希望将完整的自定义串行写入字符串打印回控制台时,错误似乎出现了,它似乎无法识别我发送给函数的参数 也许我误解了一些非常简单的事情。对任何人来说都应该是简单的,即使对Python只有一点点了解 干杯 守则: #! /usr/bin/env python # IMPORTS APPEAR HERE *** ser =
#! /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和Python/PyGTK编写一个小型GUI应用程序,使用PySerial模块通过串行方式控制步进电机。但是,我想打包我自己的“write”函数,这样我就可以将默认值附加到电缆下面的“send”中。例如,电机编号和始终在指令末尾追加返回值。在同一个函数中直接读回响应等其他事情也有助于评估响应,因此,将其封装到自定义函数中似乎是明智之举
如有任何上述建议或帮助,将不胜感激
谢谢你
安迪
更新:我已经解决了不包括“self”的原始问题,并且我已经设法让Stack接受我通常使用的选项卡,以便它看起来更干净。我还想指出,我删除的唯一代码是简单的变量设置。然而,问题依然存在 可能是因为你错过了自我论证:
def send_command(self, commands):
def send_命令(命令)中出现缩进错误: 第一个参数应该是“self”:
LAPP类(对象):
def发送命令(self,commands):
顺序写入(“#”+电机号+命令+”\r\n')
首先,缩进应该使用多个空格。在Python中,空格非常重要,如果只使用一个空格,很难看出是否正确。四是通常可以接受的金额
send_命令
方法的主要问题是您忘记了Python中任何方法的第一个参数是(按惯例)self
。因此,签名应为:
def send_command(self, commands):
但是,您显示的代码不会给出您所声明的错误:而是给出以下内容:
TypeError: send_command() takes exactly 1 argument (2 given)
此外,在您的方法中,未定义的不是
命令
,而是电机号
。这就是为什么显示您正在运行的实际代码总是很重要的原因,减少到足以实际再现错误的程度 你的题目有点误导人;不是没有定义命令,而是在send\u command
方法中没有定义变量commands
。我不知道为什么会发生这种情况。我会深入调查的。你有没有试过用另一个名字作为论点?也许用foo
替换它,看看它是否仍在运行。更新代码后,实际的错误是什么?是找不到纳米科技
还是马达
?奇怪的是,我的代码中使用的标签有点奇怪,一旦我真的重新键入了它,它就工作正常了!一定是间距问题。除了Eclipse,你能推荐一个好的Python IDE吗?Eclipse显然不好?!我正在使用vim/gvim或textmate(在macos上)。您会在这个网站上找到一些关于python ide的问题:)
TypeError: send_command() takes exactly 1 argument (2 given)