cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 2 | # |
| 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 |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 8 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 9 | #from wxPython.wx import * |
| 10 | import wx |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 11 | import wxSerialConfigDialog |
| 12 | import serial |
| 13 | import threading |
| 14 | |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 15 | #---------------------------------------------------------------------- |
| 16 | # 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 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 21 | SERIALRX = wx.NewEventType() |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 22 | # bind to serial data receive events |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 23 | EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0) |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 24 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 25 | class SerialRxEvent(wx.PyCommandEvent): |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 26 | eventType = SERIALRX |
| 27 | def __init__(self, windowID, data): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 28 | wx.PyCommandEvent.__init__(self, self.eventType, windowID) |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 29 | self.data = data |
| 30 | |
| 31 | def Clone(self): |
| 32 | self.__class__(self.GetId(), self.data) |
| 33 | |
| 34 | #---------------------------------------------------------------------- |
| 35 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 36 | ID_CLEAR = wx.NewId() |
| 37 | ID_SAVEAS = wx.NewId() |
| 38 | ID_SETTINGS = wx.NewId() |
| 39 | ID_TERM = wx.NewId() |
| 40 | ID_EXIT = wx.NewId() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 41 | ID_RTS = wx.NewId() |
| 42 | ID_DTR = wx.NewId() |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 43 | |
| 44 | NEWLINE_CR = 0 |
| 45 | NEWLINE_LF = 1 |
| 46 | NEWLINE_CRLF = 2 |
| 47 | |
| 48 | class TerminalSetup: |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 49 | """Placeholder for various terminal settings. Used to pass the |
| 50 | options to the TerminalSettingsDialog.""" |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 51 | def __init__(self): |
| 52 | self.echo = False |
| 53 | self.unprintable = False |
| 54 | self.newline = NEWLINE_CRLF |
| 55 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 56 | class TerminalSettingsDialog(wx.Dialog): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 57 | """Simple dialog with common terminal settings like echo, newline mode.""" |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 58 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 59 | def __init__(self, *args, **kwds): |
| 60 | self.settings = kwds['settings'] |
| 61 | del kwds['settings'] |
| 62 | # begin wxGlade: TerminalSettingsDialog.__init__ |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 63 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
| 64 | wx.Dialog.__init__(self, *args, **kwds) |
| 65 | self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo") |
| 66 | self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters") |
| 67 | 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 Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 68 | self.sizer_4_staticbox = wx.StaticBox(self, -1, "Input/Output") |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 69 | self.button_ok = wx.Button(self, -1, "OK") |
| 70 | self.button_cancel = wx.Button(self, -1, "Cancel") |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 71 | |
| 72 | self.__set_properties() |
| 73 | self.__do_layout() |
| 74 | # end wxGlade |
| 75 | self.__attach_events() |
| 76 | self.checkbox_echo.SetValue(self.settings.echo) |
| 77 | self.checkbox_unprintable.SetValue(self.settings.unprintable) |
| 78 | self.radio_box_newline.SetSelection(self.settings.newline) |
| 79 | |
| 80 | def __set_properties(self): |
| 81 | # begin wxGlade: TerminalSettingsDialog.__set_properties |
| 82 | self.SetTitle("Terminal Settings") |
| 83 | self.radio_box_newline.SetSelection(0) |
| 84 | self.button_ok.SetDefault() |
| 85 | # end wxGlade |
| 86 | |
| 87 | def __do_layout(self): |
| 88 | # begin wxGlade: TerminalSettingsDialog.__do_layout |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 89 | sizer_2 = wx.BoxSizer(wx.VERTICAL) |
| 90 | sizer_3 = wx.BoxSizer(wx.HORIZONTAL) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 91 | self.sizer_4_staticbox.Lower() |
| 92 | sizer_4 = wx.StaticBoxSizer(self.sizer_4_staticbox, wx.VERTICAL) |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 93 | sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4) |
| 94 | sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 95 | sizer_4.Add(self.radio_box_newline, 0, 0, 0) |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 96 | sizer_2.Add(sizer_4, 0, wx.EXPAND, 0) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 97 | sizer_3.Add(self.button_ok, 0, 0, 0) |
| 98 | sizer_3.Add(self.button_cancel, 0, 0, 0) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 99 | sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 100 | self.SetSizer(sizer_2) |
| 101 | sizer_2.Fit(self) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 102 | self.Layout() |
| 103 | # end wxGlade |
| 104 | |
| 105 | def __attach_events(self): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 106 | self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.button_ok.GetId()) |
| 107 | self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.button_cancel.GetId()) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 108 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 109 | def OnOK(self, events): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 110 | """Update data wil new values and close dialog.""" |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 111 | self.settings.echo = self.checkbox_echo.GetValue() |
| 112 | self.settings.unprintable = self.checkbox_unprintable.GetValue() |
| 113 | self.settings.newline = self.radio_box_newline.GetSelection() |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 114 | self.EndModal(wx.ID_OK) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 115 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 116 | def OnCancel(self, events): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 117 | """Do not update data but close dialog.""" |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 118 | self.EndModal(wx.ID_CANCEL) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 119 | |
| 120 | # end of class TerminalSettingsDialog |
| 121 | |
| 122 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 123 | class TerminalFrame(wx.Frame): |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 124 | """Simple terminal program for wxPython""" |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 125 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 126 | def __init__(self, *args, **kwds): |
| 127 | self.serial = serial.Serial() |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 128 | self.serial.timeout = 0.5 #make sure that the alive event can be checked from time to time |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 129 | self.settings = TerminalSetup() #placeholder for the settings |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 130 | self.thread = None |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 131 | self.alive = threading.Event() |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 132 | # begin wxGlade: TerminalFrame.__init__ |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 133 | kwds["style"] = wx.DEFAULT_FRAME_STYLE |
| 134 | wx.Frame.__init__(self, *args, **kwds) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 135 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 136 | # Menu Bar |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 137 | self.frame_terminal_menubar = wx.MenuBar() |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 138 | wxglade_tmp_menu = wx.Menu() |
| 139 | wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL) |
| 140 | wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 141 | wxglade_tmp_menu.AppendSeparator() |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 142 | wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 143 | wxglade_tmp_menu.AppendSeparator() |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 144 | wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 145 | self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File") |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 146 | wxglade_tmp_menu = wx.Menu() |
| 147 | wxglade_tmp_menu.Append(ID_RTS, "RTS", "", wx.ITEM_CHECK) |
| 148 | wxglade_tmp_menu.Append(ID_DTR, "&DTR", "", wx.ITEM_CHECK) |
| 149 | wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL) |
| 150 | self.frame_terminal_menubar.Append(wxglade_tmp_menu, "Serial Port") |
| 151 | self.SetMenuBar(self.frame_terminal_menubar) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 152 | # Menu Bar end |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 153 | self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 154 | |
| 155 | self.__set_properties() |
| 156 | self.__do_layout() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 157 | |
| 158 | self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR) |
| 159 | self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS) |
| 160 | self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM) |
| 161 | self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT) |
| 162 | self.Bind(wx.EVT_MENU, self.OnRTS, id=ID_RTS) |
| 163 | self.Bind(wx.EVT_MENU, self.OnDTR, id=ID_DTR) |
| 164 | self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 165 | # end wxGlade |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 166 | self.__attach_events() #register events |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 167 | self.OnPortSettings(None) #call setup dialog on startup, opens port |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 168 | if not self.alive.isSet(): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 169 | self.Close() |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 170 | |
| 171 | def StartThread(self): |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 172 | """Start the receiver thread""" |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 173 | self.thread = threading.Thread(target=self.ComPortThread) |
| 174 | self.thread.setDaemon(1) |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 175 | self.alive.set() |
cliechti | f777669 | 2005-10-02 21:51:42 +0000 | [diff] [blame] | 176 | self.thread.start() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 177 | self.serial.rts = True |
| 178 | self.serial.dtr = True |
| 179 | self.frame_terminal_menubar.Check(ID_RTS, self.serial.rts) |
| 180 | self.frame_terminal_menubar.Check(ID_DTR, self.serial.dtr) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 181 | |
| 182 | def StopThread(self): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 183 | """Stop the receiver thread, wait util it's finished.""" |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 184 | if self.thread is not None: |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 185 | self.alive.clear() #clear alive event for thread |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 186 | self.thread.join() #wait until thread has finished |
| 187 | self.thread = None |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 188 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 189 | def __set_properties(self): |
| 190 | # begin wxGlade: TerminalFrame.__set_properties |
| 191 | self.SetTitle("Serial Terminal") |
| 192 | self.SetSize((546, 383)) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 193 | self.text_ctrl_output.SetFont(wx.Font(9, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "")) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 194 | # end wxGlade |
| 195 | |
| 196 | def __do_layout(self): |
| 197 | # begin wxGlade: TerminalFrame.__do_layout |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 198 | sizer_1 = wx.BoxSizer(wx.VERTICAL) |
| 199 | sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 200 | self.SetSizer(sizer_1) |
| 201 | self.Layout() |
| 202 | # end wxGlade |
| 203 | |
| 204 | def __attach_events(self): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 205 | #register events at the controls |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 206 | self.Bind(wx.EVT_MENU, self.OnClear, id = ID_CLEAR) |
| 207 | self.Bind(wx.EVT_MENU, self.OnSaveAs, id = ID_SAVEAS) |
| 208 | self.Bind(wx.EVT_MENU, self.OnExit, id = ID_EXIT) |
| 209 | self.Bind(wx.EVT_MENU, self.OnPortSettings, id = ID_SETTINGS) |
| 210 | self.Bind(wx.EVT_MENU, self.OnTermSettings, id = ID_TERM) |
| 211 | self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey) |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 212 | self.Bind(EVT_SERIALRX, self.OnSerialRead) |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 213 | self.Bind(wx.EVT_CLOSE, self.OnClose) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 214 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 215 | def OnExit(self, event): # wxGlade: TerminalFrame.<event_handler> |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 216 | """Menu point Exit""" |
| 217 | self.Close() |
| 218 | |
| 219 | def OnClose(self, event): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 220 | """Called on application shutdown.""" |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 221 | self.StopThread() # stop reader thread |
| 222 | self.serial.close() # cleanup |
| 223 | self.Destroy() # close windows, exit app |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 224 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 225 | def OnSaveAs(self, event): # wxGlade: TerminalFrame.<event_handler> |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 226 | """Save contents of output window.""" |
| 227 | filename = None |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 228 | with wx.FileDialog( |
| 229 | None, |
| 230 | "Save Text As...", |
| 231 | ".", |
| 232 | "", |
| 233 | "Text File|*.txt|All Files|*", |
| 234 | wx.SAVE) as dlg: |
| 235 | if dlg.ShowModal() == wx.ID_OK: |
| 236 | filename = dlg.GetPath() |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 237 | |
| 238 | if filename is not None: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 239 | with file(filename, 'w') as f: |
| 240 | text = self.text_ctrl_output.GetValue() |
| 241 | if type(text) == unicode: |
| 242 | text = text.encode("latin1") # hm, is that a good asumption? |
| 243 | f.write(text) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 244 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 245 | def OnClear(self, event): # wxGlade: TerminalFrame.<event_handler> |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 246 | """Clear contents of output window.""" |
| 247 | self.text_ctrl_output.Clear() |
| 248 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 249 | def OnPortSettings(self, event): # wxGlade: TerminalFrame.<event_handler> |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 250 | """Show the portsettings dialog. The reader thread is stopped for the |
| 251 | settings change.""" |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 252 | if event is not None: # will be none when called on startup |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 253 | self.StopThread() |
| 254 | self.serial.close() |
| 255 | ok = False |
| 256 | while not ok: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 257 | with wxSerialConfigDialog.SerialConfigDialog( |
| 258 | None, |
| 259 | -1, |
| 260 | "", |
| 261 | show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW, |
| 262 | serial=self.serial) as dialog_serial_cfg: |
| 263 | result = dialog_serial_cfg.ShowModal() |
| 264 | # open port if not called on startup, open it on startup and OK too |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 265 | if result == wx.ID_OK or event is not None: |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 266 | try: |
| 267 | self.serial.open() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 268 | except serial.SerialException as e: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 269 | with wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)as dlg: |
| 270 | dlg.ShowModal() |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 271 | else: |
| 272 | self.StartThread() |
| 273 | self.SetTitle("Serial Terminal on %s [%s, %s%s%s%s%s]" % ( |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 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 | )) |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 282 | ok = True |
| 283 | else: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 284 | # on startup, dialog aborted |
cliechti | bc318d4 | 2004-11-13 03:13:12 +0000 | [diff] [blame] | 285 | self.alive.clear() |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 286 | ok = True |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 287 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 288 | 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 | """ |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 293 | dialog = TerminalSettingsDialog(None, -1, "", settings=self.settings) |
| 294 | result = dialog.ShowModal() |
| 295 | dialog.Destroy() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 296 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 297 | def OnKey(self, event): |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 298 | """\ |
| 299 | Key event handler. if the key is in the ASCII range, write it to the |
| 300 | serial port. Newline handling and local echo is also done here. |
| 301 | """ |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 302 | code = event.GetKeyCode() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 303 | if code < 256: # is it printable? |
| 304 | if code == 13: # is it a newline? (check for CR which is the RETURN key) |
| 305 | if self.settings.echo: # do echo if needed |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 306 | self.text_ctrl_output.AppendText('\n') |
| 307 | if self.settings.newline == NEWLINE_CR: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 308 | self.serial.write('\r') # send CR |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 309 | elif self.settings.newline == NEWLINE_LF: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 310 | self.serial.write('\n') # send LF |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 311 | elif self.settings.newline == NEWLINE_CRLF: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 312 | self.serial.write('\r\n') # send CR+LF |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 313 | else: |
| 314 | char = chr(code) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 315 | if self.settings.echo: # do echo if needed |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 316 | self.text_ctrl_output.WriteText(char) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 317 | self.serial.write(char) # send the charcater |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 318 | else: |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 319 | print("Extra Key:", code) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 320 | |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 321 | def OnSerialRead(self, event): |
cliechti | 0eb8671 | 2003-10-04 00:49:04 +0000 | [diff] [blame] | 322 | """Handle input from the serial port.""" |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 323 | text = event.data |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 324 | if self.settings.unprintable: |
| 325 | text = ''.join([(c >= ' ') and c or '<%d>' % ord(c) for c in text]) |
| 326 | self.text_ctrl_output.AppendText(text) |
| 327 | |
| 328 | def ComPortThread(self): |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 329 | """\ |
| 330 | Thread that handles the incomming traffic. Does the basic input |
| 331 | transformation (newlines) and generates an SerialRxEvent |
| 332 | """ |
| 333 | while self.alive.isSet(): |
| 334 | b = self.serial.read(self.serial.in_waiting or 1) |
| 335 | if b: |
| 336 | # newline transformation |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 337 | if self.settings.newline == NEWLINE_CR: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 338 | b = b.replace('\r', '\n') |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 339 | elif self.settings.newline == NEWLINE_LF: |
| 340 | pass |
| 341 | elif self.settings.newline == NEWLINE_CRLF: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 342 | b = b.replace('\r\n', '\n') |
| 343 | event = SerialRxEvent(self.GetId(), b) |
cliechti | 40d71f6 | 2004-07-09 22:14:17 +0000 | [diff] [blame] | 344 | self.GetEventHandler().AddPendingEvent(event) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 345 | #~ self.OnSerialRead(text) # output text in window |
| 346 | |
| 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> |
| 351 | self.serial.dtr = event.Checked() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 352 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 353 | # end of class TerminalFrame |
| 354 | |
| 355 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 356 | class MyApp(wx.App): |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 357 | def OnInit(self): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 358 | wx.InitAllImageHandlers() |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 359 | frame_terminal = TerminalFrame(None, -1, "") |
| 360 | self.SetTopWindow(frame_terminal) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 361 | frame_terminal.Show(True) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 362 | return 1 |
| 363 | |
| 364 | # end of class MyApp |
| 365 | |
| 366 | if __name__ == "__main__": |
| 367 | app = MyApp(0) |
| 368 | app.MainLoop() |