root/trunk/twisted/internet/_posixserialport.py

Revision 33004, 2.0 KB (checked in by itamarst, 7 months ago)

Merge serialport-connectionlost-3690-4.

Author: itamar
Review: exarkun
Fixes: #3690

Serial ports now correctly call connectionLost on the protocol when disconnected.

Line 
1# Copyright (c) Twisted Matrix Laboratories.
2# See LICENSE for details.
3
4
5"""
6Serial Port Protocol
7"""
8
9# system imports
10import os, errno
11
12# dependent on pyserial ( http://pyserial.sf.net/ )
13# only tested w/ 1.18 (5 Dec 2002)
14import serial
15from serial import PARITY_NONE, PARITY_EVEN, PARITY_ODD
16from serial import STOPBITS_ONE, STOPBITS_TWO
17from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
18
19from serialport import BaseSerialPort
20
21# twisted imports
22from twisted.internet import abstract, fdesc, main
23
24class 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 = self._serialFactory(
36            deviceNameOrPortNumber, baudrate=baudrate, bytesize=bytesize,
37            parity=parity, stopbits=stopbits, timeout=timeout,
38            xonxoff=xonxoff, rtscts=rtscts)
39        self.reactor = reactor
40        self.flushInput()
41        self.flushOutput()
42        self.protocol = protocol
43        self.protocol.makeConnection(self)
44        self.startReading()
45
46
47    def fileno(self):
48        return self._serial.fd
49
50
51    def writeSomeData(self, data):
52        """
53        Write some data to the serial device.
54        """
55        return fdesc.writeToFD(self.fileno(), data)
56
57
58    def doRead(self):
59        """
60        Some data's readable from serial device.
61        """
62        return fdesc.readFromFD(self.fileno(), self.protocol.dataReceived)
63
64
65    def connectionLost(self, reason):
66        """
67        Called when the serial port disconnects.
68
69        Will call C{connectionLost} on the protocol that is handling the
70        serial data.
71        """
72        abstract.FileDescriptor.connectionLost(self, reason)
73        self._serial.close()
74        self.protocol.connectionLost(reason)
Note: See TracBrowser for help on using the browser.