root/trunk/twisted/internet/serialport.py

Revision 33004, 2.2 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# http://twistedmatrix.com/trac/ticket/3725#comment:24
10# Apparently applications use these names even though they should
11# be imported from pyserial
12__all__ = ["serial", "PARITY_ODD", "PARITY_EVEN", "PARITY_NONE",
13           "STOPBITS_TWO", "STOPBITS_ONE", "FIVEBITS",
14           "EIGHTBITS", "SEVENBITS", "SIXBITS",
15# Name this module is actually trying to export
16           "SerialPort"]
17
18# system imports
19import os, sys
20
21# all of them require pyserial at the moment, so check that first
22import serial
23from serial import PARITY_NONE, PARITY_EVEN, PARITY_ODD
24from serial import STOPBITS_ONE, STOPBITS_TWO
25from serial import FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS
26
27
28
29class BaseSerialPort:
30    """
31    Base class for Windows and POSIX serial ports.
32
33    @ivar _serialFactory: a pyserial C{serial.Serial} factory, used to create
34        the instance stored in C{self._serial}. Overrideable to enable easier
35        testing.
36
37    @ivar _serial: a pyserial C{serial.Serial} instance used to manage the
38        options on the serial port.
39    """
40
41    _serialFactory = serial.Serial
42
43
44    def setBaudRate(self, baudrate):
45        if hasattr(self._serial, "setBaudrate"):
46            self._serial.setBaudrate(baudrate)
47        else:
48            self._serial.setBaudRate(baudrate)
49
50    def inWaiting(self):
51        return self._serial.inWaiting()
52
53    def flushInput(self):
54        self._serial.flushInput()
55
56    def flushOutput(self):
57        self._serial.flushOutput()
58
59    def sendBreak(self):
60        self._serial.sendBreak()
61
62    def getDSR(self):
63        return self._serial.getDSR()
64
65    def getCD(self):
66        return self._serial.getCD()
67
68    def getRI(self):
69        return self._serial.getRI()
70
71    def getCTS(self):
72        return self._serial.getCTS()
73
74    def setDTR(self, on = 1):
75        self._serial.setDTR(on)
76
77    def setRTS(self, on = 1):
78        self._serial.setRTS(on)
79
80class SerialPort(BaseSerialPort):
81    pass
82
83# replace SerialPort with appropriate serial port
84if os.name == 'posix':
85    from twisted.internet._posixserialport import SerialPort
86elif sys.platform == 'win32':
87    from twisted.internet._win32serialport import SerialPort
Note: See TracBrowser for help on using the browser.