ocxserialtest.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # ocxserialtest.py
  2. #
  3. # Sample that uses the mscomm OCX to talk to a serial
  4. # device.
  5. # Very simple - queries a modem for ATI responses
  6. import win32ui, win32uiole
  7. import win32con
  8. from pywin.mfc import dialog, activex
  9. from win32com.client import gencache
  10. import pythoncom
  11. SERIAL_SETTINGS = '19200,n,8,1'
  12. SERIAL_PORT = 2
  13. win32ui.DoWaitCursor(1)
  14. serialModule = gencache.EnsureModule("{648A5603-2C6E-101B-82B6-000000000014}", 0, 1, 1)
  15. win32ui.DoWaitCursor(0)
  16. if serialModule is None:
  17. raise ImportError("MS COMM Control does not appear to be installed on the PC")
  18. def MakeDlgTemplate():
  19. style = win32con.DS_MODALFRAME | win32con.WS_POPUP \
  20. | win32con.WS_VISIBLE | win32con.WS_CAPTION \
  21. | win32con.WS_SYSMENU | win32con.DS_SETFONT
  22. cs = win32con.WS_CHILD | win32con.WS_VISIBLE
  23. dlg = [ ["Very Basic Terminal",
  24. (0, 0, 350, 180), style, None, (8, "MS Sans Serif")], ]
  25. s = win32con.WS_TABSTOP | cs
  26. dlg.append(["RICHEDIT", None, 132, (5, 5, 340, 170),s | win32con.ES_WANTRETURN | win32con.ES_MULTILINE | win32con.ES_AUTOVSCROLL | win32con.WS_VSCROLL])
  27. return dlg
  28. ####################################
  29. #
  30. # Serial Control
  31. #
  32. class MySerialControl(activex.Control, serialModule.MSComm):
  33. def __init__(self, parent):
  34. activex.Control.__init__(self)
  35. serialModule.MSComm.__init__(self)
  36. self.parent = parent
  37. def OnComm(self):
  38. self.parent.OnComm()
  39. class TestSerDialog(dialog.Dialog):
  40. def __init__(self, *args):
  41. dialog.Dialog.__init__(*(self,)+args)
  42. self.olectl = None
  43. def OnComm(self):
  44. event = self.olectl.CommEvent
  45. if event == serialModule.OnCommConstants.comEvReceive:
  46. self.editwindow.ReplaceSel(self.olectl.Input)
  47. def OnKey(self, key):
  48. if self.olectl:
  49. self.olectl.Output = chr(key)
  50. def OnInitDialog(self):
  51. rc = dialog.Dialog.OnInitDialog(self)
  52. self.editwindow = self.GetDlgItem(132)
  53. self.editwindow.HookAllKeyStrokes(self.OnKey)
  54. self.olectl = MySerialControl(self)
  55. try:
  56. self.olectl.CreateControl("OCX",
  57. win32con.WS_TABSTOP | win32con.WS_VISIBLE,
  58. (7,43,500,300), self._obj_, 131)
  59. except win32ui.error:
  60. self.MessageBox("The Serial Control could not be created")
  61. self.olectl = None
  62. self.EndDialog(win32con.IDCANCEL)
  63. if self.olectl:
  64. self.olectl.Settings = SERIAL_SETTINGS
  65. self.olectl.CommPort = SERIAL_PORT
  66. self.olectl.RThreshold = 1
  67. try:
  68. self.olectl.PortOpen = 1
  69. except pythoncom.com_error as details:
  70. print("Could not open the specified serial port - %s" % (details.excepinfo[2]))
  71. self.EndDialog(win32con.IDCANCEL)
  72. return rc
  73. def OnDestroy(self, msg):
  74. if self.olectl:
  75. try:
  76. self.olectl.PortOpen = 0
  77. except pythoncom.com_error as details:
  78. print("Error closing port - %s" % (details.excepinfo[2]))
  79. return dialog.Dialog.OnDestroy(self, msg)
  80. def test():
  81. d = TestSerDialog(MakeDlgTemplate() )
  82. d.DoModal()
  83. if __name__ == "__main__":
  84. from . import demoutils
  85. if demoutils.NeedGoodGUI():
  86. test()