| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | Serial Port Protocol |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | import os, errno |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | import serial |
|---|
| 15 | from serial import PARITY_NONE, PARITY_EVEN, PARITY_ODD |
|---|
| 16 | from serial import STOPBITS_ONE, STOPBITS_TWO |
|---|
| 17 | from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS |
|---|
| 18 | |
|---|
| 19 | from serialport import BaseSerialPort |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | from twisted.internet import abstract, fdesc, main |
|---|
| 23 | |
|---|
| 24 | class SerialPort(BaseSerialPort, abstract.FileDescriptor): |
|---|
| 25 | """ |
|---|
| 26 | A select()able serial device, acting as a transport. |
|---|
| 27 | """ |
|---|
| 28 | |
|---|
| 29 | connected = 1 |
|---|
| 30 | |
|---|
| 31 | def __init__(self, protocol, deviceNameOrPortNumber, reactor, |
|---|
| 32 | baudrate = 9600, bytesize = EIGHTBITS, parity = PARITY_NONE, |
|---|
| 33 | stopbits = STOPBITS_ONE, timeout = 0, xonxoff = 0, rtscts = 0): |
|---|
| 34 | abstract.FileDescriptor.__init__(self, reactor) |
|---|
| 35 | self._serial = serial.Serial(deviceNameOrPortNumber, baudrate = baudrate, bytesize = bytesize, parity = parity, stopbits = stopbits, timeout = timeout, xonxoff = xonxoff, rtscts = rtscts) |
|---|
| 36 | self.reactor = reactor |
|---|
| 37 | self.flushInput() |
|---|
| 38 | self.flushOutput() |
|---|
| 39 | self.protocol = protocol |
|---|
| 40 | self.protocol.makeConnection(self) |
|---|
| 41 | self.startReading() |
|---|
| 42 | |
|---|
| 43 | def fileno(self): |
|---|
| 44 | return self._serial.fd |
|---|
| 45 | |
|---|
| 46 | def writeSomeData(self, data): |
|---|
| 47 | """ |
|---|
| 48 | Write some data to the serial device. |
|---|
| 49 | """ |
|---|
| 50 | return fdesc.writeToFD(self.fileno(), data) |
|---|
| 51 | |
|---|
| 52 | def doRead(self): |
|---|
| 53 | """ |
|---|
| 54 | Some data's readable from serial device. |
|---|
| 55 | """ |
|---|
| 56 | return fdesc.readFromFD(self.fileno(), self.protocol.dataReceived) |
|---|
| 57 | |
|---|
| 58 | def connectionLost(self, reason): |
|---|
| 59 | abstract.FileDescriptor.connectionLost(self, reason) |
|---|
| 60 | self._serial.close() |
|---|