| 1 | # Copyright (c) Twisted Matrix Laboratories. |
|---|
| 2 | # See LICENSE for details. |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | Serial Port Protocol |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | # http://twistedmatrix.com/trac/ticket/3725#comment:24 |
|---|
| 10 | # Apparently applications use these names even though they should |
|---|
| 11 | # be imported from pyserial |
|---|
| 12 | __all__ = ["serial", "PARITY_ODD", "PARITY_EVEN", "PARITY_NONE", |
|---|
| 13 | "STOPBITS_TWO", "STOPBITS_ONE", "FIVEBITS", |
|---|
| 14 | "EIGHTBITS", "SEVENBITS", "SIXBITS", |
|---|
| 15 | # Name this module is actually trying to export |
|---|
| 16 | "SerialPort"] |
|---|
| 17 | |
|---|
| 18 | # system imports |
|---|
| 19 | import os, sys |
|---|
| 20 | |
|---|
| 21 | # all of them require pyserial at the moment, so check that first |
|---|
| 22 | import serial |
|---|
| 23 | from serial import PARITY_NONE, PARITY_EVEN, PARITY_ODD |
|---|
| 24 | from serial import STOPBITS_ONE, STOPBITS_TWO |
|---|
| 25 | from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class BaseSerialPort: |
|---|
| 30 | """ |
|---|
| 31 | Base class for Windows and POSIX serial ports. |
|---|
| 32 | |
|---|
| 33 | @ivar _serialFactory: a pyserial C{serial.Serial} factory, used to create |
|---|
| 34 | the instance stored in C{self._serial}. Overrideable to enable easier |
|---|
| 35 | testing. |
|---|
| 36 | |
|---|
| 37 | @ivar _serial: a pyserial C{serial.Serial} instance used to manage the |
|---|
| 38 | options on the serial port. |
|---|
| 39 | """ |
|---|
| 40 | |
|---|
| 41 | _serialFactory = serial.Serial |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | def setBaudRate(self, baudrate): |
|---|
| 45 | if hasattr(self._serial, "setBaudrate"): |
|---|
| 46 | self._serial.setBaudrate(baudrate) |
|---|
| 47 | else: |
|---|
| 48 | self._serial.setBaudRate(baudrate) |
|---|
| 49 | |
|---|
| 50 | def inWaiting(self): |
|---|
| 51 | return self._serial.inWaiting() |
|---|
| 52 | |
|---|
| 53 | def flushInput(self): |
|---|
| 54 | self._serial.flushInput() |
|---|
| 55 | |
|---|
| 56 | def flushOutput(self): |
|---|
| 57 | self._serial.flushOutput() |
|---|
| 58 | |
|---|
| 59 | def sendBreak(self): |
|---|
| 60 | self._serial.sendBreak() |
|---|
| 61 | |
|---|
| 62 | def getDSR(self): |
|---|
| 63 | return self._serial.getDSR() |
|---|
| 64 | |
|---|
| 65 | def getCD(self): |
|---|
| 66 | return self._serial.getCD() |
|---|
| 67 | |
|---|
| 68 | def getRI(self): |
|---|
| 69 | return self._serial.getRI() |
|---|
| 70 | |
|---|
| 71 | def getCTS(self): |
|---|
| 72 | return self._serial.getCTS() |
|---|
| 73 | |
|---|
| 74 | def setDTR(self, on = 1): |
|---|
| 75 | self._serial.setDTR(on) |
|---|
| 76 | |
|---|
| 77 | def setRTS(self, on = 1): |
|---|
| 78 | self._serial.setRTS(on) |
|---|
| 79 | |
|---|
| 80 | class SerialPort(BaseSerialPort): |
|---|
| 81 | pass |
|---|
| 82 | |
|---|
| 83 | # replace SerialPort with appropriate serial port |
|---|
| 84 | if os.name == 'posix': |
|---|
| 85 | from twisted.internet._posixserialport import SerialPort |
|---|
| 86 | elif sys.platform == 'win32': |
|---|
| 87 | from twisted.internet._win32serialport import SerialPort |
|---|