summaryrefslogtreecommitdiff
path: root/pyserial
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2004-07-09 22:14:17 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2004-07-09 22:14:17 +0000
commit40d71f6632ee7af8b24e2fee78c134d08bd4e376 (patch)
tree3fa01884bd86c35868b7b7302e112db1c93d9dbb /pyserial
parent290999b37be7a1815beb5c1a76547ad266a4961d (diff)
downloadpyserial-git-40d71f6632ee7af8b24e2fee78c134d08bd4e376.tar.gz
make it threadsafe for wxGTK
Diffstat (limited to 'pyserial')
-rw-r--r--pyserial/examples/wxTerminal.py33
1 files changed, 30 insertions, 3 deletions
diff --git a/pyserial/examples/wxTerminal.py b/pyserial/examples/wxTerminal.py
index c2dd827..b53a36e 100644
--- a/pyserial/examples/wxTerminal.py
+++ b/pyserial/examples/wxTerminal.py
@@ -6,6 +6,29 @@ import wxSerialConfigDialog
import serial
import threading
+#----------------------------------------------------------------------
+# Create an own event type, so that GUI updates can be delegated
+# this is required as on some platforms only the main thread can
+# access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
+# could be used too, but an event is more elegant.
+
+SERIALRX = wxNewEventType()
+
+def EVT_SERIALRX(window, function):
+ """function to subscribe to serial data receive events"""
+ window.Connect(-1, -1, SERIALRX, function)
+
+class SerialRxEvent(wxPyCommandEvent):
+ eventType = SERIALRX
+ def __init__(self, windowID, data):
+ wxPyCommandEvent.__init__(self, self.eventType, windowID)
+ self.data = data
+
+ def Clone(self):
+ self.__class__(self.GetId(), self.data)
+
+#----------------------------------------------------------------------
+
ID_CLEAR = wxNewId()
ID_SAVEAS = wxNewId()
ID_SETTINGS = wxNewId()
@@ -164,6 +187,7 @@ class TerminalFrame(wxFrame):
EVT_MENU(self, ID_TERM, self.OnTermSettings)
EVT_CHAR(self, self.OnKey)
EVT_CHAR(self.text_ctrl_output, self.OnKey)
+ EVT_SERIALRX(self, self.OnSerialRead)
EVT_CLOSE(self, self.OnClose)
def OnExit(self, event):
@@ -265,15 +289,16 @@ class TerminalFrame(wxFrame):
else:
print "Extra Key:", code
- def OnSerialRead(self, text):
+ def OnSerialRead(self, event):
"""Handle input from the serial port."""
+ text = event.data
if self.settings.unprintable:
text = ''.join([(c >= ' ') and c or '<%d>' % ord(c) for c in text])
self.text_ctrl_output.AppendText(text)
def ComPortThread(self):
"""Thread that handles the incomming traffic. Does the basic input
- transformation (newlines) and passes the data to OnSerialRead."""
+ transformation (newlines) and generates an SerialRxEvent"""
while self.alive: #loop while this flag is true
text = self.serial.read(1) #read one, with timout
if text: #check if not timeout
@@ -287,7 +312,9 @@ class TerminalFrame(wxFrame):
pass
elif self.settings.newline == NEWLINE_CRLF:
text = text.replace('\r\n', '\n')
- self.OnSerialRead(text) #output text in window
+ event = SerialRxEvent(self.GetId(), text)
+ self.GetEventHandler().AddPendingEvent(event)
+ #~ self.OnSerialRead(text) #output text in window
# end of class TerminalFrame