Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:10:20+00:00 2026-05-15T13:10:20+00:00

I have written this short script (which I’ve stripped away some minor detail for

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:10:21+00:00Added an answer on May 15, 2026 at 1:10 pm

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

      def send_command(self, commands):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
So I am obviously not a very good programmer. I have written this small
related (sort of) to this question. I have written a script that will loop
I have written a short python script that opens Google music in web view
I have written this short function to protect against my_sql injection, because of its
I have written this clone method for when the parent of the Employee class
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have written this code in javascript however I have to make it work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.