1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
2
# See LICENSE for details.
12
# dependent on pyserial ( http://pyserial.sf.net/ )
13
# only tested w/ 1.18 (5 Dec 2002)
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
21
from twisted.internet import abstract, javareactor, main
22
from twisted.python import log
24
class SerialPort(BaseSerialPort, javareactor.JConnection):
25
"""A select()able serial device, acting as a transport."""
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, None)
37
self.reactor = reactor
38
self.protocol = protocol
39
self.protocol.makeConnection(self)
40
wb = javareactor.WriteBlocker(self, reactor.q)
42
self.writeBlocker = wb
43
javareactor.ReadBlocker(self, reactor.q).start()
45
def writeSomeData(self, data):
47
self._serial.write(data)
49
# should have something better here
51
return main.CONNECTION_LOST
56
readBytes = self._serial.read(min(8192, self.inWaiting()))
58
return main.CONNECTION_LOST
60
return main.CONNECTION_LOST
61
self.protocol.dataReceived(readBytes)
63
def connectionLost(self, reason):
65
self.protocol.connectionLost(reason)
66
abstract.FileDescriptor.connectionLost(self, reason)
69
raise NotImplementedError
72
raise NotImplementedError
74
def getTcpNoDelay(self):
75
raise NotImplementedError
77
def setTcpNoDelay(self, enabled):
78
raise NotImplementedError