views:

80

answers:

3

Hi,

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!

+2  A: 

It could be because you're missing the self argument:

  def send_command(self, commands):
Ryan Ginstrom
+1  A: 

you've got an indentation error in def send_command(commands):

and your first parameter should be "self" :

class ArialApp(object):

<snap>

    def send_command(self, commands):
        ser.write("#" + motor_no + commands + '\r\n')
dzen
Odd, turns out there was something strange with the tabbing used in my code, once I literally re-typed it, its working fine! Must have been a spacing issue. Can you suggest a good Python IDE other than Eclipse, which clearly isn't good?!
Andy Barlow
I'm using vim / gvim or textmate (on macos). you'll find some asking about python ide on this website :)
dzen
+1  A: 

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:

def send_command(self, commands):

However, the code you have shown would not give the error you state: it would instead give this:

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

In addition, in your method it's not commands which is not defined, but motor_no. This is why it's always important to show the actual code you're running, cut down enough to actually reproduce the error.

Daniel Roseman