Hi again,
I've been looking more at reading serial - this time with a GUI in front. After attempts to get it working I thought it best to try threading to perform the task.
What it should do is read the serial port and update a label with the frame data. When you press the start button it works just fine the first time but then doesn't update it with further data.
I've therefor concluded I might need a further loop somewhere to call the thread again but I've not had any luck. Can someone take a look at the code below and possibly give me a starting point to allow it to continually update?
The threading code was an example I found which I have edited - and I'm very new at it so I've probably not done it correctly. I'm not sure I have actually called the function within the thread correctly...however if someone can see something I am missing that would be great.
Thanks,
import wx
import serial
import datetime;
import time
import wx
from threading import Thread
from wx.lib.pubsub import Publisher
class TestThread(Thread):
def __init__(self):
Thread.__init__(self)
self.start()
self.XBEE()
def XBEE(self):
self.ser = serial.Serial('com7',9600,timeout=1)
while True:
try:
Data_in = self.ser.readline().encode('hex')
for data in Data_in.split('7e'):
if data[6:8] == '90':
print "=========================="
print "Found Packet: 7e%s" % data
print "Packet Type = ZgBee RX Packet"
self.AH = data [10:18]
self.AL = data [18:26]
print "Device Address = ", self.AH, self.AL
self.TH = data [34:36]
self.TL = data [38:40]
self.THc = int(self.TH, 16)
self.TLc = int(self.TL, 16)
if data[41:42] == '0':
self.Temp = "%d.%d" % (self.THc, self.TLc)
print "Temperature:", self.Temp
else:
self.Temp = "-%d.%d" % (self.THc, self.TLc)
print "Temperature:", self.Temp
now = datetime.datetime.now()
self.CUR_YEAR = now.year
self.CUR_MONTH = now.month
self.CUR_DAY = now.day
self.CUR_HOUR = now.hour
self.CUR_MIN = now.minute
self.CUR_SEC = now.second
self.CUR_MSEC = now.microsecond
self.timedate = "Time and Date: %d/%d/%d - %d:%d:%d:%d" % (self.CUR_DAY, self.CUR_MONTH, self.CUR_YEAR, self.CUR_HOUR, self.CUR_MIN, self.CUR_SEC, self.CUR_MSEC)
print self.timedate
print "======================="
print " "
Publisher().sendMessage("update", self.Temp)
except ValueError:
print "Invalid Number detected"
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
self.displayLbl = wx.StaticText(panel, label="Temperature Reading")
self.btn = btn = wx.Button(panel, label="Start Reading")
btn.Bind(wx.EVT_BUTTON, self.onButton)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
panel.SetSizer(sizer)
Publisher().subscribe(self.updateDisplay, "update")
#----------------------------------------------------------------------
def onButton(self, event):
TestThread()
btn = event.GetEventObject()
btn.Disable()
#----------------------------------------------------------------------
def updateDisplay(self, msg):
"""
Receives data from thread and updates the display
"""
t = msg.data
if isinstance(t, int):
self.displayLbl.SetLabel(t)
else:
self.displayLbl.SetLabel("%s" % t)
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()