views:

87

answers:

1

Hello my StackOverflow friends. I have been doing wxPython tutorials and reading the documentation, so far I like it. I wanted to make a simple app first. What this app would do is send a command to a micro controller to turn a relay on or off.

I have a global variable to get the COM port to be used (Instead of hardcoding COM1 in for instance)

I can't get that variable to be correctly updated. Here is the code:

#!/usr/bin/env python

import wx
from firmata import *

# Arduino(port, baudrate=115200)
#arduino = Arduino(comListenPort)
#arduino.pin_mode(7, firmata.OUTPUT)
comListenPort = 'Is not set'
getComPort = 'Not Set'

class ArduinoDemo(wx.Frame):
    def __init__(self, parent, id):
        # Main window creation
        wx.Frame.__init__(self, parent, id, 'Demonstration of Firmata', \
                          size = (300, 200))

        # Content
        mainPanel = wx.Panel(self)

        # Open Contact button creation
        relayOpen = wx.Button(mainPanel, label = 'Open Contact', \
                              pos = (25, 25), size = (120, 60))

        # Close Contact button creation
        relayClosed = wx.Button(mainPanel, label = 'Close Contact', \
                                pos = (150, 25), size = (120, 60))

        # Binds click event from relayOpen to openRelay
        self.Bind(wx.EVT_BUTTON, self.closeRelay, relayClosed)
        # Binds click event from relayClose to closeRelay
        self.Bind(wx.EVT_BUTTON, self.openRelay, relayOpen)

        # Get correct COM port
        getComPort = wx.TextEntryDialog(None, 'Enter COM Port', 'COM Port', '')
        if getComPort.ShowModal() == wx.ID_OK:
            comListenPort = getComPort.GetValue()
            # # Debug
            print getComPort.GetValue()
            print comListenPort
            # # /Debug


    def openRelay(self, event):
        #arduino.digital_write(7, firmata.HIGH)
        # # Debug
        print comListenPort # does not print correctly
        # # /Debug

    def closeRelay(self, event):
        #arduino.digital_write(7, firmata.LOW)
        # # Debug
        print getComPort # does not print correctly
        # # /Debug


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = ArduinoDemo(parent = None, id = -1)
    frame.Show()
    app.MainLoop()

Now I assume that having a global variable for this would the best way to do it, but I am completely looking for suggestions and pointers in everything. To the point though, comListenPort does not get assigned the value of my TextEntryDialog box. I know this has to be the dumbest thing I am overlooking.

The two debug print statements getComPort.GetValue() and comListenPort both print the correct data. When I click either the relayOpen or relayClosed button, they say 'is not set' or 'not set' still. I hope someone can slap some sense into me, I am pretty dumbfounded (emphasis on DUMB)

Thanks again

+1  A: 

I think that inside __init__ Python sees comListenPort as a local variable, not global.. You should declare it global before using it:

global comListenPort
comListenPort = getComPort.GetValue()

Alternatively, you could store the value as an instance variable of ArduinoDemo:

self.comListenPort = getComPort.GetValue()
Joril
Thanks, that worked. So would it be better to do this in __init__ or in ArduinoDemo?
Dan
Do you mean the "global" statement? You should use it inside every method or function that uses comListenPort, so that would be inside __init__. Here's the language reference: http://docs.python.org/reference/simple_stmts.html#the-global-statement
Joril