Hi! :)

I have been starting this small GUI application on monitoring data sent via the serial port... I have made the necessary preparations, please see:

import  wx
import time
import wx.gizmos as gizmos
import datetime
       
class MACE(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, None, -1, "MACE PQ MONITORING SYSTEM", wx.DefaultPosition, wx.Size(600, 500),style=wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|
                  wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)


        menubar = wx.MenuBar()
        self.statusbar =self.CreateStatusBar()
        file = wx.Menu()
        help = wx.Menu()

        menubar.Append(file, "File")
        menubar.Append(help, "Help")
        self.SetMenuBar(menubar)


        #FOR REAL TIME MONITOR#

        array= ['12','40','20','221', '222', '223','3.1','3.2','3.3'] #values should be dynamic, basing on string sent via the serial port
        
        self.panelA = wx.Window(self, -1, style=wx.SIMPLE_BORDER)
        self.panelA.SetBackgroundColour(wx.WHITE)

        self.SetAutoLayout(True)

        style = gizmos.LED_ALIGN_CENTER
        self.led = gizmos.LEDNumberCtrl(self, -1, (100, 40), (150,35), style)
        self.OnTimer(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)        
        
        powlabel = wx.StaticText(self.panelA, -1, "Real Power (Watts)", pos=(60,102))
        self.powdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 90), (125,35), style)
        self.powdisp.SetValue(array[1])

        imlabel = wx.StaticText(self.panelA, -1, "Imaginary Power (VAR)", pos=(60,154))
        self.imdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 142), (125,35), style)
        self.imdisp.SetValue(array[2])
        
        volabel = wx.StaticText(self.panelA, -1, "Voltages (Volts)", pos=(40,200))
        curlabel = wx.StaticText(self.panelA, -1, "Currents (Amps)", pos=(220,200))

        p1 = wx.StaticText(self.panelA, -1, "Phase 1", pos=(155,232))
        p2 = wx.StaticText(self.panelA, -1, "Phase 2", pos=(155,282))
        p3 = wx.StaticText(self.panelA, -1, "Phase 3", pos=(155,332))


        self.p1vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 220), (125,35), style)
        self.p1vdisp.SetValue(array[3])
        self.p1cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 220), (125,35), style)
        self.p1cdisp.SetValue(array[6])

        self.p2vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 270), (125,35), style)
        self.p2vdisp.SetValue(array[4])
        self.p2cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 270), (125,35), style)
        self.p2cdisp.SetValue(array[7])
        
        self.p3vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 320), (125,35), style)
        self.p3vdisp.SetValue(array[5])
        self.p3cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 320), (125,35), style)
        self.p3cdisp.SetValue(array[8])
        

        lc = wx.LayoutConstraints()
        lc.top.SameAs(self, wx.Top, 5)
        lc.left.SameAs(self, wx.Left, 5)
        lc.bottom.SameAs(self, wx.Bottom, 5)
        lc.right.PercentOf(self, wx.Right, 60)
        self.panelA.SetConstraints(lc)      
       
        #FOR DUMPED DATA DISPLAY#
        self.panelB = wx.Window(self, -1, style=wx.SIMPLE_BORDER)
        self.panelB.SetBackgroundColour(wx.WHITE)

        PanelLabel = wx.StaticText(self.panelB, -1,"Dummped Data",(5,5))              
        PanelLabel.SetForegroundColour(wx.BLACK)

                
        lc = wx.LayoutConstraints()
        lc.top.SameAs(self, wx.Top, 5)
        lc.right.SameAs(self, wx.Right, 5)
        lc.bottom.SameAs(self, wx.Bottom, 5)
        lc.left.RightOf(self.panelA, 5)
        self.panelB.SetConstraints(lc)

        
    def OnTimer(self, event):
        current = time.localtime(time.time())
        ts = time.strftime("%H %M %S", current)
        self.led.SetValue(ts)
        
class MyApp(wx.App):
    def OnInit(self):
        frame = MACE(None, -1, 'MACE Power Quality Monitor')
        frame.Show()
        self.SetTopWindow(frame)
        return 1
    

app = MyApp(0)
app.MainLoop()

In line 26, I have set a fixed value of integers contained by the array, these are the values I will display in my monitor boxes... The real plan is to make the contents of this array dynamic, i.e. changing every time I receive data from my serial port...

I'm a newbie and haven't tried serial communication ever too... Help? Thank you!

Recommended Answers

All 9 Replies

You can try to use the module pySerial.

Can you help me how to do that? any sample of the same kind?

First you need to know the port settings and try to see you can
establish communication using a script.

Some code example:

import serial
                  #portnum baudrate bitsize  parity  stopbit   timeout
ser = serial.Serial(0,    115200,   8,        'N',    1,       0.1)
ser.write(chr(0x20))   # write a byte, convert to character using 'chr'
data = ser.read()      # can use ord() to convert back to ascii code.

If you can get the serial communication to work, then you can think about integrating with your GUI. Most likely, you need to use threading to listen to the serial port.

Thank you.. will try to integrate that soonest I can... I hope I can still get back here and ask a few more questions later when I debug.. :)

import  wx,time,wx.gizmos as gizmos
import serial

class MACE(wx.Frame):
   
    
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, None, -1, "MACE PQ MONITORING SYSTEM", wx.DefaultPosition, wx.Size(360, 480),
                          style=wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)    

#FOR REAL TIME MONITOR#
        self.panelA = wx.Window(self, -1, style=wx.SIMPLE_BORDER)
        self.panelA.SetBackgroundColour(wx.WHITE)
        self.Centre()
        self.SetAutoLayout(True)
        
        style = gizmos.LED_ALIGN_CENTER
        self.led = gizmos.LEDNumberCtrl(self, -1, (100, 30), (150,35), style)
        self.OnTimer(None)
        self.timer = wx.Timer(self, -1)
        self.timer.Start(1000)
        self.Bind(wx.EVT_TIMER, self.OnTimer)        
        
        powlabel = wx.StaticText(self.panelA, -1, "Real Power (Watts)", pos=(60,102))
        self.powdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 90), (125,35), style)

        imlabel = wx.StaticText(self.panelA, -1, "Imaginary Power (VAR)", pos=(60,154))
        self.imdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 142), (125,35), style)
                
        imlabel = wx.StaticText(self.panelA, -1, "Frequency", pos=(60,204))
        self.freqdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 192), (125,35), style)
        
        volabel = wx.StaticText(self.panelA, -1, "Voltages (Volts)", pos=(40,250))
        curlabel = wx.StaticText(self.panelA, -1, "Currents (Amps)", pos=(220,250))

        p1 = wx.StaticText(self.panelA, -1, "Phase 1", pos=(155,282))
        p2 = wx.StaticText(self.panelA, -1, "Phase 2", pos=(155,332))
        p3 = wx.StaticText(self.panelA, -1, "Phase 3", pos=(155,382))

        self.p1vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 270), (125,35), style)
        self.p1cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 270), (125,35), style)
        self.p2vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 320), (125,35), style)
        self.p2cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 320), (125,35), style)
        self.p3vdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (20, 370), (125,35), style)        
        self.p3cdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (200, 370), (125,35), style)

        lc = wx.LayoutConstraints()
        lc.top.SameAs(self, wx.Top, 5)
        lc.left.SameAs(self, wx.Left, 5)
        lc.bottom.SameAs(self, wx.Bottom, 5)
        lc.right.SameAs(self, wx.Right, 5)
        self.panelA.SetConstraints(lc)

        array = self.MyDisplay(None)
        self.powdisp.SetValue(array[0])
        self.imdisp.SetValue(array[1])
        self.freqdisp.SetValue(array[2])
        self.p1vdisp.SetValue(array[3])
        self.p1cdisp.SetValue(array[6])
        self.p2vdisp.SetValue(array[4])
        self.p2cdisp.SetValue(array[7])
        self.p3vdisp.SetValue(array[5])
#i removed this temporarily because the last character in my array still has a '\n' #included in it, which gizmonumeric does not display
        #self.p3cdisp.SetValue(array[8])


        
    def OnTimer(self, event):
        current = time.localtime(time.time())
        ts = time.strftime("%H %M %S", current)
        self.led.SetValue(ts)
#intention is to have a changing value of the array... :(
    def MyDisplay(self,event):
        ser = serial.Serial(8)
        i=0
        while(i==0):
            line=ser.readline()
            value=line.split(",")
            return value

class MyApp(wx.App):
    def OnInit(self):
        frame = MACE(None, -1, 'MACE Power Quality Monitor')
        frame.Show()
        self.SetTopWindow(frame)
        return 1
    

app = MyApp(0)
app.MainLoop()

My problem is... I cannot make this run.. :( I am assuming this is because of the infinite while loop which i have included, but i need that so that i can get the new values from my serial port, right? please help me..

I found out how to remove '\n'! :)

i=0
while(i==0):
    line=ser.readline()
    values=line.rstrip("\n")
    array=values.split(",")
    print array

now.. i need help on using this varying array inside my GUI application.. so that the display boxes get new values each time the array changes value

As a good practice, avoid using 'array' as a variable. array is a module included in the standard distribution.

#
self.panelA = wx.Window(self, -1, style=wx.SIMPLE_BORDER)
#
self.panelA.SetBackgroundColour(wx.WHITE)
#
self.Centre()
#
self.SetAutoLayout(True)
#

#
style = gizmos.LED_ALIGN_CENTER
#
self.led = gizmos.LEDNumberCtrl(self, -1, (100, 30), (150,35), style)
#
self.OnTimer(None)
#
self.timer = wx.Timer(self, -1)
#
self.timer.Start(1000)
#
self.Bind(wx.EVT_TIMER, self.OnTimer)
#

#
powlabel = wx.StaticText(self.panelA, -1, "Real Power (Watts)", pos=(60,102))
#
self.powdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 90), (125,35), style)
#

#
imlabel = wx.StaticText(self.panelA, -1, "Imaginary Power (VAR)", pos=(60,154))
#
self.imdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 142), (125,35), style)

#
self.panelA = wx.Window(self, -1, style=wx.SIMPLE_BORDER)
#
self.panelA.SetBackgroundColour(wx.WHITE)
#
self.Centre()
#
self.SetAutoLayout(True)
#

#
style = gizmos.LED_ALIGN_CENTER
#
self.led = gizmos.LEDNumberCtrl(self, -1, (100, 30), (150,35), style)
#
self.OnTimer(None)
#
self.timer = wx.Timer(self, -1)
#
self.timer.Start(1000)
#
self.Bind(wx.EVT_TIMER, self.OnTimer)
#

#
powlabel = wx.StaticText(self.panelA, -1, "Real Power (Watts)", pos=(60,102))
#
self.powdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 90), (125,35), style)
#

#
imlabel = wx.StaticText(self.panelA, -1, "Imaginary Power (VAR)", pos=(60,154))
#
self.imdisp = gizmos.LEDNumberCtrl(self.panelA, -1, (180, 142), (125,35), style)

learn about code tags http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3 !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.