diff options
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/wxSerialConfigDialog.py | 201 | ||||
-rw-r--r-- | examples/wxSerialConfigDialog.wxg | 293 | ||||
-rwxr-xr-x | examples/wxTerminal.py | 178 | ||||
-rw-r--r-- | examples/wxTerminal.wxg | 61 |
4 files changed, 400 insertions, 333 deletions
diff --git a/examples/wxSerialConfigDialog.py b/examples/wxSerialConfigDialog.py index 3a2a83d..7ceecad 100755 --- a/examples/wxSerialConfigDialog.py +++ b/examples/wxSerialConfigDialog.py @@ -19,13 +19,15 @@ SHOW_ALL = SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT class SerialConfigDialog(wx.Dialog): - """Serial Port configuration dialog, to be used with pySerial 2.0+ - When instantiating a class of this dialog, then the "serial" keyword - argument is mandatory. It is a reference to a serial.Serial instance. - the optional "show" keyword argument can be used to show/hide different - settings. The default is SHOW_ALL which corresponds to - SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT. All constants can be - found in this module (not the class).""" + """\ + Serial Port configuration dialog, to be used with pySerial 2.0+ + When instantiating a class of this dialog, then the "serial" keyword + argument is mandatory. It is a reference to a serial.Serial instance. + the optional "show" keyword argument can be used to show/hide different + settings. The default is SHOW_ALL which corresponds to + SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT. All constants can be + found in this module (not the class). + """ def __init__(self, *args, **kwds): # grab the serial keyword and remove it from the dict @@ -36,43 +38,74 @@ class SerialConfigDialog(wx.Dialog): self.show = kwds['show'] del kwds['show'] # begin wxGlade: SerialConfigDialog.__init__ - # end wxGlade kwds["style"] = wx.DEFAULT_DIALOG_STYLE wx.Dialog.__init__(self, *args, **kwds) self.label_2 = wx.StaticText(self, -1, "Port") - self.combo_box_port = wx.ComboBox(self, -1, choices=["dummy1", "dummy2", "dummy3", "dummy4", "dummy5"], style=wx.CB_DROPDOWN) - if self.show & SHOW_BAUDRATE: - self.label_1 = wx.StaticText(self, -1, "Baudrate") - self.choice_baudrate = wx.Choice(self, -1, choices=["choice 1"]) - if self.show & SHOW_FORMAT: - self.label_3 = wx.StaticText(self, -1, "Data Bits") - self.choice_databits = wx.Choice(self, -1, choices=["choice 1"]) - self.label_4 = wx.StaticText(self, -1, "Stop Bits") - self.choice_stopbits = wx.Choice(self, -1, choices=["choice 1"]) - self.label_5 = wx.StaticText(self, -1, "Parity") - self.choice_parity = wx.Choice(self, -1, choices=["choice 1"]) - if self.show & SHOW_TIMEOUT: - self.checkbox_timeout = wx.CheckBox(self, -1, "Use Timeout") - self.text_ctrl_timeout = wx.TextCtrl(self, -1, "") - self.label_6 = wx.StaticText(self, -1, "seconds") - if self.show & SHOW_FLOW: - self.checkbox_rtscts = wx.CheckBox(self, -1, "RTS/CTS") - self.checkbox_xonxoff = wx.CheckBox(self, -1, "Xon/Xoff") - self.button_ok = wx.Button(self, -1, "OK") - self.button_cancel = wx.Button(self, -1, "Cancel") + self.choice_port = wx.Choice(self, -1, choices=[]) + self.label_1 = wx.StaticText(self, -1, "Baudrate") + self.choice_baudrate = wx.Choice(self, -1, choices=["choice 1"]) + self.sizer_1_staticbox = wx.StaticBox(self, -1, "Basics") + self.panel_format = wx.Panel(self, -1) + self.label_3 = wx.StaticText(self.panel_format, -1, "Data Bits") + self.choice_databits = wx.Choice(self.panel_format, -1, choices=["choice 1"]) + self.label_4 = wx.StaticText(self.panel_format, -1, "Stop Bits") + self.choice_stopbits = wx.Choice(self.panel_format, -1, choices=["choice 1"]) + self.label_5 = wx.StaticText(self.panel_format, -1, "Parity") + self.choice_parity = wx.Choice(self.panel_format, -1, choices=["choice 1"]) + self.sizer_format_staticbox = wx.StaticBox(self.panel_format, -1, "Data Format") + self.panel_timeout = wx.Panel(self, -1) + self.checkbox_timeout = wx.CheckBox(self.panel_timeout, -1, "Use Timeout") + self.text_ctrl_timeout = wx.TextCtrl(self.panel_timeout, -1, "") + self.label_6 = wx.StaticText(self.panel_timeout, -1, "seconds") + self.sizer_timeout_staticbox = wx.StaticBox(self.panel_timeout, -1, "Timeout") + self.panel_flow = wx.Panel(self, -1) + self.checkbox_rtscts = wx.CheckBox(self.panel_flow, -1, "RTS/CTS") + self.checkbox_xonxoff = wx.CheckBox(self.panel_flow, -1, "Xon/Xoff") + self.sizer_flow_staticbox = wx.StaticBox(self.panel_flow, -1, "Flow Control") + self.button_ok = wx.Button(self, wx.ID_OK, "") + self.button_cancel = wx.Button(self, wx.ID_CANCEL, "") self.__set_properties() self.__do_layout() + # end wxGlade + # attach the event handlers + self.__attach_events() + + def __set_properties(self): + # begin wxGlade: SerialConfigDialog.__set_properties + self.SetTitle("Serial Port Configuration") + self.choice_baudrate.SetSelection(0) + self.choice_databits.SetSelection(0) + self.choice_stopbits.SetSelection(0) + self.choice_parity.SetSelection(0) + self.text_ctrl_timeout.Enable(False) + self.button_ok.SetDefault() + # end wxGlade + self.SetTitle("Serial Port Configuration") + if self.show & SHOW_TIMEOUT: + self.text_ctrl_timeout.Enable(0) + self.button_ok.SetDefault() + + if not self.show & SHOW_BAUDRATE: + self.label_1.Hide() + self.choice_baudrate.Hide() + if not self.show & SHOW_FORMAT: + self.panel_format.Hide() + if not self.show & SHOW_TIMEOUT: + self.panel_timeout.Hide() + if not self.show & SHOW_FLOW: + self.panel_flow.Hide() + # fill in ports and select current setting preferred_index = 0 - self.combo_box_port.Clear() + self.choice_port.Clear() self.ports = [] for n, (portname, desc, hwid) in enumerate(sorted(serial.tools.list_ports.comports())): - self.combo_box_port.Append('%s (%s [%s])' % (portname, desc, hwid)) + self.choice_port.Append('%s - %s' % (portname, desc)) self.ports.append(portname) - if self.serial.portstr == portname: + if self.serial.name == portname: preferred_index = n - self.combo_box_port.SetSelection(preferred_index) + self.choice_port.SetSelection(preferred_index) if self.show & SHOW_BAUDRATE: # fill in baud rates and select current setting self.choice_baudrate.Clear() @@ -117,68 +150,54 @@ class SerialConfigDialog(wx.Dialog): self.checkbox_rtscts.SetValue(self.serial.rtscts) # set the rtscts mode self.checkbox_xonxoff.SetValue(self.serial.xonxoff) - # attach the event handlers - self.__attach_events() - - def __set_properties(self): - # begin wxGlade: SerialConfigDialog.__set_properties - # end wxGlade - self.SetTitle("Serial Port Configuration") - if self.show & SHOW_TIMEOUT: - self.text_ctrl_timeout.Enable(0) - self.button_ok.SetDefault() def __do_layout(self): # begin wxGlade: SerialConfigDialog.__do_layout - # end wxGlade sizer_2 = wx.BoxSizer(wx.VERTICAL) sizer_3 = wx.BoxSizer(wx.HORIZONTAL) - sizer_basics = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Basics"), wx.VERTICAL) - sizer_5 = wx.BoxSizer(wx.HORIZONTAL) - sizer_5.Add(self.label_2, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_5.Add(self.combo_box_port, 1, 0, 0) - sizer_basics.Add(sizer_5, 0, wx.RIGHT|wx.EXPAND, 0) - if self.show & SHOW_BAUDRATE: - sizer_baudrate = wx.BoxSizer(wx.HORIZONTAL) - sizer_baudrate.Add(self.label_1, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_baudrate.Add(self.choice_baudrate, 1, wx.ALIGN_RIGHT, 0) - sizer_basics.Add(sizer_baudrate, 0, wx.EXPAND, 0) - sizer_2.Add(sizer_basics, 0, wx.EXPAND, 0) - if self.show & SHOW_FORMAT: - sizer_8 = wx.BoxSizer(wx.HORIZONTAL) - sizer_7 = wx.BoxSizer(wx.HORIZONTAL) - sizer_6 = wx.BoxSizer(wx.HORIZONTAL) - sizer_format = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Data Format"), wx.VERTICAL) - sizer_6.Add(self.label_3, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_6.Add(self.choice_databits, 1, wx.ALIGN_RIGHT, 0) - sizer_format.Add(sizer_6, 0, wx.EXPAND, 0) - sizer_7.Add(self.label_4, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_7.Add(self.choice_stopbits, 1, wx.ALIGN_RIGHT, 0) - sizer_format.Add(sizer_7, 0, wx.EXPAND, 0) - sizer_8.Add(self.label_5, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_8.Add(self.choice_parity, 1, wx.ALIGN_RIGHT, 0) - sizer_format.Add(sizer_8, 0, wx.EXPAND, 0) - sizer_2.Add(sizer_format, 0, wx.EXPAND, 0) - if self.show & SHOW_TIMEOUT: - sizer_timeout = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Timeout"), wx.HORIZONTAL) - sizer_timeout.Add(self.checkbox_timeout, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_timeout.Add(self.text_ctrl_timeout, 0, 0, 0) - sizer_timeout.Add(self.label_6, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_2.Add(sizer_timeout, 0, 0, 0) - if self.show & SHOW_FLOW: - sizer_flow = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Flow Control"), wx.HORIZONTAL) - sizer_flow.Add(self.checkbox_rtscts, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_flow.Add(self.checkbox_xonxoff, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4) - sizer_flow.Add((10,10), 1, wx.EXPAND, 0) - sizer_2.Add(sizer_flow, 0, wx.EXPAND, 0) + self.sizer_flow_staticbox.Lower() + sizer_flow = wx.StaticBoxSizer(self.sizer_flow_staticbox, wx.HORIZONTAL) + self.sizer_timeout_staticbox.Lower() + sizer_timeout = wx.StaticBoxSizer(self.sizer_timeout_staticbox, wx.HORIZONTAL) + self.sizer_format_staticbox.Lower() + sizer_format = wx.StaticBoxSizer(self.sizer_format_staticbox, wx.VERTICAL) + grid_sizer_1 = wx.FlexGridSizer(3, 2, 0, 0) + self.sizer_1_staticbox.Lower() + sizer_1 = wx.StaticBoxSizer(self.sizer_1_staticbox, wx.VERTICAL) + sizer_basics = wx.FlexGridSizer(3, 2, 0, 0) + sizer_basics.Add(self.label_2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + sizer_basics.Add(self.choice_port, 0, wx.EXPAND, 0) + sizer_basics.Add(self.label_1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + sizer_basics.Add(self.choice_baudrate, 0, wx.EXPAND, 0) + sizer_basics.AddGrowableCol(1) + sizer_1.Add(sizer_basics, 0, wx.EXPAND, 0) + sizer_2.Add(sizer_1, 0, wx.EXPAND, 0) + grid_sizer_1.Add(self.label_3, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + grid_sizer_1.Add(self.choice_databits, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) + grid_sizer_1.Add(self.label_4, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + grid_sizer_1.Add(self.choice_stopbits, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) + grid_sizer_1.Add(self.label_5, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + grid_sizer_1.Add(self.choice_parity, 1, wx.EXPAND | wx.ALIGN_RIGHT, 0) + sizer_format.Add(grid_sizer_1, 1, wx.EXPAND, 0) + self.panel_format.SetSizer(sizer_format) + sizer_2.Add(self.panel_format, 0, wx.EXPAND, 0) + sizer_timeout.Add(self.checkbox_timeout, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + sizer_timeout.Add(self.text_ctrl_timeout, 0, 0, 0) + sizer_timeout.Add(self.label_6, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + self.panel_timeout.SetSizer(sizer_timeout) + sizer_2.Add(self.panel_timeout, 0, wx.EXPAND, 0) + sizer_flow.Add(self.checkbox_rtscts, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + sizer_flow.Add(self.checkbox_xonxoff, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 4) + sizer_flow.Add((10, 10), 1, wx.EXPAND, 0) + self.panel_flow.SetSizer(sizer_flow) + sizer_2.Add(self.panel_flow, 0, wx.EXPAND, 0) sizer_3.Add(self.button_ok, 0, 0, 0) sizer_3.Add(self.button_cancel, 0, 0, 0) - sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4) - self.SetAutoLayout(1) + sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4) self.SetSizer(sizer_2) sizer_2.Fit(self) - sizer_2.SetSizeHints(self) self.Layout() + # end wxGlade def __attach_events(self): wx.EVT_BUTTON(self, self.button_ok.GetId(), self.OnOK) @@ -188,7 +207,7 @@ class SerialConfigDialog(wx.Dialog): def OnOK(self, events): success = True - self.serial.port = self.ports[self.combo_box_port.GetSelection()] + self.serial.port = self.ports[self.choice_port.GetSelection()] if self.show & SHOW_BAUDRATE: self.serial.baudrate = self.serial.BAUDRATES[self.choice_baudrate.GetSelection()] if self.show & SHOW_FORMAT: @@ -203,10 +222,12 @@ class SerialConfigDialog(wx.Dialog): try: self.serial.timeout = float(self.text_ctrl_timeout.GetValue()) except ValueError: - dlg = wx.MessageDialog(self, 'Timeout must be a numeric value', - 'Value Error', wx.OK | wx.ICON_ERROR) - dlg.ShowModal() - dlg.Destroy() + with wx.MessageDialog( + self, + 'Timeout must be a numeric value', + 'Value Error', + wx.OK | wx.ICON_ERROR) as dlg: + dlg.ShowModal() success = False else: self.serial.timeout = None @@ -243,7 +264,7 @@ class MyApp(wx.App): if result != wx.ID_OK: break # the user can play around with the values, CANCEL aborts the loop - while 1: + while True: dialog_serial_cfg = SerialConfigDialog(None, -1, "", serial=ser) self.SetTopWindow(dialog_serial_cfg) result = dialog_serial_cfg.ShowModal() diff --git a/examples/wxSerialConfigDialog.wxg b/examples/wxSerialConfigDialog.wxg index f5e92e0..8938479 100644 --- a/examples/wxSerialConfigDialog.wxg +++ b/examples/wxSerialConfigDialog.wxg @@ -1,7 +1,7 @@ <?xml version="1.0"?> -<!-- generated by wxGlade 0.3.1 on Fri Oct 03 01:53:04 2003 --> +<!-- generated by wxGlade 0.6.5 on Sat Sep 12 02:03:03 2015 --> -<application path="D:\prog\python\pyserial_sf\pyserial\examples\wxSerialConfigDialog.py" name="app" class="MyApp" option="0" language="python" top_window="dialog_serial_cfg" encoding="ISO-8859-1" use_gettext="0" overwrite="0"> +<application path="wxSerialConfigDialog.py" name="app" class="MyApp" option="0" language="python" top_window="dialog_serial_cfg" encoding="ISO-8859-1" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0" indent_amount="4" indent_symbol="space" source_extension=".cpp" header_extension=".h"> <object class="SerialConfigDialog" name="dialog_serial_cfg" base="EditDialog"> <style>wxDEFAULT_DIALOG_STYLE</style> <title>Serial Port Configuration</title> @@ -11,59 +11,51 @@ <flag>wxEXPAND</flag> <border>0</border> <option>0</option> - <object class="wxStaticBoxSizer" name="sizer_basics" base="EditStaticBoxSizer"> + <object class="wxStaticBoxSizer" name="sizer_1" base="EditStaticBoxSizer"> <orient>wxVERTICAL</orient> <label>Basics</label> <object class="sizeritem"> - <flag>wxRIGHT|wxEXPAND</flag> + <flag>wxEXPAND</flag> <border>0</border> <option>0</option> - <object class="wxBoxSizer" name="sizer_5" base="EditBoxSizer"> - <orient>wxHORIZONTAL</orient> + <object class="wxFlexGridSizer" name="sizer_basics" base="EditFlexGridSizer"> + <hgap>0</hgap> + <rows>3</rows> + <growable_cols>1</growable_cols> + <cols>2</cols> + <vgap>0</vgap> <object class="sizeritem"> <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> <border>4</border> - <option>1</option> + <option>0</option> <object class="wxStaticText" name="label_2" base="EditStaticText"> <attribute>1</attribute> <label>Port</label> </object> </object> <object class="sizeritem"> + <flag>wxEXPAND</flag> <border>0</border> - <option>1</option> - <object class="wxComboBox" name="combo_box_port" base="EditComboBox"> + <option>0</option> + <object class="wxChoice" name="choice_port" base="EditChoice"> <selection>0</selection> <choices> - <choice>dummy1</choice> - <choice>dummy2</choice> - <choice>dummy3</choice> - <choice>dummy4</choice> - <choice>dummy5</choice> </choices> </object> </object> - </object> - </object> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>0</option> - <object class="wxBoxSizer" name="sizer_baudrate" base="EditBoxSizer"> - <orient>wxHORIZONTAL</orient> <object class="sizeritem"> <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> <border>4</border> - <option>1</option> + <option>0</option> <object class="wxStaticText" name="label_1" base="EditStaticText"> <attribute>1</attribute> <label>Baudrate</label> </object> </object> <object class="sizeritem"> - <flag>wxALIGN_RIGHT</flag> + <flag>wxEXPAND</flag> <border>0</border> - <option>1</option> + <option>0</option> <object class="wxChoice" name="choice_baudrate" base="EditChoice"> <selection>0</selection> <choices> @@ -79,89 +71,79 @@ <flag>wxEXPAND</flag> <border>0</border> <option>0</option> - <object class="wxStaticBoxSizer" name="sizer_format" base="EditStaticBoxSizer"> - <orient>wxVERTICAL</orient> - <label>Data Format</label> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>0</option> - <object class="wxBoxSizer" name="sizer_6" base="EditBoxSizer"> - <orient>wxHORIZONTAL</orient> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>1</option> - <object class="wxStaticText" name="label_3" base="EditStaticText"> - <attribute>1</attribute> - <label>Data Bits</label> + <object class="wxPanel" name="panel_format" base="EditPanel"> + <style>wxTAB_TRAVERSAL</style> + <object class="wxStaticBoxSizer" name="sizer_format" base="EditStaticBoxSizer"> + <orient>wxVERTICAL</orient> + <label>Data Format</label> + <object class="sizeritem"> + <flag>wxEXPAND</flag> + <border>0</border> + <option>1</option> + <object class="wxFlexGridSizer" name="grid_sizer_1" base="EditFlexGridSizer"> + <hgap>0</hgap> + <rows>3</rows> + <cols>2</cols> + <vgap>0</vgap> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>1</option> + <object class="wxStaticText" name="label_3" base="EditStaticText"> + <attribute>1</attribute> + <label>Data Bits</label> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxALIGN_RIGHT</flag> - <border>0</border> - <option>1</option> - <object class="wxChoice" name="choice_databits" base="EditChoice"> - <selection>0</selection> - <choices> - <choice>choice 1</choice> - </choices> + <object class="sizeritem"> + <flag>wxEXPAND|wxALIGN_RIGHT</flag> + <border>0</border> + <option>1</option> + <object class="wxChoice" name="choice_databits" base="EditChoice"> + <selection>0</selection> + <choices> + <choice>choice 1</choice> + </choices> + </object> </object> - </object> - </object> - </object> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>0</option> - <object class="wxBoxSizer" name="sizer_7" base="EditBoxSizer"> - <orient>wxHORIZONTAL</orient> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>1</option> - <object class="wxStaticText" name="label_4" base="EditStaticText"> - <attribute>1</attribute> - <label>Stop Bits</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>1</option> + <object class="wxStaticText" name="label_4" base="EditStaticText"> + <attribute>1</attribute> + <label>Stop Bits</label> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxALIGN_RIGHT</flag> - <border>0</border> - <option>1</option> - <object class="wxChoice" name="choice_stopbits" base="EditChoice"> - <selection>0</selection> - <choices> - <choice>choice 1</choice> - </choices> + <object class="sizeritem"> + <flag>wxEXPAND|wxALIGN_RIGHT</flag> + <border>0</border> + <option>1</option> + <object class="wxChoice" name="choice_stopbits" base="EditChoice"> + <selection>0</selection> + <choices> + <choice>choice 1</choice> + </choices> + </object> </object> - </object> - </object> - </object> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>0</option> - <object class="wxBoxSizer" name="sizer_8" base="EditBoxSizer"> - <orient>wxHORIZONTAL</orient> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>1</option> - <object class="wxStaticText" name="label_5" base="EditStaticText"> - <attribute>1</attribute> - <label>Parity</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>1</option> + <object class="wxStaticText" name="label_5" base="EditStaticText"> + <attribute>1</attribute> + <label>Parity</label> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxALIGN_RIGHT</flag> - <border>0</border> - <option>1</option> - <object class="wxChoice" name="choice_parity" base="EditChoice"> - <selection>0</selection> - <choices> - <choice>choice 1</choice> - </choices> + <object class="sizeritem"> + <flag>wxEXPAND|wxALIGN_RIGHT</flag> + <border>0</border> + <option>1</option> + <object class="wxChoice" name="choice_parity" base="EditChoice"> + <selection>0</selection> + <choices> + <choice>choice 1</choice> + </choices> + </object> </object> </object> </object> @@ -169,33 +151,37 @@ </object> </object> <object class="sizeritem"> + <flag>wxEXPAND</flag> <border>0</border> <option>0</option> - <object class="wxStaticBoxSizer" name="sizer_timeout" base="EditStaticBoxSizer"> - <orient>wxHORIZONTAL</orient> - <label>Timeout</label> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>0</option> - <object class="wxCheckBox" name="checkbox_timeout" base="EditCheckBox"> - <label>Use Timeout</label> + <object class="wxPanel" name="panel_timeout" base="EditPanel"> + <style>wxTAB_TRAVERSAL</style> + <object class="wxStaticBoxSizer" name="sizer_timeout" base="EditStaticBoxSizer"> + <orient>wxHORIZONTAL</orient> + <label>Timeout</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>0</option> + <object class="wxCheckBox" name="checkbox_timeout" base="EditCheckBox"> + <label>Use Timeout</label> + </object> </object> - </object> - <object class="sizeritem"> - <border>0</border> - <option>0</option> - <object class="wxTextCtrl" name="text_ctrl_timeout" base="EditTextCtrl"> - <disabled>1</disabled> + <object class="sizeritem"> + <border>0</border> + <option>0</option> + <object class="wxTextCtrl" name="text_ctrl_timeout" base="EditTextCtrl"> + <disabled>1</disabled> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>0</option> - <object class="wxStaticText" name="label_6" base="EditStaticText"> - <attribute>1</attribute> - <label>seconds</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>0</option> + <object class="wxStaticText" name="label_6" base="EditStaticText"> + <attribute>1</attribute> + <label>seconds</label> + </object> </object> </object> </object> @@ -204,32 +190,35 @@ <flag>wxEXPAND</flag> <border>0</border> <option>0</option> - <object class="wxStaticBoxSizer" name="sizer_flow" base="EditStaticBoxSizer"> - <orient>wxHORIZONTAL</orient> - <label>Flow Control</label> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>0</option> - <object class="wxCheckBox" name="checkbox_rtscts" base="EditCheckBox"> - <label>RTS/CTS</label> + <object class="wxPanel" name="panel_flow" base="EditPanel"> + <style>wxTAB_TRAVERSAL</style> + <object class="wxStaticBoxSizer" name="sizer_flow" base="EditStaticBoxSizer"> + <orient>wxHORIZONTAL</orient> + <label>Flow Control</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>0</option> + <object class="wxCheckBox" name="checkbox_rtscts" base="EditCheckBox"> + <label>RTS/CTS</label> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> - <border>4</border> - <option>0</option> - <object class="wxCheckBox" name="checkbox_xonxoff" base="EditCheckBox"> - <label>Xon/Xoff</label> + <object class="sizeritem"> + <flag>wxALL|wxALIGN_CENTER_VERTICAL</flag> + <border>4</border> + <option>0</option> + <object class="wxCheckBox" name="checkbox_xonxoff" base="EditCheckBox"> + <label>Xon/Xoff</label> + </object> </object> - </object> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>1</option> - <object class="spacer" name="spacer" base="EditSpacer"> - <height>10</height> - <width>10</width> + <object class="sizeritem"> + <flag>wxEXPAND</flag> + <border>0</border> + <option>1</option> + <object class="spacer" name="spacer" base="EditSpacer"> + <height>10</height> + <width>10</width> + </object> </object> </object> </object> @@ -244,15 +233,15 @@ <border>0</border> <option>0</option> <object class="wxButton" name="button_ok" base="EditButton"> + <stockitem>OK</stockitem> <default>1</default> - <label>OK</label> </object> </object> <object class="sizeritem"> <border>0</border> <option>0</option> <object class="wxButton" name="button_cancel" base="EditButton"> - <label>Cancel</label> + <stockitem>CANCEL</stockitem> </object> </object> </object> diff --git a/examples/wxTerminal.py b/examples/wxTerminal.py index 47ee698..973b933 100755 --- a/examples/wxTerminal.py +++ b/examples/wxTerminal.py @@ -38,6 +38,8 @@ ID_SAVEAS = wx.NewId() ID_SETTINGS = wx.NewId() ID_TERM = wx.NewId() ID_EXIT = wx.NewId() +ID_RTS = wx.NewId() +ID_DTR = wx.NewId() NEWLINE_CR = 0 NEWLINE_LF = 1 @@ -63,6 +65,7 @@ class TerminalSettingsDialog(wx.Dialog): self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo") self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters") self.radio_box_newline = wx.RadioBox(self, -1, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS) + self.sizer_4_staticbox = wx.StaticBox(self, -1, "Input/Output") self.button_ok = wx.Button(self, -1, "OK") self.button_cancel = wx.Button(self, -1, "Cancel") @@ -85,18 +88,17 @@ class TerminalSettingsDialog(wx.Dialog): # begin wxGlade: TerminalSettingsDialog.__do_layout sizer_2 = wx.BoxSizer(wx.VERTICAL) sizer_3 = wx.BoxSizer(wx.HORIZONTAL) - sizer_4 = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Input/Output"), wx.VERTICAL) + self.sizer_4_staticbox.Lower() + sizer_4 = wx.StaticBoxSizer(self.sizer_4_staticbox, wx.VERTICAL) sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4) sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4) sizer_4.Add(self.radio_box_newline, 0, 0, 0) sizer_2.Add(sizer_4, 0, wx.EXPAND, 0) sizer_3.Add(self.button_ok, 0, 0, 0) sizer_3.Add(self.button_cancel, 0, 0, 0) - sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4) - self.SetAutoLayout(1) + sizer_2.Add(sizer_3, 0, wx.ALL | wx.ALIGN_RIGHT, 4) self.SetSizer(sizer_2) sizer_2.Fit(self) - sizer_2.SetSizeHints(self) self.Layout() # end wxGlade @@ -130,24 +132,36 @@ class TerminalFrame(wx.Frame): # begin wxGlade: TerminalFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) - self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY) - + # Menu Bar self.frame_terminal_menubar = wx.MenuBar() - self.SetMenuBar(self.frame_terminal_menubar) wxglade_tmp_menu = wx.Menu() wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL) wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL) wxglade_tmp_menu.AppendSeparator() - wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL) wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL) wxglade_tmp_menu.AppendSeparator() wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL) self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File") + wxglade_tmp_menu = wx.Menu() + wxglade_tmp_menu.Append(ID_RTS, "RTS", "", wx.ITEM_CHECK) + wxglade_tmp_menu.Append(ID_DTR, "&DTR", "", wx.ITEM_CHECK) + wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL) + self.frame_terminal_menubar.Append(wxglade_tmp_menu, "Serial Port") + self.SetMenuBar(self.frame_terminal_menubar) # Menu Bar end + self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE | wx.TE_READONLY) self.__set_properties() self.__do_layout() + + self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR) + self.Bind(wx.EVT_MENU, self.OnSaveAs, id=ID_SAVEAS) + self.Bind(wx.EVT_MENU, self.OnTermSettings, id=ID_TERM) + self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT) + self.Bind(wx.EVT_MENU, self.OnRTS, id=ID_RTS) + self.Bind(wx.EVT_MENU, self.OnDTR, id=ID_DTR) + self.Bind(wx.EVT_MENU, self.OnPortSettings, id=ID_SETTINGS) # end wxGlade self.__attach_events() #register events self.OnPortSettings(None) #call setup dialog on startup, opens port @@ -160,6 +174,10 @@ class TerminalFrame(wx.Frame): self.thread.setDaemon(1) self.alive.set() self.thread.start() + self.serial.rts = True + self.serial.dtr = True + self.frame_terminal_menubar.Check(ID_RTS, self.serial.rts) + self.frame_terminal_menubar.Check(ID_DTR, self.serial.dtr) def StopThread(self): """Stop the receiver thread, wait util it's finished.""" @@ -172,13 +190,13 @@ class TerminalFrame(wx.Frame): # begin wxGlade: TerminalFrame.__set_properties self.SetTitle("Serial Terminal") self.SetSize((546, 383)) + self.text_ctrl_output.SetFont(wx.Font(9, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "")) # end wxGlade def __do_layout(self): # begin wxGlade: TerminalFrame.__do_layout sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0) - self.SetAutoLayout(1) self.SetSizer(sizer_1) self.Layout() # end wxGlade @@ -194,102 +212,109 @@ class TerminalFrame(wx.Frame): self.Bind(EVT_SERIALRX, self.OnSerialRead) self.Bind(wx.EVT_CLOSE, self.OnClose) - def OnExit(self, event): + def OnExit(self, event): # wxGlade: TerminalFrame.<event_handler> """Menu point Exit""" self.Close() def OnClose(self, event): """Called on application shutdown.""" - self.StopThread() #stop reader thread - self.serial.close() #cleanup - self.Destroy() #close windows, exit app + self.StopThread() # stop reader thread + self.serial.close() # cleanup + self.Destroy() # close windows, exit app - def OnSaveAs(self, event): + def OnSaveAs(self, event): # wxGlade: TerminalFrame.<event_handler> """Save contents of output window.""" filename = None - dlg = wx.FileDialog(None, "Save Text As...", ".", "", "Text File|*.txt|All Files|*", wx.SAVE) - if dlg.ShowModal() == wx.ID_OK: - filename = dlg.GetPath() - dlg.Destroy() + with wx.FileDialog( + None, + "Save Text As...", + ".", + "", + "Text File|*.txt|All Files|*", + wx.SAVE) as dlg: + if dlg.ShowModal() == wx.ID_OK: + filename = dlg.GetPath() if filename is not None: - f = file(filename, 'w') - text = self.text_ctrl_output.GetValue() - if type(text) == unicode: - text = text.encode("latin1") #hm, is that a good asumption? - f.write(text) - f.close() + with file(filename, 'w') as f: + text = self.text_ctrl_output.GetValue() + if type(text) == unicode: + text = text.encode("latin1") # hm, is that a good asumption? + f.write(text) - def OnClear(self, event): + def OnClear(self, event): # wxGlade: TerminalFrame.<event_handler> """Clear contents of output window.""" self.text_ctrl_output.Clear() - def OnPortSettings(self, event=None): + def OnPortSettings(self, event): # wxGlade: TerminalFrame.<event_handler> """Show the portsettings dialog. The reader thread is stopped for the settings change.""" - if event is not None: #will be none when called on startup + if event is not None: # will be none when called on startup self.StopThread() self.serial.close() ok = False while not ok: - dialog_serial_cfg = wxSerialConfigDialog.SerialConfigDialog(None, -1, "", - show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW, - serial=self.serial - ) - result = dialog_serial_cfg.ShowModal() - dialog_serial_cfg.Destroy() - #open port if not called on startup, open it on startup and OK too + with wxSerialConfigDialog.SerialConfigDialog( + None, + -1, + "", + show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW, + serial=self.serial) as dialog_serial_cfg: + result = dialog_serial_cfg.ShowModal() + # open port if not called on startup, open it on startup and OK too if result == wx.ID_OK or event is not None: try: self.serial.open() except serial.SerialException as e: - dlg = wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR) - dlg.ShowModal() - dlg.Destroy() + with wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)as dlg: + dlg.ShowModal() else: self.StartThread() self.SetTitle("Serial Terminal on %s [%s, %s%s%s%s%s]" % ( - self.serial.portstr, - self.serial.baudrate, - self.serial.bytesize, - self.serial.parity, - self.serial.stopbits, - self.serial.rtscts and ' RTS/CTS' or '', - self.serial.xonxoff and ' Xon/Xoff' or '', - ) - ) + self.serial.portstr, + self.serial.baudrate, + self.serial.bytesize, + self.serial.parity, + self.serial.stopbits, + ' RTS/CTS' if self.serial.rtscts else '', + ' Xon/Xoff' if self.serial.xonxoff else '', + )) ok = True else: - #on startup, dialog aborted + # on startup, dialog aborted self.alive.clear() ok = True - def OnTermSettings(self, event): - """Menu point Terminal Settings. Show the settings dialog - with the current terminal settings""" + def OnTermSettings(self, event): # wxGlade: TerminalFrame.<event_handler> + """\ + Menu point Terminal Settings. Show the settings dialog + with the current terminal settings. + """ dialog = TerminalSettingsDialog(None, -1, "", settings=self.settings) result = dialog.ShowModal() dialog.Destroy() def OnKey(self, event): - """Key event handler. if the key is in the ASCII range, write it to the serial port. - Newline handling and local echo is also done here.""" + """\ + Key event handler. if the key is in the ASCII range, write it to the + serial port. Newline handling and local echo is also done here. + """ code = event.GetKeyCode() - if code < 256: #is it printable? - if code == 13: #is it a newline? (check for CR which is the RETURN key) - if self.settings.echo: #do echo if needed + if code < 256: # is it printable? + if code == 13: # is it a newline? (check for CR which is the RETURN key) + if self.settings.echo: # do echo if needed self.text_ctrl_output.AppendText('\n') if self.settings.newline == NEWLINE_CR: - self.serial.write('\r') #send CR + self.serial.write('\r') # send CR elif self.settings.newline == NEWLINE_LF: - self.serial.write('\n') #send LF + self.serial.write('\n') # send LF elif self.settings.newline == NEWLINE_CRLF: - self.serial.write('\r\n') #send CR+LF + self.serial.write('\r\n') # send CR+LF else: char = chr(code) - if self.settings.echo: #do echo if needed + if self.settings.echo: # do echo if needed self.text_ctrl_output.WriteText(char) - self.serial.write(char) #send the charcater + self.serial.write(char) # send the charcater else: print("Extra Key:", code) @@ -301,24 +326,29 @@ class TerminalFrame(wx.Frame): self.text_ctrl_output.AppendText(text) def ComPortThread(self): - """Thread that handles the incomming traffic. Does the basic input - transformation (newlines) and generates an SerialRxEvent""" - while self.alive.isSet(): #loop while alive event is true - text = self.serial.read(1) #read one, with timout - if text: #check if not timeout - n = self.serial.inWaiting() #look if there is more to read - if n: - text = text + self.serial.read(n) #get it - #newline transformation + """\ + Thread that handles the incomming traffic. Does the basic input + transformation (newlines) and generates an SerialRxEvent + """ + while self.alive.isSet(): + b = self.serial.read(self.serial.in_waiting or 1) + if b: + # newline transformation if self.settings.newline == NEWLINE_CR: - text = text.replace('\r', '\n') + b = b.replace('\r', '\n') elif self.settings.newline == NEWLINE_LF: pass elif self.settings.newline == NEWLINE_CRLF: - text = text.replace('\r\n', '\n') - event = SerialRxEvent(self.GetId(), text) + b = b.replace('\r\n', '\n') + event = SerialRxEvent(self.GetId(), b) self.GetEventHandler().AddPendingEvent(event) - #~ self.OnSerialRead(text) #output text in window + #~ self.OnSerialRead(text) # output text in window + + def OnRTS(self, event): # wxGlade: TerminalFrame.<event_handler> + self.serial.rts = event.IsChecked() + + def OnDTR(self, event): # wxGlade: TerminalFrame.<event_handler> + self.serial.dtr = event.Checked() # end of class TerminalFrame @@ -328,7 +358,7 @@ class MyApp(wx.App): wx.InitAllImageHandlers() frame_terminal = TerminalFrame(None, -1, "") self.SetTopWindow(frame_terminal) - frame_terminal.Show(1) + frame_terminal.Show(True) return 1 # end of class MyApp diff --git a/examples/wxTerminal.wxg b/examples/wxTerminal.wxg index 183f876..b74bb31 100644 --- a/examples/wxTerminal.wxg +++ b/examples/wxTerminal.wxg @@ -1,33 +1,24 @@ <?xml version="1.0"?> -<!-- generated by wxGlade 0.3.1 on Sat Oct 04 02:41:48 2003 --> +<!-- generated by wxGlade 0.6.5 on Sat Sep 12 02:36:17 2015 --> -<application path="D:\prog\python\pyserial_sf\pyserial\examples\wxTerminal.py" name="app" class="MyApp" option="0" language="python" top_window="frame_terminal" encoding="ISO-8859-1" use_gettext="0" overwrite="0"> +<application path="wxTerminal.py" name="app" class="MyApp" option="0" language="python" top_window="frame_terminal" encoding="ISO-8859-1" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0" indent_amount="4" indent_symbol="space" source_extension=".cpp" header_extension=".h"> <object class="TerminalFrame" name="frame_terminal" base="EditFrame"> <style>wxDEFAULT_FRAME_STYLE</style> <title>Serial Terminal</title> <menubar>1</menubar> <size>546, 383</size> - <object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer"> - <orient>wxVERTICAL</orient> - <object class="sizeritem"> - <flag>wxEXPAND</flag> - <border>0</border> - <option>1</option> - <object class="wxTextCtrl" name="text_ctrl_output" base="EditTextCtrl"> - <style>wxTE_MULTILINE|wxTE_READONLY</style> - </object> - </object> - </object> <object class="wxMenuBar" name="frame_terminal_menubar" base="EditMenuBar"> <menus> <menu name="" label="&File"> <item> <label>&Clear</label> <id>ID_CLEAR</id> + <handler>OnClear</handler> </item> <item> <label>&Save Text As...</label> <id>ID_SAVEAS</id> + <handler>OnSaveAs</handler> </item> <item> <label>---</label> @@ -35,12 +26,9 @@ <name>---</name> </item> <item> - <label>&Port Settings...</label> - <id>ID_SETTINGS</id> - </item> - <item> <label>&Terminal Settings...</label> <id>ID_TERM</id> + <handler>OnTermSettings</handler> </item> <item> <label>---</label> @@ -49,10 +37,49 @@ <item> <label>&Exit</label> <id>ID_EXIT</id> + <handler>OnExit</handler> + </item> + </menu> + <menu name="" label="Serial Port"> + <item> + <label>RTS</label> + <id>ID_RTS</id> + <checkable>1</checkable> + <handler>OnRTS</handler> + </item> + <item> + <label>&DTR</label> + <id>ID_DTR</id> + <checkable>1</checkable> + <handler>OnDTR</handler> + </item> + <item> + <label>&Port Settings...</label> + <id>ID_SETTINGS</id> + <handler>OnPortSettings</handler> </item> </menu> </menus> </object> + <object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer"> + <orient>wxVERTICAL</orient> + <object class="sizeritem"> + <flag>wxEXPAND</flag> + <border>0</border> + <option>1</option> + <object class="wxTextCtrl" name="text_ctrl_output" base="EditTextCtrl"> + <style>wxTE_MULTILINE|wxTE_READONLY</style> + <font> + <size>9</size> + <family>modern</family> + <style>normal</style> + <weight>normal</weight> + <underlined>0</underlined> + <face></face> + </font> + </object> + </object> + </object> </object> <object class="TerminalSettingsDialog" name="dialog_terminal_Settings" base="EditDialog"> <style>wxDEFAULT_DIALOG_STYLE</style> |