blob: 4ebabb7aa958a4e81511076e7db5c906b63edd96 [file] [log] [blame]
cliechti80a0ed12003-10-03 23:53:42 +00001#!/usr/bin/env python
Chris Liechtifbdd8a02015-08-09 02:37:45 +02002#
3# A simple terminal application with wxPython.
4#
5# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
6#
7# SPDX-License-Identifier: BSD-3-Clause
cliechti80a0ed12003-10-03 23:53:42 +00008
Chris Liechtib88445d2015-10-04 23:49:28 +02009import codecs
cliechti80a0ed12003-10-03 23:53:42 +000010import serial
11import threading
Chris Liechtib88445d2015-10-04 23:49:28 +020012import wx
13import wxSerialConfigDialog
cliechti80a0ed12003-10-03 23:53:42 +000014
Chris Liechtia7069172015-09-20 20:47:01 +020015# ----------------------------------------------------------------------
cliechti40d71f62004-07-09 22:14:17 +000016# Create an own event type, so that GUI updates can be delegated
17# this is required as on some platforms only the main thread can
18# access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
19# could be used too, but an event is more elegant.
20
cliechtid5d51982008-04-10 23:48:55 +000021SERIALRX = wx.NewEventType()
cliechtibc318d42004-11-13 03:13:12 +000022# bind to serial data receive events
cliechtid5d51982008-04-10 23:48:55 +000023EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
cliechti40d71f62004-07-09 22:14:17 +000024
Chris Liechtia7069172015-09-20 20:47:01 +020025
cliechtid5d51982008-04-10 23:48:55 +000026class SerialRxEvent(wx.PyCommandEvent):
cliechti40d71f62004-07-09 22:14:17 +000027 eventType = SERIALRX
Chris Liechtia7069172015-09-20 20:47:01 +020028
cliechti40d71f62004-07-09 22:14:17 +000029 def __init__(self, windowID, data):
cliechtid5d51982008-04-10 23:48:55 +000030 wx.PyCommandEvent.__init__(self, self.eventType, windowID)
cliechti40d71f62004-07-09 22:14:17 +000031 self.data = data
32
33 def Clone(self):
34 self.__class__(self.GetId(), self.data)
35
Chris Liechtia7069172015-09-20 20:47:01 +020036# ----------------------------------------------------------------------
cliechti40d71f62004-07-09 22:14:17 +000037
Chris Liechtia7069172015-09-20 20:47:01 +020038ID_CLEAR = wx.NewId()
39ID_SAVEAS = wx.NewId()
40ID_SETTINGS = wx.NewId()
41ID_TERM = wx.NewId()
42ID_EXIT = wx.NewId()
43ID_RTS = wx.NewId()
44ID_DTR = wx.NewId()
cliechti80a0ed12003-10-03 23:53:42 +000045
Chris Liechtia7069172015-09-20 20:47:01 +020046NEWLINE_CR = 0
47NEWLINE_LF = 1
48NEWLINE_CRLF = 2
49
cliechti80a0ed12003-10-03 23:53:42 +000050
51class TerminalSetup:
Chris Liechtib88445d2015-10-04 23:49:28 +020052 """
53 Placeholder for various terminal settings. Used to pass the
54 options to the TerminalSettingsDialog.
55 """
cliechti80a0ed12003-10-03 23:53:42 +000056 def __init__(self):
57 self.echo = False
58 self.unprintable = False
59 self.newline = NEWLINE_CRLF
60
Chris Liechtia7069172015-09-20 20:47:01 +020061
cliechtid5d51982008-04-10 23:48:55 +000062class TerminalSettingsDialog(wx.Dialog):
cliechti0eb86712003-10-04 00:49:04 +000063 """Simple dialog with common terminal settings like echo, newline mode."""
Chris Liechti4caf6a52015-08-04 01:07:45 +020064
cliechti80a0ed12003-10-03 23:53:42 +000065 def __init__(self, *args, **kwds):
66 self.settings = kwds['settings']
67 del kwds['settings']
68 # begin wxGlade: TerminalSettingsDialog.__init__
cliechtid5d51982008-04-10 23:48:55 +000069 kwds["style"] = wx.DEFAULT_DIALOG_STYLE
70 wx.Dialog.__init__(self, *args, **kwds)
71 self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo")
72 self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters")
73 self.radio_box_newline = wx.RadioBox(self, -1, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
Chris Liechtibe00ee92015-09-12 02:02:04 +020074 self.sizer_4_staticbox = wx.StaticBox(self, -1, "Input/Output")
Chris Liechtid7e9e9e2015-10-14 18:14:27 +020075 self.button_ok = wx.Button(self, wx.ID_OK, "")
76 self.button_cancel = wx.Button(self, wx.ID_CANCEL, "")
cliechti80a0ed12003-10-03 23:53:42 +000077
78 self.__set_properties()
79 self.__do_layout()
80 # end wxGlade
81 self.__attach_events()
82 self.checkbox_echo.SetValue(self.settings.echo)
83 self.checkbox_unprintable.SetValue(self.settings.unprintable)
84 self.radio_box_newline.SetSelection(self.settings.newline)
85
86 def __set_properties(self):
87 # begin wxGlade: TerminalSettingsDialog.__set_properties
88 self.SetTitle("Terminal Settings")
89 self.radio_box_newline.SetSelection(0)
90 self.button_ok.SetDefault()
91 # end wxGlade
92
93 def __do_layout(self):
94 # begin wxGlade: TerminalSettingsDialog.__do_layout
cliechtid5d51982008-04-10 23:48:55 +000095 sizer_2 = wx.BoxSizer(wx.VERTICAL)
96 sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
Chris Liechtibe00ee92015-09-12 02:02:04 +020097 self.sizer_4_staticbox.Lower()
98 sizer_4 = wx.StaticBoxSizer(self.sizer_4_staticbox, wx.VERTICAL)
cliechtid5d51982008-04-10 23:48:55 +000099 sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
100 sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
cliechti80a0ed12003-10-03 23:53:42 +0000101 sizer_4.Add(self.radio_box_newline, 0, 0, 0)
cliechtid5d51982008-04-10 23:48:55 +0000102 sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
cliechti80a0ed12003-10-03 23:53:42 +0000103 sizer_3.Add(self.button_ok, 0, 0, 0)
104 sizer_3.Add(self.button_cancel, 0, 0, 0)
Chris Liechtibe00ee92015-09-12 02:02:04 +0200105 sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4)
cliechti80a0ed12003-10-03 23:53:42 +0000106 self.SetSizer(sizer_2)
107 sizer_2.Fit(self)
cliechti80a0ed12003-10-03 23:53:42 +0000108 self.Layout()
109 # end wxGlade
110
111 def __attach_events(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200112 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.button_ok.GetId())
113 self.Bind(wx.EVT_BUTTON, self.OnCancel, id=self.button_cancel.GetId())
Chris Liechti4caf6a52015-08-04 01:07:45 +0200114
cliechti80a0ed12003-10-03 23:53:42 +0000115 def OnOK(self, events):
cliechti0eb86712003-10-04 00:49:04 +0000116 """Update data wil new values and close dialog."""
cliechti80a0ed12003-10-03 23:53:42 +0000117 self.settings.echo = self.checkbox_echo.GetValue()
118 self.settings.unprintable = self.checkbox_unprintable.GetValue()
119 self.settings.newline = self.radio_box_newline.GetSelection()
cliechtid5d51982008-04-10 23:48:55 +0000120 self.EndModal(wx.ID_OK)
Chris Liechti4caf6a52015-08-04 01:07:45 +0200121
cliechti80a0ed12003-10-03 23:53:42 +0000122 def OnCancel(self, events):
cliechti0eb86712003-10-04 00:49:04 +0000123 """Do not update data but close dialog."""
cliechtid5d51982008-04-10 23:48:55 +0000124 self.EndModal(wx.ID_CANCEL)
cliechti80a0ed12003-10-03 23:53:42 +0000125
126# end of class TerminalSettingsDialog
127
128
cliechtid5d51982008-04-10 23:48:55 +0000129class TerminalFrame(wx.Frame):
cliechti80a0ed12003-10-03 23:53:42 +0000130 """Simple terminal program for wxPython"""
Chris Liechti4caf6a52015-08-04 01:07:45 +0200131
cliechti80a0ed12003-10-03 23:53:42 +0000132 def __init__(self, *args, **kwds):
133 self.serial = serial.Serial()
Chris Liechtia7069172015-09-20 20:47:01 +0200134 self.serial.timeout = 0.5 # make sure that the alive event can be checked from time to time
135 self.settings = TerminalSetup() # placeholder for the settings
cliechti80a0ed12003-10-03 23:53:42 +0000136 self.thread = None
Chris Liechti4caf6a52015-08-04 01:07:45 +0200137 self.alive = threading.Event()
cliechti80a0ed12003-10-03 23:53:42 +0000138 # begin wxGlade: TerminalFrame.__init__
cliechtid5d51982008-04-10 23:48:55 +0000139 kwds["style"] = wx.DEFAULT_FRAME_STYLE
140 wx.Frame.__init__(self, *args, **kwds)
Chris Liechti3d3e71e2016-01-24 23:55:05 +0100141
cliechti80a0ed12003-10-03 23:53:42 +0000142 # Menu Bar
cliechtid5d51982008-04-10 23:48:55 +0000143 self.frame_terminal_menubar = wx.MenuBar()
cliechtid5d51982008-04-10 23:48:55 +0000144 wxglade_tmp_menu = wx.Menu()
145 wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL)
146 wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000147 wxglade_tmp_menu.AppendSeparator()
cliechtid5d51982008-04-10 23:48:55 +0000148 wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000149 wxglade_tmp_menu.AppendSeparator()
cliechtid5d51982008-04-10 23:48:55 +0000150 wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000151 self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File")
Chris Liechtibe00ee92015-09-12 02:02:04 +0200152 wxglade_tmp_menu = wx.Menu()
153 wxglade_tmp_menu.Append(ID_RTS, "RTS", "", wx.ITEM_CHECK)
154 wxglade_tmp_menu.Append(ID_DTR, "&DTR", "", wx.ITEM_CHECK)
155 wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL)
156 self.frame_terminal_menubar.Append(wxglade_tmp_menu, "Serial Port")
157 self.SetMenuBar(self.frame_terminal_menubar)
cliechti80a0ed12003-10-03 23:53:42 +0000158 # Menu Bar end
Chris Liechtibe00ee92015-09-12 02:02:04 +0200159 self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY)
cliechti80a0ed12003-10-03 23:53:42 +0000160
161 self.__set_properties()
162 self.__do_layout()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200163
164 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
165 self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)
166 self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM)
167 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
168 self.Bind(wx.EVT_MENU, self.OnRTS, id=ID_RTS)
169 self.Bind(wx.EVT_MENU, self.OnDTR, id=ID_DTR)
170 self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS)
cliechti80a0ed12003-10-03 23:53:42 +0000171 # end wxGlade
Chris Liechtia7069172015-09-20 20:47:01 +0200172 self.__attach_events() # register events
173 self.OnPortSettings(None) # call setup dialog on startup, opens port
cliechtibc318d42004-11-13 03:13:12 +0000174 if not self.alive.isSet():
cliechti0eb86712003-10-04 00:49:04 +0000175 self.Close()
cliechti80a0ed12003-10-03 23:53:42 +0000176
177 def StartThread(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200178 """Start the receiver thread"""
cliechti80a0ed12003-10-03 23:53:42 +0000179 self.thread = threading.Thread(target=self.ComPortThread)
180 self.thread.setDaemon(1)
cliechtibc318d42004-11-13 03:13:12 +0000181 self.alive.set()
cliechtif7776692005-10-02 21:51:42 +0000182 self.thread.start()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200183 self.serial.rts = True
184 self.serial.dtr = True
185 self.frame_terminal_menubar.Check(ID_RTS, self.serial.rts)
186 self.frame_terminal_menubar.Check(ID_DTR, self.serial.dtr)
cliechti80a0ed12003-10-03 23:53:42 +0000187
188 def StopThread(self):
Chris Liechtib88445d2015-10-04 23:49:28 +0200189 """Stop the receiver thread, wait until it's finished."""
cliechti80a0ed12003-10-03 23:53:42 +0000190 if self.thread is not None:
Chris Liechtia7069172015-09-20 20:47:01 +0200191 self.alive.clear() # clear alive event for thread
192 self.thread.join() # wait until thread has finished
cliechti80a0ed12003-10-03 23:53:42 +0000193 self.thread = None
Chris Liechti4caf6a52015-08-04 01:07:45 +0200194
cliechti80a0ed12003-10-03 23:53:42 +0000195 def __set_properties(self):
196 # begin wxGlade: TerminalFrame.__set_properties
197 self.SetTitle("Serial Terminal")
198 self.SetSize((546, 383))
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200199 self.text_ctrl_output.SetFont(wx.Font(9, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
cliechti80a0ed12003-10-03 23:53:42 +0000200 # end wxGlade
201
202 def __do_layout(self):
203 # begin wxGlade: TerminalFrame.__do_layout
cliechtid5d51982008-04-10 23:48:55 +0000204 sizer_1 = wx.BoxSizer(wx.VERTICAL)
205 sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0)
cliechti80a0ed12003-10-03 23:53:42 +0000206 self.SetSizer(sizer_1)
207 self.Layout()
208 # end wxGlade
209
210 def __attach_events(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200211 # register events at the controls
212 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
213 self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)
214 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
215 self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS)
216 self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM)
217 self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey)
cliechtibc318d42004-11-13 03:13:12 +0000218 self.Bind(EVT_SERIALRX, self.OnSerialRead)
cliechtid5d51982008-04-10 23:48:55 +0000219 self.Bind(wx.EVT_CLOSE, self.OnClose)
cliechti80a0ed12003-10-03 23:53:42 +0000220
Chris Liechtibe00ee92015-09-12 02:02:04 +0200221 def OnExit(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000222 """Menu point Exit"""
223 self.Close()
224
225 def OnClose(self, event):
cliechti0eb86712003-10-04 00:49:04 +0000226 """Called on application shutdown."""
Chris Liechtibe00ee92015-09-12 02:02:04 +0200227 self.StopThread() # stop reader thread
228 self.serial.close() # cleanup
229 self.Destroy() # close windows, exit app
cliechti80a0ed12003-10-03 23:53:42 +0000230
Chris Liechtibe00ee92015-09-12 02:02:04 +0200231 def OnSaveAs(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000232 """Save contents of output window."""
Chris Liechtibe00ee92015-09-12 02:02:04 +0200233 with wx.FileDialog(
234 None,
235 "Save Text As...",
236 ".",
237 "",
238 "Text File|*.txt|All Files|*",
239 wx.SAVE) as dlg:
240 if dlg.ShowModal() == wx.ID_OK:
241 filename = dlg.GetPath()
Chris Liechtib88445d2015-10-04 23:49:28 +0200242 with codecs.open(filename, 'w', encoding='utf-8') as f:
243 text = self.text_ctrl_output.GetValue().encode("utf-8")
244 f.write(text)
Chris Liechtia7069172015-09-20 20:47:01 +0200245
Chris Liechtibe00ee92015-09-12 02:02:04 +0200246 def OnClear(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000247 """Clear contents of output window."""
248 self.text_ctrl_output.Clear()
Chris Liechtia7069172015-09-20 20:47:01 +0200249
Chris Liechtibe00ee92015-09-12 02:02:04 +0200250 def OnPortSettings(self, event): # wxGlade: TerminalFrame.<event_handler>
Chris Liechtib88445d2015-10-04 23:49:28 +0200251 """
252 Show the port settings dialog. The reader thread is stopped for the
253 settings change.
254 """
Chris Liechtibe00ee92015-09-12 02:02:04 +0200255 if event is not None: # will be none when called on startup
cliechti0eb86712003-10-04 00:49:04 +0000256 self.StopThread()
257 self.serial.close()
258 ok = False
259 while not ok:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200260 with wxSerialConfigDialog.SerialConfigDialog(
Chris Liechtib88445d2015-10-04 23:49:28 +0200261 self,
Chris Liechtibe00ee92015-09-12 02:02:04 +0200262 -1,
263 "",
Chris Liechtia7069172015-09-20 20:47:01 +0200264 show=wxSerialConfigDialog.SHOW_BAUDRATE | wxSerialConfigDialog.SHOW_FORMAT | wxSerialConfigDialog.SHOW_FLOW,
Chris Liechtibe00ee92015-09-12 02:02:04 +0200265 serial=self.serial) as dialog_serial_cfg:
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200266 dialog_serial_cfg.CenterOnParent()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200267 result = dialog_serial_cfg.ShowModal()
268 # open port if not called on startup, open it on startup and OK too
cliechtid5d51982008-04-10 23:48:55 +0000269 if result == wx.ID_OK or event is not None:
cliechti0eb86712003-10-04 00:49:04 +0000270 try:
271 self.serial.open()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200272 except serial.SerialException as e:
Chris Liechtib88445d2015-10-04 23:49:28 +0200273 with wx.MessageDialog(self, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)as dlg:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200274 dlg.ShowModal()
cliechti0eb86712003-10-04 00:49:04 +0000275 else:
276 self.StartThread()
Chris Liechtib88445d2015-10-04 23:49:28 +0200277 self.SetTitle("Serial Terminal on %s [%s,%s,%s,%s%s%s]" % (
Chris Liechtibe00ee92015-09-12 02:02:04 +0200278 self.serial.portstr,
279 self.serial.baudrate,
280 self.serial.bytesize,
281 self.serial.parity,
282 self.serial.stopbits,
283 ' RTS/CTS' if self.serial.rtscts else '',
284 ' Xon/Xoff' if self.serial.xonxoff else '',
285 ))
cliechti0eb86712003-10-04 00:49:04 +0000286 ok = True
287 else:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200288 # on startup, dialog aborted
cliechtibc318d42004-11-13 03:13:12 +0000289 self.alive.clear()
cliechti0eb86712003-10-04 00:49:04 +0000290 ok = True
cliechti80a0ed12003-10-03 23:53:42 +0000291
Chris Liechtibe00ee92015-09-12 02:02:04 +0200292 def OnTermSettings(self, event): # wxGlade: TerminalFrame.<event_handler>
293 """\
294 Menu point Terminal Settings. Show the settings dialog
295 with the current terminal settings.
296 """
Chris Liechtib88445d2015-10-04 23:49:28 +0200297 with TerminalSettingsDialog(self, -1, "", settings=self.settings) as dialog:
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200298 dialog.CenterOnParent()
Chris Liechtia7069172015-09-20 20:47:01 +0200299 dialog.ShowModal()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200300
cliechti80a0ed12003-10-03 23:53:42 +0000301 def OnKey(self, event):
Chris Liechtibe00ee92015-09-12 02:02:04 +0200302 """\
Chris Liechtib88445d2015-10-04 23:49:28 +0200303 Key event handler. If the key is in the ASCII range, write it to the
Chris Liechtibe00ee92015-09-12 02:02:04 +0200304 serial port. Newline handling and local echo is also done here.
305 """
Chris Liechtib88445d2015-10-04 23:49:28 +0200306 code = event.GetUnicodeKey()
307 if code < 256: # XXX bug in some versions of wx returning only capital letters
308 code = event.GetKeyCode()
309 if code == 13: # is it a newline? (check for CR which is the RETURN key)
310 if self.settings.echo: # do echo if needed
311 self.text_ctrl_output.AppendText('\n')
312 if self.settings.newline == NEWLINE_CR:
313 self.serial.write(b'\r') # send CR
314 elif self.settings.newline == NEWLINE_LF:
315 self.serial.write(b'\n') # send LF
316 elif self.settings.newline == NEWLINE_CRLF:
317 self.serial.write(b'\r\n') # send CR+LF
cliechti0eb86712003-10-04 00:49:04 +0000318 else:
Chris Liechtib88445d2015-10-04 23:49:28 +0200319 char = unichr(code)
320 if self.settings.echo: # do echo if needed
321 self.WriteText(char)
322 self.serial.write(char.encode('UTF-8', 'replace')) # send the character
cliechti80a0ed12003-10-03 23:53:42 +0000323
Chris Liechtib88445d2015-10-04 23:49:28 +0200324 def WriteText(self, text):
cliechti80a0ed12003-10-03 23:53:42 +0000325 if self.settings.unprintable:
Chris Liechtia7069172015-09-20 20:47:01 +0200326 text = ''.join([c if (c >= ' ' and c != '\x7f') else unichr(0x2400 + ord(c)) for c in text])
cliechti80a0ed12003-10-03 23:53:42 +0000327 self.text_ctrl_output.AppendText(text)
328
Chris Liechtib88445d2015-10-04 23:49:28 +0200329 def OnSerialRead(self, event):
330 """Handle input from the serial port."""
331 self.WriteText(event.data.decode('UTF-8', 'replace'))
332
cliechti80a0ed12003-10-03 23:53:42 +0000333 def ComPortThread(self):
Chris Liechtibe00ee92015-09-12 02:02:04 +0200334 """\
Chris Liechtib88445d2015-10-04 23:49:28 +0200335 Thread that handles the incoming traffic. Does the basic input
Chris Liechtibe00ee92015-09-12 02:02:04 +0200336 transformation (newlines) and generates an SerialRxEvent
337 """
338 while self.alive.isSet():
339 b = self.serial.read(self.serial.in_waiting or 1)
340 if b:
341 # newline transformation
cliechti80a0ed12003-10-03 23:53:42 +0000342 if self.settings.newline == NEWLINE_CR:
Chris Liechtib88445d2015-10-04 23:49:28 +0200343 b = b.replace(b'\r', b'\n')
cliechti80a0ed12003-10-03 23:53:42 +0000344 elif self.settings.newline == NEWLINE_LF:
345 pass
346 elif self.settings.newline == NEWLINE_CRLF:
Chris Liechtib88445d2015-10-04 23:49:28 +0200347 b = b.replace(b'\r\n', b'\n')
Chris Liechtibe00ee92015-09-12 02:02:04 +0200348 event = SerialRxEvent(self.GetId(), b)
cliechti40d71f62004-07-09 22:14:17 +0000349 self.GetEventHandler().AddPendingEvent(event)
Chris Liechtibe00ee92015-09-12 02:02:04 +0200350
351 def OnRTS(self, event): # wxGlade: TerminalFrame.<event_handler>
352 self.serial.rts = event.IsChecked()
353
354 def OnDTR(self, event): # wxGlade: TerminalFrame.<event_handler>
355 self.serial.dtr = event.Checked()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200356
cliechti80a0ed12003-10-03 23:53:42 +0000357# end of class TerminalFrame
358
359
cliechtid5d51982008-04-10 23:48:55 +0000360class MyApp(wx.App):
cliechti80a0ed12003-10-03 23:53:42 +0000361 def OnInit(self):
cliechtid5d51982008-04-10 23:48:55 +0000362 wx.InitAllImageHandlers()
cliechti80a0ed12003-10-03 23:53:42 +0000363 frame_terminal = TerminalFrame(None, -1, "")
364 self.SetTopWindow(frame_terminal)
Chris Liechtibe00ee92015-09-12 02:02:04 +0200365 frame_terminal.Show(True)
cliechti80a0ed12003-10-03 23:53:42 +0000366 return 1
367
368# end of class MyApp
369
370if __name__ == "__main__":
371 app = MyApp(0)
372 app.MainLoop()