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
19
from serialport import BaseSerialPort
22
from twisted.internet import abstract, fdesc, main
24
class SerialPort(BaseSerialPort, abstract.FileDescriptor):
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 = 0, xonxoff = 0, rtscts = 0):
31
abstract.FileDescriptor.__init__(self, reactor)
32
self._serial = serial.Serial(deviceNameOrPortNumber, baudrate = baudrate, bytesize = bytesize, parity = parity, stopbits = stopbits, timeout = timeout, xonxoff = xonxoff, rtscts = rtscts)
33
self.reactor = reactor
36
self.protocol = protocol
37
self.protocol.makeConnection(self)
41
return self._serial.fd
43
def writeSomeData(self, data):
44
"""Write some data to the serial device.
47
return os.write(self.fileno(), data)
49
if io.args[0] == errno.EAGAIN:
51
return main.CONNECTION_LOST
53
if ose.errno == errno.EAGAIN:
54
# I think most systems use this one
59
"""Some data's readable from serial device.
61
return fdesc.readFromFD(self.fileno(), self.protocol.dataReceived)
63
def connectionLost(self, reason):
64
abstract.FileDescriptor.connectionLost(self, reason)