Newer
Older
TestStandRepository / Software / Arduino / libraries / pyserial-2.7 / examples / wxTerminal.py
  1. #!/usr/bin/env python
  2. # generated by wxGlade 0.3.1 on Fri Oct 03 23:23:45 2003
  3.  
  4. #from wxPython.wx import *
  5. import wx
  6. import wxSerialConfigDialog
  7. import serial
  8. import threading
  9.  
  10. #----------------------------------------------------------------------
  11. # Create an own event type, so that GUI updates can be delegated
  12. # this is required as on some platforms only the main thread can
  13. # access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
  14. # could be used too, but an event is more elegant.
  15.  
  16. SERIALRX = wx.NewEventType()
  17. # bind to serial data receive events
  18. EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
  19.  
  20. class SerialRxEvent(wx.PyCommandEvent):
  21. eventType = SERIALRX
  22. def __init__(self, windowID, data):
  23. wx.PyCommandEvent.__init__(self, self.eventType, windowID)
  24. self.data = data
  25.  
  26. def Clone(self):
  27. self.__class__(self.GetId(), self.data)
  28.  
  29. #----------------------------------------------------------------------
  30.  
  31. ID_CLEAR = wx.NewId()
  32. ID_SAVEAS = wx.NewId()
  33. ID_SETTINGS = wx.NewId()
  34. ID_TERM = wx.NewId()
  35. ID_EXIT = wx.NewId()
  36.  
  37. NEWLINE_CR = 0
  38. NEWLINE_LF = 1
  39. NEWLINE_CRLF = 2
  40.  
  41. class TerminalSetup:
  42. """Placeholder for various terminal settings. Used to pass the
  43. options to the TerminalSettingsDialog."""
  44. def __init__(self):
  45. self.echo = False
  46. self.unprintable = False
  47. self.newline = NEWLINE_CRLF
  48.  
  49. class TerminalSettingsDialog(wx.Dialog):
  50. """Simple dialog with common terminal settings like echo, newline mode."""
  51. def __init__(self, *args, **kwds):
  52. self.settings = kwds['settings']
  53. del kwds['settings']
  54. # begin wxGlade: TerminalSettingsDialog.__init__
  55. kwds["style"] = wx.DEFAULT_DIALOG_STYLE
  56. wx.Dialog.__init__(self, *args, **kwds)
  57. self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo")
  58. self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters")
  59. self.radio_box_newline = wx.RadioBox(self, -1, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
  60. self.button_ok = wx.Button(self, -1, "OK")
  61. self.button_cancel = wx.Button(self, -1, "Cancel")
  62.  
  63. self.__set_properties()
  64. self.__do_layout()
  65. # end wxGlade
  66. self.__attach_events()
  67. self.checkbox_echo.SetValue(self.settings.echo)
  68. self.checkbox_unprintable.SetValue(self.settings.unprintable)
  69. self.radio_box_newline.SetSelection(self.settings.newline)
  70.  
  71. def __set_properties(self):
  72. # begin wxGlade: TerminalSettingsDialog.__set_properties
  73. self.SetTitle("Terminal Settings")
  74. self.radio_box_newline.SetSelection(0)
  75. self.button_ok.SetDefault()
  76. # end wxGlade
  77.  
  78. def __do_layout(self):
  79. # begin wxGlade: TerminalSettingsDialog.__do_layout
  80. sizer_2 = wx.BoxSizer(wx.VERTICAL)
  81. sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
  82. sizer_4 = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Input/Output"), wx.VERTICAL)
  83. sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
  84. sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
  85. sizer_4.Add(self.radio_box_newline, 0, 0, 0)
  86. sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
  87. sizer_3.Add(self.button_ok, 0, 0, 0)
  88. sizer_3.Add(self.button_cancel, 0, 0, 0)
  89. sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
  90. self.SetAutoLayout(1)
  91. self.SetSizer(sizer_2)
  92. sizer_2.Fit(self)
  93. sizer_2.SetSizeHints(self)
  94. self.Layout()
  95. # end wxGlade
  96.  
  97. def __attach_events(self):
  98. self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.button_ok.GetId())
  99. self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.button_cancel.GetId())
  100. def OnOK(self, events):
  101. """Update data wil new values and close dialog."""
  102. self.settings.echo = self.checkbox_echo.GetValue()
  103. self.settings.unprintable = self.checkbox_unprintable.GetValue()
  104. self.settings.newline = self.radio_box_newline.GetSelection()
  105. self.EndModal(wx.ID_OK)
  106. def OnCancel(self, events):
  107. """Do not update data but close dialog."""
  108. self.EndModal(wx.ID_CANCEL)
  109.  
  110. # end of class TerminalSettingsDialog
  111.  
  112.  
  113. class TerminalFrame(wx.Frame):
  114. """Simple terminal program for wxPython"""
  115. def __init__(self, *args, **kwds):
  116. self.serial = serial.Serial()
  117. self.serial.timeout = 0.5 #make sure that the alive event can be checked from time to time
  118. self.settings = TerminalSetup() #placeholder for the settings
  119. self.thread = None
  120. self.alive = threading.Event()
  121. # begin wxGlade: TerminalFrame.__init__
  122. kwds["style"] = wx.DEFAULT_FRAME_STYLE
  123. wx.Frame.__init__(self, *args, **kwds)
  124. self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY)
  125. # Menu Bar
  126. self.frame_terminal_menubar = wx.MenuBar()
  127. self.SetMenuBar(self.frame_terminal_menubar)
  128. wxglade_tmp_menu = wx.Menu()
  129. wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL)
  130. wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL)
  131. wxglade_tmp_menu.AppendSeparator()
  132. wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL)
  133. wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL)
  134. wxglade_tmp_menu.AppendSeparator()
  135. wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL)
  136. self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File")
  137. # Menu Bar end
  138.  
  139. self.__set_properties()
  140. self.__do_layout()
  141. # end wxGlade
  142. self.__attach_events() #register events
  143. self.OnPortSettings(None) #call setup dialog on startup, opens port
  144. if not self.alive.isSet():
  145. self.Close()
  146.  
  147. def StartThread(self):
  148. """Start the receiver thread"""
  149. self.thread = threading.Thread(target=self.ComPortThread)
  150. self.thread.setDaemon(1)
  151. self.alive.set()
  152. self.thread.start()
  153.  
  154. def StopThread(self):
  155. """Stop the receiver thread, wait util it's finished."""
  156. if self.thread is not None:
  157. self.alive.clear() #clear alive event for thread
  158. self.thread.join() #wait until thread has finished
  159. self.thread = None
  160. def __set_properties(self):
  161. # begin wxGlade: TerminalFrame.__set_properties
  162. self.SetTitle("Serial Terminal")
  163. self.SetSize((546, 383))
  164. # end wxGlade
  165.  
  166. def __do_layout(self):
  167. # begin wxGlade: TerminalFrame.__do_layout
  168. sizer_1 = wx.BoxSizer(wx.VERTICAL)
  169. sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0)
  170. self.SetAutoLayout(1)
  171. self.SetSizer(sizer_1)
  172. self.Layout()
  173. # end wxGlade
  174.  
  175. def __attach_events(self):
  176. #register events at the controls
  177. self.Bind(wx.EVT_MENU, self.OnClear, id = ID_CLEAR)
  178. self.Bind(wx.EVT_MENU, self.OnSaveAs, id = ID_SAVEAS)
  179. self.Bind(wx.EVT_MENU, self.OnExit, id = ID_EXIT)
  180. self.Bind(wx.EVT_MENU, self.OnPortSettings, id = ID_SETTINGS)
  181. self.Bind(wx.EVT_MENU, self.OnTermSettings, id = ID_TERM)
  182. self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey)
  183. self.Bind(EVT_SERIALRX, self.OnSerialRead)
  184. self.Bind(wx.EVT_CLOSE, self.OnClose)
  185.  
  186. def OnExit(self, event):
  187. """Menu point Exit"""
  188. self.Close()
  189.  
  190. def OnClose(self, event):
  191. """Called on application shutdown."""
  192. self.StopThread() #stop reader thread
  193. self.serial.close() #cleanup
  194. self.Destroy() #close windows, exit app
  195.  
  196. def OnSaveAs(self, event):
  197. """Save contents of output window."""
  198. filename = None
  199. dlg = wx.FileDialog(None, "Save Text As...", ".", "", "Text File|*.txt|All Files|*", wx.SAVE)
  200. if dlg.ShowModal() == wx.ID_OK:
  201. filename = dlg.GetPath()
  202. dlg.Destroy()
  203. if filename is not None:
  204. f = file(filename, 'w')
  205. text = self.text_ctrl_output.GetValue()
  206. if type(text) == unicode:
  207. text = text.encode("latin1") #hm, is that a good asumption?
  208. f.write(text)
  209. f.close()
  210. def OnClear(self, event):
  211. """Clear contents of output window."""
  212. self.text_ctrl_output.Clear()
  213. def OnPortSettings(self, event=None):
  214. """Show the portsettings dialog. The reader thread is stopped for the
  215. settings change."""
  216. if event is not None: #will be none when called on startup
  217. self.StopThread()
  218. self.serial.close()
  219. ok = False
  220. while not ok:
  221. dialog_serial_cfg = wxSerialConfigDialog.SerialConfigDialog(None, -1, "",
  222. show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW,
  223. serial=self.serial
  224. )
  225. result = dialog_serial_cfg.ShowModal()
  226. dialog_serial_cfg.Destroy()
  227. #open port if not called on startup, open it on startup and OK too
  228. if result == wx.ID_OK or event is not None:
  229. try:
  230. self.serial.open()
  231. except serial.SerialException, e:
  232. dlg = wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)
  233. dlg.ShowModal()
  234. dlg.Destroy()
  235. else:
  236. self.StartThread()
  237. self.SetTitle("Serial Terminal on %s [%s, %s%s%s%s%s]" % (
  238. self.serial.portstr,
  239. self.serial.baudrate,
  240. self.serial.bytesize,
  241. self.serial.parity,
  242. self.serial.stopbits,
  243. self.serial.rtscts and ' RTS/CTS' or '',
  244. self.serial.xonxoff and ' Xon/Xoff' or '',
  245. )
  246. )
  247. ok = True
  248. else:
  249. #on startup, dialog aborted
  250. self.alive.clear()
  251. ok = True
  252.  
  253. def OnTermSettings(self, event):
  254. """Menu point Terminal Settings. Show the settings dialog
  255. with the current terminal settings"""
  256. dialog = TerminalSettingsDialog(None, -1, "", settings=self.settings)
  257. result = dialog.ShowModal()
  258. dialog.Destroy()
  259. def OnKey(self, event):
  260. """Key event handler. if the key is in the ASCII range, write it to the serial port.
  261. Newline handling and local echo is also done here."""
  262. code = event.GetKeyCode()
  263. if code < 256: #is it printable?
  264. if code == 13: #is it a newline? (check for CR which is the RETURN key)
  265. if self.settings.echo: #do echo if needed
  266. self.text_ctrl_output.AppendText('\n')
  267. if self.settings.newline == NEWLINE_CR:
  268. self.serial.write('\r') #send CR
  269. elif self.settings.newline == NEWLINE_LF:
  270. self.serial.write('\n') #send LF
  271. elif self.settings.newline == NEWLINE_CRLF:
  272. self.serial.write('\r\n') #send CR+LF
  273. else:
  274. char = chr(code)
  275. if self.settings.echo: #do echo if needed
  276. self.text_ctrl_output.WriteText(char)
  277. self.serial.write(char) #send the charcater
  278. else:
  279. print "Extra Key:", code
  280.  
  281. def OnSerialRead(self, event):
  282. """Handle input from the serial port."""
  283. text = event.data
  284. if self.settings.unprintable:
  285. text = ''.join([(c >= ' ') and c or '<%d>' % ord(c) for c in text])
  286. self.text_ctrl_output.AppendText(text)
  287.  
  288. def ComPortThread(self):
  289. """Thread that handles the incomming traffic. Does the basic input
  290. transformation (newlines) and generates an SerialRxEvent"""
  291. while self.alive.isSet(): #loop while alive event is true
  292. text = self.serial.read(1) #read one, with timout
  293. if text: #check if not timeout
  294. n = self.serial.inWaiting() #look if there is more to read
  295. if n:
  296. text = text + self.serial.read(n) #get it
  297. #newline transformation
  298. if self.settings.newline == NEWLINE_CR:
  299. text = text.replace('\r', '\n')
  300. elif self.settings.newline == NEWLINE_LF:
  301. pass
  302. elif self.settings.newline == NEWLINE_CRLF:
  303. text = text.replace('\r\n', '\n')
  304. event = SerialRxEvent(self.GetId(), text)
  305. self.GetEventHandler().AddPendingEvent(event)
  306. #~ self.OnSerialRead(text) #output text in window
  307. # end of class TerminalFrame
  308.  
  309.  
  310. class MyApp(wx.App):
  311. def OnInit(self):
  312. wx.InitAllImageHandlers()
  313. frame_terminal = TerminalFrame(None, -1, "")
  314. self.SetTopWindow(frame_terminal)
  315. frame_terminal.Show(1)
  316. return 1
  317.  
  318. # end of class MyApp
  319.  
  320. if __name__ == "__main__":
  321. app = MyApp(0)
  322. app.MainLoop()