blob: 08c9ee99f0deeb4897886f8050959e7a0794c322 [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
Chris Liechti9da0f0f2020-09-15 01:23:11 +020010from serial.tools.miniterm import unichr
cliechti80a0ed12003-10-03 23:53:42 +000011import serial
12import threading
Chris Liechtib88445d2015-10-04 23:49:28 +020013import wx
Chris Liechti9da0f0f2020-09-15 01:23:11 +020014import wx.lib.newevent
Chris Liechtib88445d2015-10-04 23:49:28 +020015import wxSerialConfigDialog
cliechti80a0ed12003-10-03 23:53:42 +000016
Chris Liechti9da0f0f2020-09-15 01:23:11 +020017try:
18 unichr
19except NameError:
20 unichr = chr
21
Chris Liechtia7069172015-09-20 20:47:01 +020022# ----------------------------------------------------------------------
cliechti40d71f62004-07-09 22:14:17 +000023# Create an own event type, so that GUI updates can be delegated
24# this is required as on some platforms only the main thread can
25# access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
26# could be used too, but an event is more elegant.
27
Chris Liechti9da0f0f2020-09-15 01:23:11 +020028SerialRxEvent, EVT_SERIALRX = wx.lib.newevent.NewEvent()
cliechtid5d51982008-04-10 23:48:55 +000029SERIALRX = wx.NewEventType()
cliechti40d71f62004-07-09 22:14:17 +000030
Chris Liechtia7069172015-09-20 20:47:01 +020031# ----------------------------------------------------------------------
cliechti40d71f62004-07-09 22:14:17 +000032
Chris Liechtia7069172015-09-20 20:47:01 +020033ID_CLEAR = wx.NewId()
34ID_SAVEAS = wx.NewId()
35ID_SETTINGS = wx.NewId()
36ID_TERM = wx.NewId()
37ID_EXIT = wx.NewId()
38ID_RTS = wx.NewId()
39ID_DTR = wx.NewId()
cliechti80a0ed12003-10-03 23:53:42 +000040
Chris Liechtia7069172015-09-20 20:47:01 +020041NEWLINE_CR = 0
42NEWLINE_LF = 1
43NEWLINE_CRLF = 2
44
cliechti80a0ed12003-10-03 23:53:42 +000045
46class TerminalSetup:
Chris Liechtib88445d2015-10-04 23:49:28 +020047 """
48 Placeholder for various terminal settings. Used to pass the
49 options to the TerminalSettingsDialog.
50 """
cliechti80a0ed12003-10-03 23:53:42 +000051 def __init__(self):
52 self.echo = False
53 self.unprintable = False
54 self.newline = NEWLINE_CRLF
55
Chris Liechtia7069172015-09-20 20:47:01 +020056
cliechtid5d51982008-04-10 23:48:55 +000057class TerminalSettingsDialog(wx.Dialog):
cliechti0eb86712003-10-04 00:49:04 +000058 """Simple dialog with common terminal settings like echo, newline mode."""
Chris Liechti4caf6a52015-08-04 01:07:45 +020059
cliechti80a0ed12003-10-03 23:53:42 +000060 def __init__(self, *args, **kwds):
61 self.settings = kwds['settings']
62 del kwds['settings']
63 # begin wxGlade: TerminalSettingsDialog.__init__
cliechtid5d51982008-04-10 23:48:55 +000064 kwds["style"] = wx.DEFAULT_DIALOG_STYLE
65 wx.Dialog.__init__(self, *args, **kwds)
66 self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo")
67 self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters")
68 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 +020069 self.sizer_4_staticbox = wx.StaticBox(self, -1, "Input/Output")
Chris Liechtid7e9e9e2015-10-14 18:14:27 +020070 self.button_ok = wx.Button(self, wx.ID_OK, "")
71 self.button_cancel = wx.Button(self, wx.ID_CANCEL, "")
cliechti80a0ed12003-10-03 23:53:42 +000072
73 self.__set_properties()
74 self.__do_layout()
75 # end wxGlade
76 self.__attach_events()
77 self.checkbox_echo.SetValue(self.settings.echo)
78 self.checkbox_unprintable.SetValue(self.settings.unprintable)
79 self.radio_box_newline.SetSelection(self.settings.newline)
80
81 def __set_properties(self):
82 # begin wxGlade: TerminalSettingsDialog.__set_properties
83 self.SetTitle("Terminal Settings")
84 self.radio_box_newline.SetSelection(0)
85 self.button_ok.SetDefault()
86 # end wxGlade
87
88 def __do_layout(self):
89 # begin wxGlade: TerminalSettingsDialog.__do_layout
cliechtid5d51982008-04-10 23:48:55 +000090 sizer_2 = wx.BoxSizer(wx.VERTICAL)
91 sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
Chris Liechtibe00ee92015-09-12 02:02:04 +020092 self.sizer_4_staticbox.Lower()
93 sizer_4 = wx.StaticBoxSizer(self.sizer_4_staticbox, wx.VERTICAL)
cliechtid5d51982008-04-10 23:48:55 +000094 sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
95 sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
cliechti80a0ed12003-10-03 23:53:42 +000096 sizer_4.Add(self.radio_box_newline, 0, 0, 0)
cliechtid5d51982008-04-10 23:48:55 +000097 sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
cliechti80a0ed12003-10-03 23:53:42 +000098 sizer_3.Add(self.button_ok, 0, 0, 0)
99 sizer_3.Add(self.button_cancel, 0, 0, 0)
Chris Liechtibe00ee92015-09-12 02:02:04 +0200100 sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4)
cliechti80a0ed12003-10-03 23:53:42 +0000101 self.SetSizer(sizer_2)
102 sizer_2.Fit(self)
cliechti80a0ed12003-10-03 23:53:42 +0000103 self.Layout()
104 # end wxGlade
105
106 def __attach_events(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200107 self.Bind(wx.EVT_BUTTON, self.OnOK, id=self.button_ok.GetId())
108 self.Bind(wx.EVT_BUTTON, self.OnCancel, id=self.button_cancel.GetId())
Chris Liechti4caf6a52015-08-04 01:07:45 +0200109
cliechti80a0ed12003-10-03 23:53:42 +0000110 def OnOK(self, events):
cliechti0eb86712003-10-04 00:49:04 +0000111 """Update data wil new values and close dialog."""
cliechti80a0ed12003-10-03 23:53:42 +0000112 self.settings.echo = self.checkbox_echo.GetValue()
113 self.settings.unprintable = self.checkbox_unprintable.GetValue()
114 self.settings.newline = self.radio_box_newline.GetSelection()
cliechtid5d51982008-04-10 23:48:55 +0000115 self.EndModal(wx.ID_OK)
Chris Liechti4caf6a52015-08-04 01:07:45 +0200116
cliechti80a0ed12003-10-03 23:53:42 +0000117 def OnCancel(self, events):
cliechti0eb86712003-10-04 00:49:04 +0000118 """Do not update data but close dialog."""
cliechtid5d51982008-04-10 23:48:55 +0000119 self.EndModal(wx.ID_CANCEL)
cliechti80a0ed12003-10-03 23:53:42 +0000120
121# end of class TerminalSettingsDialog
122
123
cliechtid5d51982008-04-10 23:48:55 +0000124class TerminalFrame(wx.Frame):
cliechti80a0ed12003-10-03 23:53:42 +0000125 """Simple terminal program for wxPython"""
Chris Liechti4caf6a52015-08-04 01:07:45 +0200126
cliechti80a0ed12003-10-03 23:53:42 +0000127 def __init__(self, *args, **kwds):
128 self.serial = serial.Serial()
Chris Liechtia7069172015-09-20 20:47:01 +0200129 self.serial.timeout = 0.5 # make sure that the alive event can be checked from time to time
130 self.settings = TerminalSetup() # placeholder for the settings
cliechti80a0ed12003-10-03 23:53:42 +0000131 self.thread = None
Chris Liechti4caf6a52015-08-04 01:07:45 +0200132 self.alive = threading.Event()
cliechti80a0ed12003-10-03 23:53:42 +0000133 # begin wxGlade: TerminalFrame.__init__
cliechtid5d51982008-04-10 23:48:55 +0000134 kwds["style"] = wx.DEFAULT_FRAME_STYLE
135 wx.Frame.__init__(self, *args, **kwds)
Chris Liechti3d3e71e2016-01-24 23:55:05 +0100136
cliechti80a0ed12003-10-03 23:53:42 +0000137 # Menu Bar
cliechtid5d51982008-04-10 23:48:55 +0000138 self.frame_terminal_menubar = wx.MenuBar()
cliechtid5d51982008-04-10 23:48:55 +0000139 wxglade_tmp_menu = wx.Menu()
140 wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL)
141 wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000142 wxglade_tmp_menu.AppendSeparator()
cliechtid5d51982008-04-10 23:48:55 +0000143 wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000144 wxglade_tmp_menu.AppendSeparator()
cliechtid5d51982008-04-10 23:48:55 +0000145 wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL)
cliechti80a0ed12003-10-03 23:53:42 +0000146 self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File")
Chris Liechtibe00ee92015-09-12 02:02:04 +0200147 wxglade_tmp_menu = wx.Menu()
148 wxglade_tmp_menu.Append(ID_RTS, "RTS", "", wx.ITEM_CHECK)
149 wxglade_tmp_menu.Append(ID_DTR, "&DTR", "", wx.ITEM_CHECK)
150 wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL)
151 self.frame_terminal_menubar.Append(wxglade_tmp_menu, "Serial Port")
152 self.SetMenuBar(self.frame_terminal_menubar)
cliechti80a0ed12003-10-03 23:53:42 +0000153 # Menu Bar end
Chris Liechtibe00ee92015-09-12 02:02:04 +0200154 self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY)
cliechti80a0ed12003-10-03 23:53:42 +0000155
156 self.__set_properties()
157 self.__do_layout()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200158
159 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
160 self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)
161 self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM)
162 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
163 self.Bind(wx.EVT_MENU, self.OnRTS, id=ID_RTS)
164 self.Bind(wx.EVT_MENU, self.OnDTR, id=ID_DTR)
165 self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS)
cliechti80a0ed12003-10-03 23:53:42 +0000166 # end wxGlade
Chris Liechtia7069172015-09-20 20:47:01 +0200167 self.__attach_events() # register events
168 self.OnPortSettings(None) # call setup dialog on startup, opens port
cliechtibc318d42004-11-13 03:13:12 +0000169 if not self.alive.isSet():
cliechti0eb86712003-10-04 00:49:04 +0000170 self.Close()
cliechti80a0ed12003-10-03 23:53:42 +0000171
172 def StartThread(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200173 """Start the receiver thread"""
cliechti80a0ed12003-10-03 23:53:42 +0000174 self.thread = threading.Thread(target=self.ComPortThread)
175 self.thread.setDaemon(1)
cliechtibc318d42004-11-13 03:13:12 +0000176 self.alive.set()
cliechtif7776692005-10-02 21:51:42 +0000177 self.thread.start()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200178 self.serial.rts = True
179 self.serial.dtr = True
180 self.frame_terminal_menubar.Check(ID_RTS, self.serial.rts)
181 self.frame_terminal_menubar.Check(ID_DTR, self.serial.dtr)
cliechti80a0ed12003-10-03 23:53:42 +0000182
183 def StopThread(self):
Chris Liechtib88445d2015-10-04 23:49:28 +0200184 """Stop the receiver thread, wait until it's finished."""
cliechti80a0ed12003-10-03 23:53:42 +0000185 if self.thread is not None:
Chris Liechtia7069172015-09-20 20:47:01 +0200186 self.alive.clear() # clear alive event for thread
187 self.thread.join() # wait until thread has finished
cliechti80a0ed12003-10-03 23:53:42 +0000188 self.thread = None
Chris Liechti4caf6a52015-08-04 01:07:45 +0200189
cliechti80a0ed12003-10-03 23:53:42 +0000190 def __set_properties(self):
191 # begin wxGlade: TerminalFrame.__set_properties
192 self.SetTitle("Serial Terminal")
193 self.SetSize((546, 383))
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200194 self.text_ctrl_output.SetFont(wx.Font(9, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, ""))
cliechti80a0ed12003-10-03 23:53:42 +0000195 # end wxGlade
196
197 def __do_layout(self):
198 # begin wxGlade: TerminalFrame.__do_layout
cliechtid5d51982008-04-10 23:48:55 +0000199 sizer_1 = wx.BoxSizer(wx.VERTICAL)
200 sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0)
cliechti80a0ed12003-10-03 23:53:42 +0000201 self.SetSizer(sizer_1)
202 self.Layout()
203 # end wxGlade
204
205 def __attach_events(self):
Chris Liechtia7069172015-09-20 20:47:01 +0200206 # register events at the controls
207 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
208 self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS)
209 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
210 self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS)
211 self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM)
212 self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey)
Chris Liechti9da0f0f2020-09-15 01:23:11 +0200213 self.Bind(wx.EVT_CHAR_HOOK, self.OnKey)
cliechtibc318d42004-11-13 03:13:12 +0000214 self.Bind(EVT_SERIALRX, self.OnSerialRead)
cliechtid5d51982008-04-10 23:48:55 +0000215 self.Bind(wx.EVT_CLOSE, self.OnClose)
cliechti80a0ed12003-10-03 23:53:42 +0000216
Chris Liechtibe00ee92015-09-12 02:02:04 +0200217 def OnExit(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000218 """Menu point Exit"""
219 self.Close()
220
221 def OnClose(self, event):
cliechti0eb86712003-10-04 00:49:04 +0000222 """Called on application shutdown."""
Chris Liechtibe00ee92015-09-12 02:02:04 +0200223 self.StopThread() # stop reader thread
224 self.serial.close() # cleanup
225 self.Destroy() # close windows, exit app
cliechti80a0ed12003-10-03 23:53:42 +0000226
Chris Liechtibe00ee92015-09-12 02:02:04 +0200227 def OnSaveAs(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000228 """Save contents of output window."""
Chris Liechtibe00ee92015-09-12 02:02:04 +0200229 with wx.FileDialog(
230 None,
231 "Save Text As...",
232 ".",
233 "",
234 "Text File|*.txt|All Files|*",
235 wx.SAVE) as dlg:
236 if dlg.ShowModal() == wx.ID_OK:
237 filename = dlg.GetPath()
Chris Liechtib88445d2015-10-04 23:49:28 +0200238 with codecs.open(filename, 'w', encoding='utf-8') as f:
239 text = self.text_ctrl_output.GetValue().encode("utf-8")
240 f.write(text)
Chris Liechtia7069172015-09-20 20:47:01 +0200241
Chris Liechtibe00ee92015-09-12 02:02:04 +0200242 def OnClear(self, event): # wxGlade: TerminalFrame.<event_handler>
cliechti80a0ed12003-10-03 23:53:42 +0000243 """Clear contents of output window."""
244 self.text_ctrl_output.Clear()
Chris Liechtia7069172015-09-20 20:47:01 +0200245
Chris Liechtibe00ee92015-09-12 02:02:04 +0200246 def OnPortSettings(self, event): # wxGlade: TerminalFrame.<event_handler>
Chris Liechtib88445d2015-10-04 23:49:28 +0200247 """
248 Show the port settings dialog. The reader thread is stopped for the
249 settings change.
250 """
Chris Liechtibe00ee92015-09-12 02:02:04 +0200251 if event is not None: # will be none when called on startup
cliechti0eb86712003-10-04 00:49:04 +0000252 self.StopThread()
253 self.serial.close()
254 ok = False
255 while not ok:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200256 with wxSerialConfigDialog.SerialConfigDialog(
Chris Liechtib88445d2015-10-04 23:49:28 +0200257 self,
Chris Liechtibe00ee92015-09-12 02:02:04 +0200258 -1,
259 "",
Chris Liechtia7069172015-09-20 20:47:01 +0200260 show=wxSerialConfigDialog.SHOW_BAUDRATE | wxSerialConfigDialog.SHOW_FORMAT | wxSerialConfigDialog.SHOW_FLOW,
Chris Liechtibe00ee92015-09-12 02:02:04 +0200261 serial=self.serial) as dialog_serial_cfg:
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200262 dialog_serial_cfg.CenterOnParent()
Chris Liechtibe00ee92015-09-12 02:02:04 +0200263 result = dialog_serial_cfg.ShowModal()
264 # open port if not called on startup, open it on startup and OK too
cliechtid5d51982008-04-10 23:48:55 +0000265 if result == wx.ID_OK or event is not None:
cliechti0eb86712003-10-04 00:49:04 +0000266 try:
267 self.serial.open()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200268 except serial.SerialException as e:
Chris Liechtib88445d2015-10-04 23:49:28 +0200269 with wx.MessageDialog(self, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)as dlg:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200270 dlg.ShowModal()
cliechti0eb86712003-10-04 00:49:04 +0000271 else:
272 self.StartThread()
Chris Liechti9a166662016-06-21 23:22:53 +0200273 self.SetTitle("Serial Terminal on {} [{},{},{},{}{}{}]".format(
274 self.serial.portstr,
275 self.serial.baudrate,
276 self.serial.bytesize,
277 self.serial.parity,
278 self.serial.stopbits,
279 ' RTS/CTS' if self.serial.rtscts else '',
280 ' Xon/Xoff' if self.serial.xonxoff else '',
281 ))
cliechti0eb86712003-10-04 00:49:04 +0000282 ok = True
283 else:
Chris Liechtibe00ee92015-09-12 02:02:04 +0200284 # on startup, dialog aborted
cliechtibc318d42004-11-13 03:13:12 +0000285 self.alive.clear()
cliechti0eb86712003-10-04 00:49:04 +0000286 ok = True
cliechti80a0ed12003-10-03 23:53:42 +0000287
Chris Liechtibe00ee92015-09-12 02:02:04 +0200288 def OnTermSettings(self, event): # wxGlade: TerminalFrame.<event_handler>
289 """\
290 Menu point Terminal Settings. Show the settings dialog
291 with the current terminal settings.
292 """
Chris Liechtib88445d2015-10-04 23:49:28 +0200293 with TerminalSettingsDialog(self, -1, "", settings=self.settings) as dialog:
Chris Liechtid7e9e9e2015-10-14 18:14:27 +0200294 dialog.CenterOnParent()
Chris Liechtia7069172015-09-20 20:47:01 +0200295 dialog.ShowModal()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200296
cliechti80a0ed12003-10-03 23:53:42 +0000297 def OnKey(self, event):
Chris Liechtibe00ee92015-09-12 02:02:04 +0200298 """\
Chris Liechtib88445d2015-10-04 23:49:28 +0200299 Key event handler. If the key is in the ASCII range, write it to the
Chris Liechtibe00ee92015-09-12 02:02:04 +0200300 serial port. Newline handling and local echo is also done here.
301 """
Chris Liechtib88445d2015-10-04 23:49:28 +0200302 code = event.GetUnicodeKey()
Chris Liechti9da0f0f2020-09-15 01:23:11 +0200303 # if code < 256: # XXX bug in some versions of wx returning only capital letters
304 # code = event.GetKeyCode()
Chris Liechtib88445d2015-10-04 23:49:28 +0200305 if code == 13: # is it a newline? (check for CR which is the RETURN key)
306 if self.settings.echo: # do echo if needed
307 self.text_ctrl_output.AppendText('\n')
308 if self.settings.newline == NEWLINE_CR:
309 self.serial.write(b'\r') # send CR
310 elif self.settings.newline == NEWLINE_LF:
311 self.serial.write(b'\n') # send LF
312 elif self.settings.newline == NEWLINE_CRLF:
313 self.serial.write(b'\r\n') # send CR+LF
cliechti0eb86712003-10-04 00:49:04 +0000314 else:
Chris Liechtib88445d2015-10-04 23:49:28 +0200315 char = unichr(code)
316 if self.settings.echo: # do echo if needed
317 self.WriteText(char)
318 self.serial.write(char.encode('UTF-8', 'replace')) # send the character
Chris Liechti9da0f0f2020-09-15 01:23:11 +0200319 event.StopPropagation()
cliechti80a0ed12003-10-03 23:53:42 +0000320
Chris Liechtib88445d2015-10-04 23:49:28 +0200321 def WriteText(self, text):
cliechti80a0ed12003-10-03 23:53:42 +0000322 if self.settings.unprintable:
Chris Liechtia7069172015-09-20 20:47:01 +0200323 text = ''.join([c if (c >= ' ' and c != '\x7f') else unichr(0x2400 + ord(c)) for c in text])
cliechti80a0ed12003-10-03 23:53:42 +0000324 self.text_ctrl_output.AppendText(text)
325
Chris Liechtib88445d2015-10-04 23:49:28 +0200326 def OnSerialRead(self, event):
327 """Handle input from the serial port."""
328 self.WriteText(event.data.decode('UTF-8', 'replace'))
329
cliechti80a0ed12003-10-03 23:53:42 +0000330 def ComPortThread(self):
Chris Liechtibe00ee92015-09-12 02:02:04 +0200331 """\
Chris Liechtib88445d2015-10-04 23:49:28 +0200332 Thread that handles the incoming traffic. Does the basic input
Chris Liechtibe00ee92015-09-12 02:02:04 +0200333 transformation (newlines) and generates an SerialRxEvent
334 """
335 while self.alive.isSet():
336 b = self.serial.read(self.serial.in_waiting or 1)
337 if b:
338 # newline transformation
cliechti80a0ed12003-10-03 23:53:42 +0000339 if self.settings.newline == NEWLINE_CR:
Chris Liechtib88445d2015-10-04 23:49:28 +0200340 b = b.replace(b'\r', b'\n')
cliechti80a0ed12003-10-03 23:53:42 +0000341 elif self.settings.newline == NEWLINE_LF:
342 pass
343 elif self.settings.newline == NEWLINE_CRLF:
Chris Liechtib88445d2015-10-04 23:49:28 +0200344 b = b.replace(b'\r\n', b'\n')
Chris Liechti9da0f0f2020-09-15 01:23:11 +0200345 wx.PostEvent(self, SerialRxEvent(data=b))
Chris Liechtibe00ee92015-09-12 02:02:04 +0200346
347 def OnRTS(self, event): # wxGlade: TerminalFrame.<event_handler>
348 self.serial.rts = event.IsChecked()
349
350 def OnDTR(self, event): # wxGlade: TerminalFrame.<event_handler>
Chris Liechti9da0f0f2020-09-15 01:23:11 +0200351 self.serial.dtr = event.IsChecked()
Chris Liechti4caf6a52015-08-04 01:07:45 +0200352
cliechti80a0ed12003-10-03 23:53:42 +0000353# end of class TerminalFrame
354
355
cliechtid5d51982008-04-10 23:48:55 +0000356class MyApp(wx.App):
cliechti80a0ed12003-10-03 23:53:42 +0000357 def OnInit(self):
cliechti80a0ed12003-10-03 23:53:42 +0000358 frame_terminal = TerminalFrame(None, -1, "")
359 self.SetTopWindow(frame_terminal)
Chris Liechtibe00ee92015-09-12 02:02:04 +0200360 frame_terminal.Show(True)
cliechti80a0ed12003-10-03 23:53:42 +0000361 return 1
362
363# end of class MyApp
364
365if __name__ == "__main__":
366 app = MyApp(0)
367 app.MainLoop()