| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | Serial Port Protocol |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | __all__ = ["serial", "PARITY_ODD", "PARITY_EVEN", "PARITY_NONE", |
|---|
| 13 | "STOPBITS_TWO", "STOPBITS_ONE", "FIVEBITS", |
|---|
| 14 | "EIGHTBITS", "SEVENBITS", "SIXBITS", |
|---|
| 15 | |
|---|
| 16 | "SerialPort"] |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | import os, sys |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 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 | class BaseSerialPort: |
|---|
| 29 | def setBaudRate(self, baudrate): |
|---|
| 30 | if hasattr(self._serial, "setBaudrate"): |
|---|
| 31 | self._serial.setBaudrate(baudrate) |
|---|
| 32 | else: |
|---|
| 33 | self._serial.setBaudRate(baudrate) |
|---|
| 34 | |
|---|
| 35 | def inWaiting(self): |
|---|
| 36 | return self._serial.inWaiting() |
|---|
| 37 | |
|---|
| 38 | def flushInput(self): |
|---|
| 39 | self._serial.flushInput() |
|---|
| 40 | |
|---|
| 41 | def flushOutput(self): |
|---|
| 42 | self._serial.flushOutput() |
|---|
| 43 | |
|---|
| 44 | def sendBreak(self): |
|---|
| 45 | self._serial.sendBreak() |
|---|
| 46 | |
|---|
| 47 | def getDSR(self): |
|---|
| 48 | return self._serial.getDSR() |
|---|
| 49 | |
|---|
| 50 | def getCD(self): |
|---|
| 51 | return self._serial.getCD() |
|---|
| 52 | |
|---|
| 53 | def getRI(self): |
|---|
| 54 | return self._serial.getRI() |
|---|
| 55 | |
|---|
| 56 | def getCTS(self): |
|---|
| 57 | return self._serial.getCTS() |
|---|
| 58 | |
|---|
| 59 | def setDTR(self, on = 1): |
|---|
| 60 | self._serial.setDTR(on) |
|---|
| 61 | |
|---|
| 62 | def setRTS(self, on = 1): |
|---|
| 63 | self._serial.setRTS(on) |
|---|
| 64 | |
|---|
| 65 | class SerialPort(BaseSerialPort): |
|---|
| 66 | pass |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | if os.name == 'posix': |
|---|
| 70 | from twisted.internet._posixserialport import SerialPort |
|---|
| 71 | elif sys.platform == 'win32': |
|---|
| 72 | from twisted.internet._win32serialport import SerialPort |
|---|