~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/twisted/internet/_posixserialport.py

  • Committer: Thomas Hervé
  • Date: 2009-07-08 13:52:04 UTC
  • Revision ID: thomas@canonical.com-20090708135204-df5eesrthifpylf8
Remove twisted copy

Show diffs side-by-side

added added

removed removed

Lines of Context:
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, errno
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
 
 
19
 
from serialport import BaseSerialPort
20
 
 
21
 
# twisted imports
22
 
from twisted.internet import abstract, fdesc, main
23
 
 
24
 
class SerialPort(BaseSerialPort, abstract.FileDescriptor):
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 = 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
34
 
        self.flushInput()
35
 
        self.flushOutput()
36
 
        self.protocol = protocol
37
 
        self.protocol.makeConnection(self)
38
 
        self.startReading()
39
 
 
40
 
    def fileno(self):
41
 
        return self._serial.fd
42
 
 
43
 
    def writeSomeData(self, data):
44
 
        """Write some data to the serial device.
45
 
        """
46
 
        try:
47
 
            return os.write(self.fileno(), data)
48
 
        except IOError, io:
49
 
            if io.args[0] == errno.EAGAIN:
50
 
                return 0
51
 
            return main.CONNECTION_LOST
52
 
        except OSError, ose:
53
 
            if ose.errno == errno.EAGAIN:
54
 
                # I think most systems use this one
55
 
                return 0
56
 
            raise
57
 
 
58
 
    def doRead(self):
59
 
        """Some data's readable from serial device.
60
 
        """
61
 
        return fdesc.readFromFD(self.fileno(), self.protocol.dataReceived)
62
 
 
63
 
    def connectionLost(self, reason):
64
 
        abstract.FileDescriptor.connectionLost(self, reason)
65
 
        self._serial.close()