私はこの短いスクリプトを書きました (サイズの細かい部分は省略しました)。非常に単純なエラーが表示されますが、理由がわかりません! 私はPythonに非常に慣れていないので、誰かが問題とそれが機能しない理由を説明できますか?
完全なカスタムシリアル書き込み文字列をコンソールに出力したいときにエラーが発生するようですが、関数に送信した Args を認識していないようです。
おそらく、私は非常に単純なことを誤解しています。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 モジュールを使用してシリアル経由でステッピング モーターを制御しています。ただし、ケーブルの「送信」にデフォルト値を追加できるように、独自の「書き込み」機能をパッケージ化したいと考えています。たとえば、モーター番号と、命令の最後に常にリターンを追加します。同じ関数で応答をすぐに読み取るなどの他のことも、応答を測定するのに役立ちます。そのため、それをカスタム関数にラップすることは賢明なことのように思えました。
上記に関するアドバイスやヘルプをいただければ幸いです。
よろしくお願いします。
アンディ
更新:「自己」を含めないという元の問題に対処し、スタックが通常使用するタブを受け入れるようにすることができたので、見やすくなりました。また、削除した唯一のコードは単純な変数設定であることに注意してください。ただし、問題は解決しません。