cliechti | 8f376e7 | 2003-10-03 00:16:11 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # generated by wxGlade 0.3.1 on Thu Oct 02 23:25:44 2003 |
| 3 | |
| 4 | from wxPython.wx import * |
| 5 | import serial |
| 6 | |
| 7 | SHOW_BAUDRATE = 1<<0 |
| 8 | SHOW_FORMAT = 1<<1 |
| 9 | SHOW_FLOW = 1<<2 |
| 10 | SHOW_TIMEOUT = 1<<3 |
| 11 | SHOW_ALL = SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT |
| 12 | |
| 13 | class SerialConfigDialog(wxDialog): |
| 14 | """Serial Port confiuration dialog, to be used with pyserial 2.0+ |
| 15 | When instantiating a class of this dialog, then the "serial" keyword |
| 16 | argument is mandatory. It is a reference to a serial.Serial instance. |
| 17 | the optional "show" keyword argument can be used to show/hide different |
| 18 | settings. The default is SHOW_ALL which coresponds to |
| 19 | SHOW_BAUDRATE|SHOW_FORMAT|SHOW_FLOW|SHOW_TIMEOUT. All constants can be |
| 20 | found in ths module (not the class).""" |
| 21 | |
| 22 | def __init__(self, *args, **kwds): |
| 23 | #grab the serial keyword and remove it from the dict |
| 24 | self.serial = kwds['serial'] |
| 25 | del kwds['serial'] |
| 26 | self.show = SHOW_ALL |
| 27 | if kwds.has_key('show'): |
| 28 | self.show = kwds['show'] |
| 29 | del kwds['show'] |
| 30 | # begin wxGlade: SerialConfigDialog.__init__ |
| 31 | # end wxGlade |
| 32 | kwds["style"] = wxDEFAULT_DIALOG_STYLE |
| 33 | wxDialog.__init__(self, *args, **kwds) |
| 34 | self.label_2 = wxStaticText(self, -1, "Port") |
| 35 | self.combo_box_port = wxComboBox(self, -1, choices=["dummy1", "dummy2", "dummy3", "dummy4", "dummy5"], style=wxCB_DROPDOWN) |
| 36 | if self.show & SHOW_BAUDRATE: |
| 37 | self.label_1 = wxStaticText(self, -1, "Baudrate") |
| 38 | self.choice_baudrate = wxChoice(self, -1, choices=["choice 1"]) |
| 39 | if self.show & SHOW_FORMAT: |
| 40 | self.label_3 = wxStaticText(self, -1, "Data Bits") |
| 41 | self.choice_databits = wxChoice(self, -1, choices=["choice 1"]) |
| 42 | self.label_4 = wxStaticText(self, -1, "Stop Bits") |
| 43 | self.choice_stopbits = wxChoice(self, -1, choices=["choice 1"]) |
| 44 | self.label_5 = wxStaticText(self, -1, "Parity") |
| 45 | self.choice_parity = wxChoice(self, -1, choices=["choice 1"]) |
| 46 | if self.show & SHOW_TIMEOUT: |
| 47 | self.checkbox_timeout = wxCheckBox(self, -1, "Use Timeout") |
| 48 | self.text_ctrl_timeout = wxTextCtrl(self, -1, "") |
| 49 | self.label_6 = wxStaticText(self, -1, "seconds") |
| 50 | if self.show & SHOW_FLOW: |
| 51 | self.checkbox_rtscts = wxCheckBox(self, -1, "RTS/CTS") |
| 52 | self.checkbox_xonxoff = wxCheckBox(self, -1, "Xon/Xoff") |
| 53 | self.button_ok = wxButton(self, -1, "OK") |
| 54 | self.button_cancel = wxButton(self, -1, "Cancel") |
| 55 | |
| 56 | self.__set_properties() |
| 57 | self.__do_layout() |
| 58 | #fill in ports and select current setting |
| 59 | index = 0 |
| 60 | self.combo_box_port.Clear() |
| 61 | for n in range(4): |
| 62 | portname = serial.device(n) |
| 63 | self.combo_box_port.Append(portname) |
| 64 | if self.serial.portstr == portname: |
| 65 | index = n |
| 66 | if self.serial.portstr is not None: |
| 67 | self.combo_box_port.SetValue(str(self.serial.portstr)) |
| 68 | else: |
| 69 | self.combo_box_port.SetSelection(index) |
| 70 | if self.show & SHOW_BAUDRATE: |
| 71 | #fill in badrates and select current setting |
| 72 | self.choice_baudrate.Clear() |
| 73 | for n, baudrate in enumerate(self.serial.BAUDRATES): |
| 74 | self.choice_baudrate.Append(str(baudrate)) |
| 75 | if self.serial.baudrate == baudrate: |
| 76 | index = n |
| 77 | self.choice_baudrate.SetSelection(index) |
| 78 | if self.show & SHOW_FORMAT: |
| 79 | #fill in databits and select current setting |
| 80 | self.choice_databits.Clear() |
| 81 | for n, bytesize in enumerate(self.serial.BYTESIZES): |
| 82 | self.choice_databits.Append(str(bytesize)) |
| 83 | if self.serial.bytesize == bytesize: |
| 84 | index = n |
| 85 | self.choice_databits.SetSelection(index) |
| 86 | #fill in stopbits and select current setting |
| 87 | self.choice_stopbits.Clear() |
| 88 | for n, stopbits in enumerate(self.serial.STOPBITS): |
| 89 | self.choice_stopbits.Append(str(stopbits)) |
| 90 | if self.serial.stopbits == stopbits: |
| 91 | index = n |
| 92 | self.choice_stopbits.SetSelection(index) |
| 93 | #fill in parities and select current setting |
| 94 | self.choice_parity.Clear() |
| 95 | for n, parity in enumerate(self.serial.PARITIES): |
| 96 | self.choice_parity.Append(str(serial.PARITY_NAMES[parity])) |
| 97 | if self.serial.parity == parity: |
| 98 | index = n |
| 99 | self.choice_parity.SetSelection(index) |
| 100 | if self.show & SHOW_TIMEOUT: |
| 101 | #set the timeout mode and value |
| 102 | if self.serial.timeout is None: |
| 103 | self.checkbox_timeout.SetValue(False) |
| 104 | self.text_ctrl_timeout.Enable(False) |
| 105 | else: |
| 106 | self.checkbox_timeout.SetValue(True) |
| 107 | self.text_ctrl_timeout.Enable(True) |
| 108 | self.text_ctrl_timeout.SetValue(str(self.serial.timeout)) |
| 109 | if self.show & SHOW_FLOW: |
| 110 | #set the rtscts mode |
| 111 | self.checkbox_rtscts.SetValue(self.serial.rtscts) |
| 112 | #set the rtscts mode |
| 113 | self.checkbox_xonxoff.SetValue(self.serial.xonxoff) |
| 114 | #attach the event handlers |
| 115 | self.__attach_events() |
| 116 | |
| 117 | def __set_properties(self): |
| 118 | # begin wxGlade: SerialConfigDialog.__set_properties |
| 119 | # end wxGlade |
| 120 | self.SetTitle("Serial Port Configuration") |
| 121 | if self.show & SHOW_TIMEOUT: |
| 122 | self.text_ctrl_timeout.Enable(0) |
| 123 | self.button_ok.SetDefault() |
| 124 | |
| 125 | def __do_layout(self): |
| 126 | # begin wxGlade: SerialConfigDialog.__do_layout |
| 127 | # end wxGlade |
| 128 | sizer_2 = wxBoxSizer(wxVERTICAL) |
| 129 | sizer_3 = wxBoxSizer(wxHORIZONTAL) |
| 130 | sizer_basics = wxStaticBoxSizer(wxStaticBox(self, -1, "Basics"), wxVERTICAL) |
| 131 | sizer_5 = wxBoxSizer(wxHORIZONTAL) |
| 132 | sizer_5.Add(self.label_2, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 133 | sizer_5.Add(self.combo_box_port, 1, 0, 0) |
| 134 | sizer_basics.Add(sizer_5, 0, wxRIGHT|wxEXPAND, 0) |
| 135 | if self.show & SHOW_BAUDRATE: |
| 136 | sizer_baudrate = wxBoxSizer(wxHORIZONTAL) |
| 137 | sizer_baudrate.Add(self.label_1, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 138 | sizer_baudrate.Add(self.choice_baudrate, 1, wxALIGN_RIGHT, 0) |
| 139 | sizer_basics.Add(sizer_baudrate, 0, wxEXPAND, 0) |
| 140 | sizer_2.Add(sizer_basics, 0, wxEXPAND, 0) |
| 141 | if self.show & SHOW_FORMAT: |
| 142 | sizer_8 = wxBoxSizer(wxHORIZONTAL) |
| 143 | sizer_7 = wxBoxSizer(wxHORIZONTAL) |
| 144 | sizer_6 = wxBoxSizer(wxHORIZONTAL) |
| 145 | sizer_format = wxStaticBoxSizer(wxStaticBox(self, -1, "Data Format"), wxVERTICAL) |
| 146 | sizer_6.Add(self.label_3, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 147 | sizer_6.Add(self.choice_databits, 1, wxALIGN_RIGHT, 0) |
| 148 | sizer_format.Add(sizer_6, 0, wxEXPAND, 0) |
| 149 | sizer_7.Add(self.label_4, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 150 | sizer_7.Add(self.choice_stopbits, 1, wxALIGN_RIGHT, 0) |
| 151 | sizer_format.Add(sizer_7, 0, wxEXPAND, 0) |
| 152 | sizer_8.Add(self.label_5, 1, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 153 | sizer_8.Add(self.choice_parity, 1, wxALIGN_RIGHT, 0) |
| 154 | sizer_format.Add(sizer_8, 0, wxEXPAND, 0) |
| 155 | sizer_2.Add(sizer_format, 0, wxEXPAND, 0) |
| 156 | if self.show & SHOW_TIMEOUT: |
| 157 | sizer_timeout = wxStaticBoxSizer(wxStaticBox(self, -1, "Timeout"), wxHORIZONTAL) |
| 158 | sizer_timeout.Add(self.checkbox_timeout, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 159 | sizer_timeout.Add(self.text_ctrl_timeout, 0, 0, 0) |
| 160 | sizer_timeout.Add(self.label_6, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 161 | sizer_2.Add(sizer_timeout, 0, 0, 0) |
| 162 | if self.show & SHOW_FLOW: |
| 163 | sizer_flow = wxStaticBoxSizer(wxStaticBox(self, -1, "Flow Control"), wxHORIZONTAL) |
| 164 | sizer_flow.Add(self.checkbox_rtscts, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 165 | sizer_flow.Add(self.checkbox_xonxoff, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
| 166 | sizer_flow.Add(10, 10, 1, wxEXPAND, 0) |
| 167 | sizer_2.Add(sizer_flow, 0, wxEXPAND, 0) |
| 168 | sizer_3.Add(self.button_ok, 0, 0, 0) |
| 169 | sizer_3.Add(self.button_cancel, 0, 0, 0) |
| 170 | sizer_2.Add(sizer_3, 0, wxALL|wxALIGN_RIGHT, 4) |
| 171 | self.SetAutoLayout(1) |
| 172 | self.SetSizer(sizer_2) |
| 173 | sizer_2.Fit(self) |
| 174 | sizer_2.SetSizeHints(self) |
| 175 | self.Layout() |
| 176 | |
| 177 | def __attach_events(self): |
| 178 | EVT_BUTTON(self, self.button_ok.GetId(), self.OnOK) |
| 179 | EVT_BUTTON(self, self.button_cancel.GetId(), self.OnCancel) |
| 180 | if self.show & SHOW_TIMEOUT: |
| 181 | EVT_CHECKBOX(self, self.checkbox_timeout.GetId(), self.OnTimeout) |
| 182 | |
| 183 | def OnOK(self, events): |
| 184 | success = True |
| 185 | self.serial.port = str(self.combo_box_port.GetValue()) |
| 186 | if self.show & SHOW_BAUDRATE: |
| 187 | self.serial.baudrate = self.serial.BAUDRATES[self.choice_baudrate.GetSelection()] |
| 188 | if self.show & SHOW_FORMAT: |
| 189 | self.serial.bytesize = self.serial.BYTESIZES[self.choice_databits.GetSelection()] |
| 190 | self.serial.stopbits = self.serial.STOPBITS[self.choice_stopbits.GetSelection()] |
| 191 | self.serial.parity = self.serial.PARITIES[self.choice_parity.GetSelection()] |
| 192 | if self.show & SHOW_FLOW: |
| 193 | self.serial.rtscts = self.checkbox_rtscts.GetValue() |
| 194 | self.serial.xonxoff = self.checkbox_xonxoff.GetValue() |
| 195 | if self.show & SHOW_TIMEOUT: |
| 196 | if self.checkbox_timeout.GetValue(): |
| 197 | try: |
| 198 | self.serial.timeout = float(self.text_ctrl_timeout.GetValue()) |
| 199 | except ValueError: |
| 200 | dlg = wxMessageDialog(self, 'Timeout must be a numeric value', |
| 201 | 'Value Error', wxOK | wxICON_ERROR) |
| 202 | dlg.ShowModal() |
| 203 | dlg.Destroy() |
| 204 | success = False |
| 205 | else: |
| 206 | self.serial.timeout = None |
| 207 | if success: |
| 208 | self.EndModal(wxID_OK) |
| 209 | |
| 210 | def OnCancel(self, events): |
| 211 | self.EndModal(wxID_CANCEL) |
| 212 | |
| 213 | def OnTimeout(self, events): |
| 214 | if self.checkbox_timeout.GetValue(): |
| 215 | self.text_ctrl_timeout.Enable(True) |
| 216 | else: |
| 217 | self.text_ctrl_timeout.Enable(False) |
| 218 | |
| 219 | # end of class SerialConfigDialog |
| 220 | |
| 221 | |
| 222 | class MyApp(wxApp): |
| 223 | """Test code""" |
| 224 | def OnInit(self): |
| 225 | wxInitAllImageHandlers() |
| 226 | |
| 227 | ser = serial.Serial() |
| 228 | print ser |
| 229 | #loop until cancel is pressed, old values are used as start for the next run |
| 230 | #show the different views, one after the other |
| 231 | #value are kept. |
| 232 | for flags in (SHOW_BAUDRATE, SHOW_FLOW, SHOW_FORMAT, SHOW_TIMEOUT, SHOW_ALL): |
| 233 | dialog_serial_cfg = SerialConfigDialog(None, -1, "", serial=ser, show=flags) |
| 234 | self.SetTopWindow(dialog_serial_cfg) |
| 235 | result = dialog_serial_cfg.ShowModal() |
| 236 | print ser |
| 237 | if result != wxID_OK: |
| 238 | break |
| 239 | #the user can play around with the values, CANCEL aborts the loop |
| 240 | while 1: |
| 241 | dialog_serial_cfg = SerialConfigDialog(None, -1, "", serial=ser) |
| 242 | self.SetTopWindow(dialog_serial_cfg) |
| 243 | result = dialog_serial_cfg.ShowModal() |
| 244 | print ser |
| 245 | if result != wxID_OK: |
| 246 | break |
| 247 | return 0 |
| 248 | |
| 249 | # end of class MyApp |
| 250 | |
| 251 | if __name__ == "__main__": |
| 252 | app = MyApp(0) |
| 253 | app.MainLoop() |