~ubuntu-branches/ubuntu/quantal/pyserial/quantal

« back to all changes in this revision

Viewing changes to examples/wxTerminal.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2004-08-29 14:49:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040829144957-moa3k4yx4qte5qth
Tags: 2.1-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import serial
7
7
import threading
8
8
 
 
9
#----------------------------------------------------------------------
 
10
# Create an own event type, so that GUI updates can be delegated
 
11
# this is required as on some platforms only the main thread can
 
12
# access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
 
13
# could be used too, but an event is more elegant.
 
14
 
 
15
SERIALRX = wxNewEventType()
 
16
 
 
17
def EVT_SERIALRX(window, function):
 
18
    """function to subscribe to serial data receive events"""
 
19
    window.Connect(-1, -1, SERIALRX, function)
 
20
 
 
21
class SerialRxEvent(wxPyCommandEvent):
 
22
    eventType = SERIALRX
 
23
    def __init__(self, windowID, data):
 
24
        wxPyCommandEvent.__init__(self, self.eventType, windowID)
 
25
        self.data = data
 
26
 
 
27
    def Clone(self):
 
28
        self.__class__(self.GetId(), self.data)
 
29
 
 
30
#----------------------------------------------------------------------
 
31
 
9
32
ID_CLEAR        = wxNewId()
10
33
ID_SAVEAS       = wxNewId()
11
34
ID_SETTINGS     = wxNewId()
164
187
        EVT_MENU(self, ID_TERM, self.OnTermSettings)
165
188
        EVT_CHAR(self, self.OnKey)
166
189
        EVT_CHAR(self.text_ctrl_output, self.OnKey)
 
190
        EVT_SERIALRX(self, self.OnSerialRead)
167
191
        EVT_CLOSE(self, self.OnClose)
168
192
 
169
193
    def OnExit(self, event):
265
289
        else:
266
290
            print "Extra Key:", code
267
291
 
268
 
    def OnSerialRead(self, text):
 
292
    def OnSerialRead(self, event):
269
293
        """Handle input from the serial port."""
 
294
        text = event.data
270
295
        if self.settings.unprintable:
271
296
            text = ''.join([(c >= ' ') and c or '<%d>' % ord(c)  for c in text])
272
297
        self.text_ctrl_output.AppendText(text)
273
298
 
274
299
    def ComPortThread(self):
275
300
        """Thread that handles the incomming traffic. Does the basic input
276
 
           transformation (newlines) and passes the data to OnSerialRead."""
 
301
           transformation (newlines) and generates an SerialRxEvent"""
277
302
        while self.alive:                       #loop while this flag is true
278
303
            text = self.serial.read(1)          #read one, with timout
279
304
            if text:                            #check if not timeout
287
312
                    pass
288
313
                elif self.settings.newline == NEWLINE_CRLF:
289
314
                    text = text.replace('\r\n', '\n')
290
 
                self.OnSerialRead(text)         #output text in window
 
315
                event = SerialRxEvent(self.GetId(), text)
 
316
                self.GetEventHandler().AddPendingEvent(event)
 
317
                #~ self.OnSerialRead(text)         #output text in window
291
318
            
292
319
# end of class TerminalFrame
293
320