| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 | |
| 5 """ | |
| 6 Serial Port Protocol | |
| 7 """ | |
| 8 | |
| 9 # system imports | |
| 10 import os | |
| 11 | |
| 12 # dependent on pyserial ( http://pyserial.sf.net/ ) | |
| 13 # only tested w/ 1.18 (5 Dec 2002) | |
| 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 from serialport import BaseSerialPort | |
| 19 | |
| 20 # twisted imports | |
| 21 from twisted.internet import abstract, javareactor, main | |
| 22 from twisted.python import log | |
| 23 | |
| 24 class SerialPort(BaseSerialPort, javareactor.JConnection): | |
| 25 """A select()able serial device, acting as a transport.""" | |
| 26 connected = 1 | |
| 27 | |
| 28 def __init__(self, protocol, deviceNameOrPortNumber, reactor, | |
| 29 baudrate = 9600, bytesize = EIGHTBITS, parity = PARITY_NONE, | |
| 30 stopbits = STOPBITS_ONE, timeout = 3, xonxoff = 0, rtscts = 0): | |
| 31 # do NOT use timeout = 0 !! | |
| 32 self._serial = serial.Serial(deviceNameOrPortNumber, baudrate = baudrate
, bytesize = bytesize, parity = parity, stopbits = stopbits, timeout = timeout,
xonxoff = xonxoff, rtscts = rtscts) | |
| 33 javareactor.JConnection.__init__(self, self._serial.sPort, protocol, Non
e) | |
| 34 self.flushInput() | |
| 35 self.flushOutput() | |
| 36 | |
| 37 self.reactor = reactor | |
| 38 self.protocol = protocol | |
| 39 self.protocol.makeConnection(self) | |
| 40 wb = javareactor.WriteBlocker(self, reactor.q) | |
| 41 wb.start() | |
| 42 self.writeBlocker = wb | |
| 43 javareactor.ReadBlocker(self, reactor.q).start() | |
| 44 | |
| 45 def writeSomeData(self, data): | |
| 46 try: | |
| 47 self._serial.write(data) | |
| 48 return len(data) | |
| 49 # should have something better here | |
| 50 except Exception, e: | |
| 51 return main.CONNECTION_LOST | |
| 52 | |
| 53 def doRead(self): | |
| 54 readBytes = '' | |
| 55 try: | |
| 56 readBytes = self._serial.read(min(8192, self.inWaiting())) | |
| 57 except Exception, e: | |
| 58 return main.CONNECTION_LOST | |
| 59 if not readBytes: | |
| 60 return main.CONNECTION_LOST | |
| 61 self.protocol.dataReceived(readBytes) | |
| 62 | |
| 63 def connectionLost(self, reason): | |
| 64 self._serial.close() | |
| 65 self.protocol.connectionLost(reason) | |
| 66 abstract.FileDescriptor.connectionLost(self, reason) | |
| 67 | |
| 68 def getHost(self): | |
| 69 raise NotImplementedError | |
| 70 | |
| 71 def getPeer(self): | |
| 72 raise NotImplementedError | |
| 73 | |
| 74 def getTcpNoDelay(self): | |
| 75 raise NotImplementedError | |
| 76 | |
| 77 def setTcpNoDelay(self, enabled): | |
| 78 raise NotImplementedError | |
| OLD | NEW |