serialport.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Serial Port Protocol
  5. """
  6. from __future__ import division, absolute_import
  7. # http://twistedmatrix.com/trac/ticket/3725#comment:24
  8. # Apparently applications use these names even though they should
  9. # be imported from pyserial
  10. __all__ = ["serial", "PARITY_ODD", "PARITY_EVEN", "PARITY_NONE",
  11. "STOPBITS_TWO", "STOPBITS_ONE", "FIVEBITS",
  12. "EIGHTBITS", "SEVENBITS", "SIXBITS",
  13. # Name this module is actually trying to export
  14. "SerialPort"]
  15. # all of them require pyserial at the moment, so check that first
  16. import serial
  17. from serial import PARITY_NONE, PARITY_EVEN, PARITY_ODD
  18. from serial import STOPBITS_ONE, STOPBITS_TWO
  19. from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
  20. from twisted.python._oldstyle import _oldStyle
  21. from twisted.python.runtime import platform
  22. @_oldStyle
  23. class BaseSerialPort:
  24. """
  25. Base class for Windows and POSIX serial ports.
  26. @ivar _serialFactory: a pyserial C{serial.Serial} factory, used to create
  27. the instance stored in C{self._serial}. Overrideable to enable easier
  28. testing.
  29. @ivar _serial: a pyserial C{serial.Serial} instance used to manage the
  30. options on the serial port.
  31. """
  32. _serialFactory = serial.Serial
  33. def setBaudRate(self, baudrate):
  34. if hasattr(self._serial, "setBaudrate"):
  35. self._serial.setBaudrate(baudrate)
  36. else:
  37. self._serial.setBaudRate(baudrate)
  38. def inWaiting(self):
  39. return self._serial.inWaiting()
  40. def flushInput(self):
  41. self._serial.flushInput()
  42. def flushOutput(self):
  43. self._serial.flushOutput()
  44. def sendBreak(self):
  45. self._serial.sendBreak()
  46. def getDSR(self):
  47. return self._serial.getDSR()
  48. def getCD(self):
  49. return self._serial.getCD()
  50. def getRI(self):
  51. return self._serial.getRI()
  52. def getCTS(self):
  53. return self._serial.getCTS()
  54. def setDTR(self, on = 1):
  55. self._serial.setDTR(on)
  56. def setRTS(self, on = 1):
  57. self._serial.setRTS(on)
  58. # Expert appropriate implementation of SerialPort.
  59. if platform.isWindows():
  60. from twisted.internet._win32serialport import SerialPort
  61. else:
  62. from twisted.internet._posixserialport import SerialPort