cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 2 | # |
| 3 | # A serial port configuration dialog for wxPython. A number of flags can |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 4 | # be used to configure the fields that are displayed. |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 5 | # |
| 6 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
| 7 | # |
| 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 9 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 10 | import wx |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 11 | import serial |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 12 | import serial.tools.list_ports |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 13 | |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 14 | SHOW_BAUDRATE = 1 << 0 |
| 15 | SHOW_FORMAT = 1 << 1 |
| 16 | SHOW_FLOW = 1 << 2 |
| 17 | SHOW_TIMEOUT = 1 << 3 |
| 18 | SHOW_ALL = SHOW_BAUDRATE | SHOW_FORMAT | SHOW_FLOW | SHOW_TIMEOUT |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 19 | |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 20 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 21 | class SerialConfigDialog(wx.Dialog): |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 22 | """\ |
| 23 | Serial Port configuration dialog, to be used with pySerial 2.0+ |
| 24 | When instantiating a class of this dialog, then the "serial" keyword |
| 25 | argument is mandatory. It is a reference to a serial.Serial instance. |
| 26 | the optional "show" keyword argument can be used to show/hide different |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 27 | settings. The default is SHOW_ALL which corresponds to |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 28 | SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT. All constants can be |
| 29 | found in this module (not the class). |
| 30 | """ |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 31 | |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 32 | def __init__(self, *args, **kwds): |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 33 | # grab the serial keyword and remove it from the dict |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 34 | self.serial = kwds['serial'] |
| 35 | del kwds['serial'] |
| 36 | self.show = SHOW_ALL |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 37 | if 'show' in kwds: |
| 38 | self.show = kwds.pop('show') |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 39 | # begin wxGlade: SerialConfigDialog.__init__ |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 40 | kwds["style"] = wx.DEFAULT_DIALOG_STYLE |
| 41 | wx.Dialog.__init__(self, *args, **kwds) |
| 42 | self.label_2 = wx.StaticText(self, -1, "Port") |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 43 | self.choice_port = wx.Choice(self, -1, choices=[]) |
| 44 | self.label_1 = wx.StaticText(self, -1, "Baudrate") |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 45 | self.combo_box_baudrate = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 46 | self.sizer_1_staticbox = wx.StaticBox(self, -1, "Basics") |
| 47 | self.panel_format = wx.Panel(self, -1) |
| 48 | self.label_3 = wx.StaticText(self.panel_format, -1, "Data Bits") |
| 49 | self.choice_databits = wx.Choice(self.panel_format, -1, choices=["choice 1"]) |
| 50 | self.label_4 = wx.StaticText(self.panel_format, -1, "Stop Bits") |
| 51 | self.choice_stopbits = wx.Choice(self.panel_format, -1, choices=["choice 1"]) |
| 52 | self.label_5 = wx.StaticText(self.panel_format, -1, "Parity") |
| 53 | self.choice_parity = wx.Choice(self.panel_format, -1, choices=["choice 1"]) |
| 54 | self.sizer_format_staticbox = wx.StaticBox(self.panel_format, -1, "Data Format") |
| 55 | self.panel_timeout = wx.Panel(self, -1) |
| 56 | self.checkbox_timeout = wx.CheckBox(self.panel_timeout, -1, "Use Timeout") |
| 57 | self.text_ctrl_timeout = wx.TextCtrl(self.panel_timeout, -1, "") |
| 58 | self.label_6 = wx.StaticText(self.panel_timeout, -1, "seconds") |
| 59 | self.sizer_timeout_staticbox = wx.StaticBox(self.panel_timeout, -1, "Timeout") |
| 60 | self.panel_flow = wx.Panel(self, -1) |
| 61 | self.checkbox_rtscts = wx.CheckBox(self.panel_flow, -1, "RTS/CTS") |
| 62 | self.checkbox_xonxoff = wx.CheckBox(self.panel_flow, -1, "Xon/Xoff") |
| 63 | self.sizer_flow_staticbox = wx.StaticBox(self.panel_flow, -1, "Flow Control") |
| 64 | self.button_ok = wx.Button(self, wx.ID_OK, "") |
| 65 | self.button_cancel = wx.Button(self, wx.ID_CANCEL, "") |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 66 | |
| 67 | self.__set_properties() |
| 68 | self.__do_layout() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 69 | # end wxGlade |
| 70 | # attach the event handlers |
| 71 | self.__attach_events() |
| 72 | |
| 73 | def __set_properties(self): |
| 74 | # begin wxGlade: SerialConfigDialog.__set_properties |
| 75 | self.SetTitle("Serial Port Configuration") |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 76 | self.choice_databits.SetSelection(0) |
| 77 | self.choice_stopbits.SetSelection(0) |
| 78 | self.choice_parity.SetSelection(0) |
| 79 | self.text_ctrl_timeout.Enable(False) |
| 80 | self.button_ok.SetDefault() |
| 81 | # end wxGlade |
| 82 | self.SetTitle("Serial Port Configuration") |
| 83 | if self.show & SHOW_TIMEOUT: |
| 84 | self.text_ctrl_timeout.Enable(0) |
| 85 | self.button_ok.SetDefault() |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 86 | |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 87 | if not self.show & SHOW_BAUDRATE: |
| 88 | self.label_1.Hide() |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 89 | self.combo_box_baudrate.Hide() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 90 | if not self.show & SHOW_FORMAT: |
| 91 | self.panel_format.Hide() |
| 92 | if not self.show & SHOW_TIMEOUT: |
| 93 | self.panel_timeout.Hide() |
| 94 | if not self.show & SHOW_FLOW: |
| 95 | self.panel_flow.Hide() |
| 96 | |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 97 | # fill in ports and select current setting |
| 98 | preferred_index = 0 |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 99 | self.choice_port.Clear() |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 100 | self.ports = [] |
cliechti | f2f8b53 | 2014-08-03 17:31:09 +0000 | [diff] [blame] | 101 | for n, (portname, desc, hwid) in enumerate(sorted(serial.tools.list_ports.comports())): |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 102 | self.choice_port.Append('%s - %s' % (portname, desc)) |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 103 | self.ports.append(portname) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 104 | if self.serial.name == portname: |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 105 | preferred_index = n |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 106 | self.choice_port.SetSelection(preferred_index) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 107 | if self.show & SHOW_BAUDRATE: |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 108 | preferred_index = None |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 109 | # fill in baud rates and select current setting |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 110 | self.combo_box_baudrate.Clear() |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 111 | for n, baudrate in enumerate(self.serial.BAUDRATES): |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 112 | self.combo_box_baudrate.Append(str(baudrate)) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 113 | if self.serial.baudrate == baudrate: |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 114 | preferred_index = n |
| 115 | if preferred_index is not None: |
| 116 | self.combo_box_baudrate.SetSelection(preferred_index) |
| 117 | else: |
| 118 | self.combo_box_baudrate.SetValue(u'%d' % (self.serial.baudrate,)) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 119 | if self.show & SHOW_FORMAT: |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 120 | # fill in data bits and select current setting |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 121 | self.choice_databits.Clear() |
| 122 | for n, bytesize in enumerate(self.serial.BYTESIZES): |
| 123 | self.choice_databits.Append(str(bytesize)) |
| 124 | if self.serial.bytesize == bytesize: |
| 125 | index = n |
| 126 | self.choice_databits.SetSelection(index) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 127 | # fill in stop bits and select current setting |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 128 | self.choice_stopbits.Clear() |
| 129 | for n, stopbits in enumerate(self.serial.STOPBITS): |
| 130 | self.choice_stopbits.Append(str(stopbits)) |
| 131 | if self.serial.stopbits == stopbits: |
| 132 | index = n |
| 133 | self.choice_stopbits.SetSelection(index) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 134 | # fill in parities and select current setting |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 135 | self.choice_parity.Clear() |
| 136 | for n, parity in enumerate(self.serial.PARITIES): |
| 137 | self.choice_parity.Append(str(serial.PARITY_NAMES[parity])) |
| 138 | if self.serial.parity == parity: |
| 139 | index = n |
| 140 | self.choice_parity.SetSelection(index) |
| 141 | if self.show & SHOW_TIMEOUT: |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 142 | # set the timeout mode and value |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 143 | if self.serial.timeout is None: |
| 144 | self.checkbox_timeout.SetValue(False) |
| 145 | self.text_ctrl_timeout.Enable(False) |
| 146 | else: |
| 147 | self.checkbox_timeout.SetValue(True) |
| 148 | self.text_ctrl_timeout.Enable(True) |
| 149 | self.text_ctrl_timeout.SetValue(str(self.serial.timeout)) |
| 150 | if self.show & SHOW_FLOW: |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 151 | # set the rtscts mode |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 152 | self.checkbox_rtscts.SetValue(self.serial.rtscts) |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 153 | # set the rtscts mode |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 154 | self.checkbox_xonxoff.SetValue(self.serial.xonxoff) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 155 | |
| 156 | def __do_layout(self): |
| 157 | # begin wxGlade: SerialConfigDialog.__do_layout |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 158 | sizer_2 = wx.BoxSizer(wx.VERTICAL) |
| 159 | sizer_3 = wx.BoxSizer(wx.HORIZONTAL) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 160 | self.sizer_flow_staticbox.Lower() |
| 161 | sizer_flow = wx.StaticBoxSizer(self.sizer_flow_staticbox, wx.HORIZONTAL) |
| 162 | self.sizer_timeout_staticbox.Lower() |
| 163 | sizer_timeout = wx.StaticBoxSizer(self.sizer_timeout_staticbox, wx.HORIZONTAL) |
| 164 | self.sizer_format_staticbox.Lower() |
| 165 | sizer_format = wx.StaticBoxSizer(self.sizer_format_staticbox, wx.VERTICAL) |
| 166 | grid_sizer_1 = wx.FlexGridSizer(3, 2, 0, 0) |
| 167 | self.sizer_1_staticbox.Lower() |
| 168 | sizer_1 = wx.StaticBoxSizer(self.sizer_1_staticbox, wx.VERTICAL) |
| 169 | sizer_basics = wx.FlexGridSizer(3, 2, 0, 0) |
| 170 | sizer_basics.Add(self.label_2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 171 | sizer_basics.Add(self.choice_port, 0, wx.EXPAND, 0) |
| 172 | sizer_basics.Add(self.label_1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 173 | sizer_basics.Add(self.combo_box_baudrate, 0, wx.EXPAND, 0) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 174 | sizer_basics.AddGrowableCol(1) |
| 175 | sizer_1.Add(sizer_basics, 0, wx.EXPAND, 0) |
| 176 | sizer_2.Add(sizer_1, 0, wx.EXPAND, 0) |
| 177 | grid_sizer_1.Add(self.label_3, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 178 | grid_sizer_1.Add(self.choice_databits, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) |
| 179 | grid_sizer_1.Add(self.label_4, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 180 | grid_sizer_1.Add(self.choice_stopbits, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) |
| 181 | grid_sizer_1.Add(self.label_5, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 182 | grid_sizer_1.Add(self.choice_parity, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) |
| 183 | sizer_format.Add(grid_sizer_1, 1, wx.EXPAND, 0) |
| 184 | self.panel_format.SetSizer(sizer_format) |
| 185 | sizer_2.Add(self.panel_format, 0, wx.EXPAND, 0) |
| 186 | sizer_timeout.Add(self.checkbox_timeout, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 187 | sizer_timeout.Add(self.text_ctrl_timeout, 0, 0, 0) |
| 188 | sizer_timeout.Add(self.label_6, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 189 | self.panel_timeout.SetSizer(sizer_timeout) |
| 190 | sizer_2.Add(self.panel_timeout, 0, wx.EXPAND, 0) |
| 191 | sizer_flow.Add(self.checkbox_rtscts, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 192 | sizer_flow.Add(self.checkbox_xonxoff, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) |
| 193 | sizer_flow.Add((10, 10), 1, wx.EXPAND, 0) |
| 194 | self.panel_flow.SetSizer(sizer_flow) |
| 195 | sizer_2.Add(self.panel_flow, 0, wx.EXPAND, 0) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 196 | sizer_3.Add(self.button_ok, 0, 0, 0) |
| 197 | sizer_3.Add(self.button_cancel, 0, 0, 0) |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 198 | sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 199 | self.SetSizer(sizer_2) |
| 200 | sizer_2.Fit(self) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 201 | self.Layout() |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 202 | # end wxGlade |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 203 | |
| 204 | def __attach_events(self): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 205 | wx.EVT_BUTTON(self, self.button_ok.GetId(), self.OnOK) |
| 206 | wx.EVT_BUTTON(self, self.button_cancel.GetId(), self.OnCancel) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 207 | if self.show & SHOW_TIMEOUT: |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 208 | wx.EVT_CHECKBOX(self, self.checkbox_timeout.GetId(), self.OnTimeout) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 209 | |
| 210 | def OnOK(self, events): |
| 211 | success = True |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 212 | self.serial.port = self.ports[self.choice_port.GetSelection()] |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 213 | if self.show & SHOW_BAUDRATE: |
Chris Liechti | 809f8f2 | 2015-10-15 22:44:59 +0200 | [diff] [blame^] | 214 | #~ self.serial.baudrate = self.serial.BAUDRATES[self.combo_box_baudrate.GetSelection()] |
| 215 | try: |
| 216 | b = int(self.combo_box_baudrate.GetValue()) |
| 217 | except ValueError: |
| 218 | with wx.MessageDialog( |
| 219 | self, |
| 220 | 'Baudrate must be a numeric value', |
| 221 | 'Value Error', |
| 222 | wx.OK | wx.ICON_ERROR) as dlg: |
| 223 | dlg.ShowModal() |
| 224 | success = False |
| 225 | else: |
| 226 | self.serial.baudrate = b |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 227 | if self.show & SHOW_FORMAT: |
| 228 | self.serial.bytesize = self.serial.BYTESIZES[self.choice_databits.GetSelection()] |
| 229 | self.serial.stopbits = self.serial.STOPBITS[self.choice_stopbits.GetSelection()] |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 230 | self.serial.parity = self.serial.PARITIES[self.choice_parity.GetSelection()] |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 231 | if self.show & SHOW_FLOW: |
Chris Liechti | 6dcdeda | 2015-09-29 01:03:03 +0200 | [diff] [blame] | 232 | self.serial.rtscts = self.checkbox_rtscts.GetValue() |
| 233 | self.serial.xonxoff = self.checkbox_xonxoff.GetValue() |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 234 | if self.show & SHOW_TIMEOUT: |
| 235 | if self.checkbox_timeout.GetValue(): |
| 236 | try: |
| 237 | self.serial.timeout = float(self.text_ctrl_timeout.GetValue()) |
| 238 | except ValueError: |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 239 | with wx.MessageDialog( |
| 240 | self, |
| 241 | 'Timeout must be a numeric value', |
| 242 | 'Value Error', |
| 243 | wx.OK | wx.ICON_ERROR) as dlg: |
| 244 | dlg.ShowModal() |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 245 | success = False |
| 246 | else: |
| 247 | self.serial.timeout = None |
| 248 | if success: |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 249 | self.EndModal(wx.ID_OK) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 250 | |
| 251 | def OnCancel(self, events): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 252 | self.EndModal(wx.ID_CANCEL) |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 253 | |
| 254 | def OnTimeout(self, events): |
| 255 | if self.checkbox_timeout.GetValue(): |
| 256 | self.text_ctrl_timeout.Enable(True) |
| 257 | else: |
| 258 | self.text_ctrl_timeout.Enable(False) |
| 259 | |
| 260 | # end of class SerialConfigDialog |
| 261 | |
| 262 | |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 263 | class MyApp(wx.App): |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 264 | """Test code""" |
| 265 | def OnInit(self): |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 266 | wx.InitAllImageHandlers() |
cliechti | 24c3a88 | 2014-08-01 03:36:27 +0000 | [diff] [blame] | 267 | |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 268 | ser = serial.Serial() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 269 | print(ser) |
| 270 | # loop until cancel is pressed, old values are used as start for the next run |
| 271 | # show the different views, one after the other |
| 272 | # value are kept. |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 273 | for flags in (SHOW_BAUDRATE, SHOW_FLOW, SHOW_FORMAT, SHOW_TIMEOUT, SHOW_ALL): |
| 274 | dialog_serial_cfg = SerialConfigDialog(None, -1, "", serial=ser, show=flags) |
| 275 | self.SetTopWindow(dialog_serial_cfg) |
| 276 | result = dialog_serial_cfg.ShowModal() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 277 | print(ser) |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 278 | if result != wx.ID_OK: |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 279 | break |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 280 | # the user can play around with the values, CANCEL aborts the loop |
Chris Liechti | be00ee9 | 2015-09-12 02:02:04 +0200 | [diff] [blame] | 281 | while True: |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 282 | dialog_serial_cfg = SerialConfigDialog(None, -1, "", serial=ser) |
| 283 | self.SetTopWindow(dialog_serial_cfg) |
| 284 | result = dialog_serial_cfg.ShowModal() |
Chris Liechti | 4caf6a5 | 2015-08-04 01:07:45 +0200 | [diff] [blame] | 285 | print(ser) |
cliechti | d5d5198 | 2008-04-10 23:48:55 +0000 | [diff] [blame] | 286 | if result != wx.ID_OK: |
cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame] | 287 | break |
| 288 | return 0 |
| 289 | |
| 290 | # end of class MyApp |
| 291 | |
| 292 | if __name__ == "__main__": |
| 293 | app = MyApp(0) |
| 294 | app.MainLoop() |