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 和 Python / PyGTK 编写一个小型 GUI 应用程序,以通过串行方式控制步进电机使用 PySerial 模块。但是,我想打包我自己的“写入”功能,以便我可以将默认值附加到电缆上的“发送”。例如,电机编号并始终在指令末尾附加回车符。其他诸如在同一函数中立即读回响应之类的事情对于衡量响应也很有用,因此,将其包装到自定义函数中似乎是明智的做法。
任何有关上述问题的建议或帮助将不胜感激。
非常感谢。
安迪
更新:我已经解决了不包括“自我”的原始问题,并且我已经设法让 Stack 接受我通常使用的选项卡,因此看起来更干净。还要注意我删除的唯一代码是简单的变量设置。然而,问题依然存在!
I have written this short script (which I've stripped away some minor detail for size) and I'm getting a very simple error, yet, I don't understand why! I'm very new to Python, so maybe someone can explain the issue and why it's not working?
The error seems to fall when I wish to print the full custom serial write string back to the console, it doesn't seem to recognise the Args I sent to the function.
Perhaps I have misunderstood something very simple. Should be simple for anyone even with the tiniest of Python understanding
Cheers
The Code:
#! /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()
The error:
File "main.py", line 62, in ArialApp
print "#" + motor_no + commands + '\r\n'
NameError: name 'commands' is not defined
Finally, just to shed some context on the situation:
I am writing a small GUI app in Glade and Python / PyGTK to control a stepper motor over serial using the PySerial module. However, I would like to package up my own "write" function so I can append default values to the 'send' down the cable. For example, the motor number and always appending returns on the end of the instructions. Other things like reading back the response straight away in the same function would be useful to gauge responses too, so, wrapping it up into a custom function seemed like the sensible thing to do.
Any advice or help on the above would be appreciated.
Thank-you kindly.
Andy
UPDATE: I have addresses the original issue of not including "self" and I've managed to get Stack to accept the tabs I normally use so its cleaner to look at. Also wanted to note the only code I removed was simple variable setting. However, the issue persists!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是因为您缺少 self 参数:
It could be because you're missing the self argument:
def send_command(commands): 中出现缩进错误,
并且您的第一个参数应该是“self”:
you've got an indentation error in def send_command(commands):
and your first parameter should be "self" :
首先,您应该使用多个空格进行缩进。空格在 Python 中很重要,如果只使用一个空格,则很难看出您是否正确。四是通常接受的金额。
send_command
方法的主要问题是您忘记了 Python 中任何方法的第一个参数(按照惯例)是self
。所以签名应该是:但是,您显示的代码不会给出您声明的错误:它会给出这样的错误:
此外,在您的方法中,它不是未定义的
commands
,而是 <代码>motor_no。这就是为什么显示您正在运行的实际代码始终很重要,并减少足够的代码以实际重现错误。Firstly, you should use more than a single space for indentation. White space is significant in Python, and it's very hard to see that you've got it right if you're only using one space. Four is the usually accepted amount.
The main issue with your
send_command
method is that you've forgotten that the first argument to any method in Python is (by convention)self
. So the signature should be:However, the code you have shown would not give the error you state: it would instead give this:
In addition, in your method it's not
commands
which is not defined, butmotor_no
. This is why it's always important to show the actual code you're running, cut down enough to actually reproduce the error.