~ampelbein/ubuntu/natty/pyserial/lp-715766

« back to all changes in this revision

Viewing changes to serial/serialwin32.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-01-25 07:59:17 UTC
  • mfrom: (3.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110125075917-m132a8pxfff5a7sf
Tags: 2.5-1
* New upstream version. Closes: #520618.
* Build a python3-serial package.
* Don't use string exception in miniterm.py. Closes: #585328.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! python
2
 
#Python Serial Port Extension for Win32, Linux, BSD, Jython
3
 
#serial driver for win32
4
 
#see __init__.py
 
2
# Python Serial Port Extension for Win32, Linux, BSD, Jython
 
3
# serial driver for win32
 
4
# see __init__.py
5
5
#
6
 
#(C) 2001-2003 Chris Liechti <cliechti@gmx.net>
 
6
# (C) 2001-2009 Chris Liechti <cliechti@gmx.net>
7
7
# this is distributed under a free software license, see license.txt
8
 
 
9
 
import win32file  # The base COM port and file IO functions.
10
 
import win32event # We use events and the WaitFor[Single|Multiple]Objects functions.
11
 
import win32con   # constants.
 
8
#
 
9
# Initial patch to use ctypes by Giovanni Bajo <rasky@develer.com>
 
10
 
 
11
import ctypes
 
12
import win32
 
13
 
12
14
from serialutil import *
13
15
 
14
 
VERSION = "$Revision: 1.40 $".split()[1]     #extract CVS version
15
 
 
16
 
#from winbase.h. these should realy be in win32con
17
 
MS_CTS_ON  = 16
18
 
MS_DSR_ON  = 32
19
 
MS_RING_ON = 64
20
 
MS_RLSD_ON = 128
21
16
 
22
17
def device(portnum):
23
18
    """Turn a port number into a device name"""
24
 
    return 'COM%d' % (portnum+1) #numbers are transformed to a string
25
 
 
26
 
class Serial(SerialBase):
27
 
    """Serial port implemenation for Win32. This implemenatation requires a 
28
 
       win32all installation."""
29
 
 
30
 
    BAUDRATES = (50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,
31
 
                 19200,38400,57600,115200)
 
19
    return 'COM%d' % (portnum+1) # numbers are transformed to a string
 
20
 
 
21
 
 
22
class Win32Serial(SerialBase):
 
23
    """Serial port implementation for Win32 based on ctypes."""
 
24
 
 
25
    BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 
26
                 9600, 19200, 38400, 57600, 115200)
 
27
 
 
28
    def __init__(self, *args, **kwargs):
 
29
        self.hComPort = None
 
30
        SerialBase.__init__(self, *args, **kwargs)
32
31
 
33
32
    def open(self):
34
33
        """Open port with current settings. This may throw a SerialException
35
34
           if the port cannot be opened."""
36
35
        if self._port is None:
37
36
            raise SerialException("Port must be configured before it can be used.")
38
 
        self.hComPort = None
 
37
        # the "\\.\COMx" format is required for devices other than COM1-COM8
 
38
        # not all versions of windows seem to support this properly
 
39
        # so that the first few ports are used with the DOS device name
 
40
        port = self.portstr
39
41
        try:
40
 
            self.hComPort = win32file.CreateFile(self.makeDeviceName(self.portstr),
41
 
                   win32con.GENERIC_READ | win32con.GENERIC_WRITE,
42
 
                   0, # exclusive access
43
 
                   None, # no security
44
 
                   win32con.OPEN_EXISTING,
45
 
                   win32con.FILE_FLAG_OVERLAPPED,
46
 
                   None)
47
 
        except Exception, msg:
48
 
            self.hComPort = None    #'cause __del__ is called anyway
49
 
            raise SerialException("could not open port %s: %s" % (self.portstr, msg))
 
42
            if port.upper().startswith('COM') and int(port[3:]) > 8:
 
43
                port = '\\\\.\\' + port
 
44
        except ValueError:
 
45
            # for like COMnotanumber
 
46
            pass
 
47
        self.hComPort = win32.CreateFile(port,
 
48
               win32.GENERIC_READ | win32.GENERIC_WRITE,
 
49
               0, # exclusive access
 
50
               None, # no security
 
51
               win32.OPEN_EXISTING,
 
52
               win32.FILE_ATTRIBUTE_NORMAL | win32.FILE_FLAG_OVERLAPPED,
 
53
               0)
 
54
        if self.hComPort == win32.INVALID_HANDLE_VALUE:
 
55
            self.hComPort = None    # 'cause __del__ is called anyway
 
56
            raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
 
57
 
50
58
        # Setup a 4k buffer
51
 
        win32file.SetupComm(self.hComPort, 4096, 4096)
52
 
 
53
 
        #Save original timeout values:
54
 
        self._orgTimeouts = win32file.GetCommTimeouts(self.hComPort)
55
 
 
56
 
        self._rtsState = win32file.RTS_CONTROL_ENABLE
57
 
        self._dtrState = win32file.DTR_CONTROL_ENABLE
 
59
        win32.SetupComm(self.hComPort, 4096, 4096)
 
60
 
 
61
        # Save original timeout values:
 
62
        self._orgTimeouts = win32.COMMTIMEOUTS()
 
63
        win32.GetCommTimeouts(self.hComPort, ctypes.byref(self._orgTimeouts))
 
64
 
 
65
        self._rtsState = win32.RTS_CONTROL_ENABLE
 
66
        self._dtrState = win32.DTR_CONTROL_ENABLE
58
67
 
59
68
        self._reconfigurePort()
60
 
        
 
69
 
61
70
        # Clear buffers:
62
71
        # Remove anything that was there
63
 
        win32file.PurgeComm(self.hComPort,
64
 
                            win32file.PURGE_TXCLEAR | win32file.PURGE_TXABORT |
65
 
                            win32file.PURGE_RXCLEAR | win32file.PURGE_RXABORT)
 
72
        win32.PurgeComm(self.hComPort,
 
73
                            win32.PURGE_TXCLEAR | win32.PURGE_TXABORT |
 
74
                            win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)
66
75
 
67
 
        self._overlappedRead = win32file.OVERLAPPED()
68
 
        self._overlappedRead.hEvent = win32event.CreateEvent(None, 1, 0, None)
69
 
        self._overlappedWrite = win32file.OVERLAPPED()
70
 
        #~ self._overlappedWrite.hEvent = win32event.CreateEvent(None, 1, 0, None)
71
 
        self._overlappedWrite.hEvent = win32event.CreateEvent(None, 0, 0, None)
 
76
        self._overlappedRead = win32.OVERLAPPED()
 
77
        self._overlappedRead.hEvent = win32.CreateEvent(None, 1, 0, None)
 
78
        self._overlappedWrite = win32.OVERLAPPED()
 
79
        #~ self._overlappedWrite.hEvent = win32.CreateEvent(None, 1, 0, None)
 
80
        self._overlappedWrite.hEvent = win32.CreateEvent(None, 0, 0, None)
72
81
        self._isOpen = True
73
82
 
74
83
    def _reconfigurePort(self):
75
 
        """Set commuication parameters on opened port."""
 
84
        """Set communication parameters on opened port."""
76
85
        if not self.hComPort:
77
86
            raise SerialException("Can only operate on a valid port handle")
78
 
        
79
 
        #Set Windows timeout values
80
 
        #timeouts is a tuple with the following items:
81
 
        #(ReadIntervalTimeout,ReadTotalTimeoutMultiplier,
82
 
        # ReadTotalTimeoutConstant,WriteTotalTimeoutMultiplier,
83
 
        # WriteTotalTimeoutConstant)
 
87
 
 
88
        # Set Windows timeout values
 
89
        # timeouts is a tuple with the following items:
 
90
        # (ReadIntervalTimeout,ReadTotalTimeoutMultiplier,
 
91
        #  ReadTotalTimeoutConstant,WriteTotalTimeoutMultiplier,
 
92
        #  WriteTotalTimeoutConstant)
84
93
        if self._timeout is None:
85
94
            timeouts = (0, 0, 0, 0, 0)
86
95
        elif self._timeout == 0:
87
 
            timeouts = (win32con.MAXDWORD, 0, 0, 0, 0)
 
96
            timeouts = (win32.MAXDWORD, 0, 0, 0, 0)
88
97
        else:
89
98
            timeouts = (0, 0, int(self._timeout*1000), 0, 0)
 
99
        if self._timeout != 0 and self._interCharTimeout is not None:
 
100
            timeouts = (int(self._interCharTimeout * 1000),) + timeouts[1:]
 
101
 
90
102
        if self._writeTimeout is None:
91
103
            pass
92
104
        elif self._writeTimeout == 0:
93
 
            timeouts = timeouts[:-2] + (0, win32con.MAXDWORD)
 
105
            timeouts = timeouts[:-2] + (0, win32.MAXDWORD)
94
106
        else:
95
107
            timeouts = timeouts[:-2] + (0, int(self._writeTimeout*1000))
96
 
        win32file.SetCommTimeouts(self.hComPort, timeouts)
 
108
        win32.SetCommTimeouts(self.hComPort, ctypes.byref(win32.COMMTIMEOUTS(*timeouts)))
97
109
 
98
 
        win32file.SetCommMask(self.hComPort, win32file.EV_ERR)
 
110
        win32.SetCommMask(self.hComPort, win32.EV_ERR)
99
111
 
100
112
        # Setup the connection info.
101
113
        # Get state and modify it:
102
 
        comDCB = win32file.GetCommState(self.hComPort)
 
114
        comDCB = win32.DCB()
 
115
        win32.GetCommState(self.hComPort, ctypes.byref(comDCB))
103
116
        comDCB.BaudRate = self._baudrate
104
117
 
105
118
        if self._bytesize == FIVEBITS:
114
127
            raise ValueError("Unsupported number of data bits: %r" % self._bytesize)
115
128
 
116
129
        if self._parity == PARITY_NONE:
117
 
            comDCB.Parity       = win32file.NOPARITY
118
 
            comDCB.fParity      = 0 # Dis/Enable Parity Check
 
130
            comDCB.Parity       = win32.NOPARITY
 
131
            comDCB.fParity      = 0 # Disable Parity Check
119
132
        elif self._parity == PARITY_EVEN:
120
 
            comDCB.Parity       = win32file.EVENPARITY
121
 
            comDCB.fParity      = 1 # Dis/Enable Parity Check
 
133
            comDCB.Parity       = win32.EVENPARITY
 
134
            comDCB.fParity      = 1 # Enable Parity Check
122
135
        elif self._parity == PARITY_ODD:
123
 
            comDCB.Parity       = win32file.ODDPARITY
124
 
            comDCB.fParity      = 1 # Dis/Enable Parity Check
 
136
            comDCB.Parity       = win32.ODDPARITY
 
137
            comDCB.fParity      = 1 # Enable Parity Check
 
138
        elif self._parity == PARITY_MARK:
 
139
            comDCB.Parity       = win32.MARKPARITY
 
140
            comDCB.fParity      = 1 # Enable Parity Check
 
141
        elif self._parity == PARITY_SPACE:
 
142
            comDCB.Parity       = win32.SPACEPARITY
 
143
            comDCB.fParity      = 1 # Enable Parity Check
125
144
        else:
126
145
            raise ValueError("Unsupported parity mode: %r" % self._parity)
127
146
 
128
147
        if self._stopbits == STOPBITS_ONE:
129
 
            comDCB.StopBits     = win32file.ONESTOPBIT
 
148
            comDCB.StopBits     = win32.ONESTOPBIT
 
149
        elif self._stopbits == STOPBITS_ONE_POINT_FIVE:
 
150
            comDCB.StopBits     = win32.ONE5STOPBITS
130
151
        elif self._stopbits == STOPBITS_TWO:
131
 
            comDCB.StopBits     = win32file.TWOSTOPBITS
 
152
            comDCB.StopBits     = win32.TWOSTOPBITS
132
153
        else:
133
154
            raise ValueError("Unsupported number of stop bits: %r" % self._stopbits)
134
 
            
 
155
 
135
156
        comDCB.fBinary          = 1 # Enable Binary Transmission
136
157
        # Char. w/ Parity-Err are replaced with 0xff (if fErrorChar is set to TRUE)
137
158
        if self._rtscts:
138
 
            comDCB.fRtsControl  = win32file.RTS_CONTROL_HANDSHAKE
 
159
            comDCB.fRtsControl  = win32.RTS_CONTROL_HANDSHAKE
139
160
        else:
140
161
            comDCB.fRtsControl  = self._rtsState
141
162
        if self._dsrdtr:
142
 
            comDCB.fDtrControl  = win32file.DTR_CONTROL_HANDSHAKE
 
163
            comDCB.fDtrControl  = win32.DTR_CONTROL_HANDSHAKE
143
164
        else:
144
165
            comDCB.fDtrControl  = self._dtrState
145
166
        comDCB.fOutxCtsFlow     = self._rtscts
152
173
        comDCB.XonChar          = XON
153
174
        comDCB.XoffChar         = XOFF
154
175
 
155
 
        try:
156
 
            win32file.SetCommState(self.hComPort, comDCB)
157
 
        except win32file.error, e:
158
 
            raise ValueError("Cannot configure port, some setting was wrong. Original message: %s" % e)
 
176
        if not win32.SetCommState(self.hComPort, ctypes.byref(comDCB)):
 
177
            raise ValueError("Cannot configure port, some setting was wrong. Original message: %s" % ctypes.WinError())
159
178
 
160
179
    #~ def __del__(self):
161
180
        #~ self.close()
164
183
        """Close port"""
165
184
        if self._isOpen:
166
185
            if self.hComPort:
167
 
                try:
168
 
                    # Restore original timeout values:
169
 
                    win32file.SetCommTimeouts(self.hComPort, self._orgTimeouts)
170
 
                except win32file.error:
171
 
                    # ignore errors. can happen for unplugged USB serial devices
172
 
                    pass
 
186
                # Restore original timeout values:
 
187
                win32.SetCommTimeouts(self.hComPort, self._orgTimeouts)
173
188
                # Close COM-Port:
174
 
                win32file.CloseHandle(self.hComPort)
175
 
                win32file.CloseHandle(self._overlappedRead.hEvent)
176
 
                win32file.CloseHandle(self._overlappedWrite.hEvent)
 
189
                win32.CloseHandle(self.hComPort)
 
190
                win32.CloseHandle(self._overlappedRead.hEvent)
 
191
                win32.CloseHandle(self._overlappedWrite.hEvent)
177
192
                self.hComPort = None
178
193
            self._isOpen = False
179
194
 
180
195
    def makeDeviceName(self, port):
181
 
        # the "\\.\COMx" format is required for devices other than COM1-COM8
182
 
        # not all versions of windows seem to support this properly
183
 
        # so that the first few ports are used with the DOS device name
184
 
        if port.upper().startswith('COM') and int(port[3:]) <= 8:
185
 
            return port
186
 
        return '\\\\.\\' + port
 
196
        return device(port)
187
197
 
188
198
    #  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
189
 
    
 
199
 
190
200
    def inWaiting(self):
191
201
        """Return the number of characters currently in the input buffer."""
192
 
        flags, comstat = win32file.ClearCommError(self.hComPort)
 
202
        flags = win32.DWORD()
 
203
        comstat = win32.COMSTAT()
 
204
        if not win32.ClearCommError(self.hComPort, ctypes.byref(flags), ctypes.byref(comstat)):
 
205
            raise SerialException('call to ClearCommError failed')
193
206
        return comstat.cbInQue
194
207
 
195
208
    def read(self, size=1):
198
211
           until the requested number of bytes is read."""
199
212
        if not self.hComPort: raise portNotOpenError
200
213
        if size > 0:
201
 
            win32event.ResetEvent(self._overlappedRead.hEvent)
202
 
            flags, comstat = win32file.ClearCommError(self.hComPort)
 
214
            win32.ResetEvent(self._overlappedRead.hEvent)
 
215
            flags = win32.DWORD()
 
216
            comstat = win32.COMSTAT()
 
217
            if not win32.ClearCommError(self.hComPort, ctypes.byref(flags), ctypes.byref(comstat)):
 
218
                raise SerialException('call to ClearCommError failed')
203
219
            if self.timeout == 0:
204
220
                n = min(comstat.cbInQue, size)
205
221
                if n > 0:
206
 
                    rc, buf = win32file.ReadFile(self.hComPort, win32file.AllocateReadBuffer(n), self._overlappedRead)
207
 
                    win32event.WaitForSingleObject(self._overlappedRead.hEvent, win32event.INFINITE)
208
 
                    read = str(buf)
 
222
                    buf = ctypes.create_string_buffer(n)
 
223
                    rc = win32.DWORD()
 
224
                    err = win32.ReadFile(self.hComPort, buf, n, ctypes.byref(rc), ctypes.byref(self._overlappedRead))
 
225
                    if not err and win32.GetLastError() != win32.ERROR_IO_PENDING:
 
226
                        raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
 
227
                    err = win32.WaitForSingleObject(self._overlappedRead.hEvent, win32.INFINITE)
 
228
                    read = buf.raw[:rc.value]
209
229
                else:
210
 
                    read = ''
 
230
                    read = bytes()
211
231
            else:
212
 
                rc, buf = win32file.ReadFile(self.hComPort, win32file.AllocateReadBuffer(size), self._overlappedRead)
213
 
                n = win32file.GetOverlappedResult(self.hComPort, self._overlappedRead, 1)
214
 
                read = str(buf[:n])
 
232
                buf = ctypes.create_string_buffer(size)
 
233
                rc = win32.DWORD()
 
234
                err = win32.ReadFile(self.hComPort, buf, size, ctypes.byref(rc), ctypes.byref(self._overlappedRead))
 
235
                if not err and win32.GetLastError() != win32.ERROR_IO_PENDING:
 
236
                    raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
 
237
                err = win32.GetOverlappedResult(self.hComPort, ctypes.byref(self._overlappedRead), ctypes.byref(rc), True)
 
238
                read = buf.raw[:rc.value]
215
239
        else:
216
 
            read = ''
217
 
        return read
 
240
            read = bytes()
 
241
        return bytes(read)
218
242
 
219
243
    def write(self, data):
220
244
        """Output the given string over the serial port."""
221
245
        if not self.hComPort: raise portNotOpenError
222
 
        if not isinstance(data, str):
223
 
            raise TypeError('expected str, got %s' % type(data))
224
 
        #print repr(s),
 
246
        #~ if not isinstance(data, (bytes, bytearray)):
 
247
            #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
 
248
        # convert data (needed in case of memoryview instance: Py 3.1 io lib), ctypes doesn't like memoryview
 
249
        data = bytes(data)
225
250
        if data:
226
251
            #~ win32event.ResetEvent(self._overlappedWrite.hEvent)
227
 
            err, n = win32file.WriteFile(self.hComPort, data, self._overlappedWrite)
228
 
            if err: #will be ERROR_IO_PENDING:
229
 
                # Wait for the write to complete.
230
 
                #~ win32event.WaitForSingleObject(self._overlappedWrite.hEvent, win32event.INFINITE)
231
 
                n = win32file.GetOverlappedResult(self.hComPort, self._overlappedWrite, 1)
232
 
                if n != len(data):
233
 
                    raise writeTimeoutError
234
 
                
 
252
            n = win32.DWORD()
 
253
            err = win32.WriteFile(self.hComPort, data, len(data), ctypes.byref(n), self._overlappedWrite)
 
254
            if not err and win32.GetLastError() != win32.ERROR_IO_PENDING:
 
255
                raise SerialException("WriteFile failed (%s)" % ctypes.WinError())
 
256
            # Wait for the write to complete.
 
257
            #~ win32.WaitForSingleObject(self._overlappedWrite.hEvent, win32.INFINITE)
 
258
            err = win32.GetOverlappedResult(self.hComPort, self._overlappedWrite, ctypes.byref(n), True)
 
259
            if n.value != len(data):
 
260
                raise writeTimeoutError
 
261
            return n.value
 
262
        else:
 
263
            return 0
 
264
 
235
265
 
236
266
    def flushInput(self):
237
267
        """Clear input buffer, discarding all that is in the buffer."""
238
268
        if not self.hComPort: raise portNotOpenError
239
 
        win32file.PurgeComm(self.hComPort, win32file.PURGE_RXCLEAR | win32file.PURGE_RXABORT)
 
269
        win32.PurgeComm(self.hComPort, win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)
240
270
 
241
271
    def flushOutput(self):
242
272
        """Clear output buffer, aborting the current output and
243
273
        discarding all that is in the buffer."""
244
274
        if not self.hComPort: raise portNotOpenError
245
 
        win32file.PurgeComm(self.hComPort, win32file.PURGE_TXCLEAR | win32file.PURGE_TXABORT)
 
275
        win32.PurgeComm(self.hComPort, win32.PURGE_TXCLEAR | win32.PURGE_TXABORT)
246
276
 
247
277
    def sendBreak(self, duration=0.25):
248
 
        """Send break condition."""
 
278
        """Send break condition. Timed, returns to idle state after given duration."""
249
279
        if not self.hComPort: raise portNotOpenError
250
280
        import time
251
 
        win32file.SetCommBreak(self.hComPort)
 
281
        win32.SetCommBreak(self.hComPort)
252
282
        time.sleep(duration)
253
 
        win32file.ClearCommBreak(self.hComPort)
 
283
        win32.ClearCommBreak(self.hComPort)
 
284
 
 
285
    def setBreak(self, level=1):
 
286
        """Set break: Controls TXD. When active, to transmitting is possible."""
 
287
        if not self.hComPort: raise portNotOpenError
 
288
        if level:
 
289
            win32.SetCommBreak(self.hComPort)
 
290
        else:
 
291
            win32.ClearCommBreak(self.hComPort)
254
292
 
255
293
    def setRTS(self, level=1):
256
294
        """Set terminal status line: Request To Send"""
257
295
        if not self.hComPort: raise portNotOpenError
258
296
        if level:
259
 
            self._rtsState = win32file.RTS_CONTROL_ENABLE
260
 
            win32file.EscapeCommFunction(self.hComPort, win32file.SETRTS)
 
297
            self._rtsState = win32.RTS_CONTROL_ENABLE
 
298
            win32.EscapeCommFunction(self.hComPort, win32.SETRTS)
261
299
        else:
262
 
            self._rtsState = win32file.RTS_CONTROL_DISABLE
263
 
            win32file.EscapeCommFunction(self.hComPort, win32file.CLRRTS)
 
300
            self._rtsState = win32.RTS_CONTROL_DISABLE
 
301
            win32.EscapeCommFunction(self.hComPort, win32.CLRRTS)
264
302
 
265
303
    def setDTR(self, level=1):
266
304
        """Set terminal status line: Data Terminal Ready"""
267
305
        if not self.hComPort: raise portNotOpenError
268
306
        if level:
269
 
            self._dtrState = win32file.DTR_CONTROL_ENABLE
270
 
            win32file.EscapeCommFunction(self.hComPort, win32file.SETDTR)
 
307
            self._dtrState = win32.DTR_CONTROL_ENABLE
 
308
            win32.EscapeCommFunction(self.hComPort, win32.SETDTR)
271
309
        else:
272
 
            self._dtrState = win32file.DTR_CONTROL_DISABLE
273
 
            win32file.EscapeCommFunction(self.hComPort, win32file.CLRDTR)
 
310
            self._dtrState = win32.DTR_CONTROL_DISABLE
 
311
            win32.EscapeCommFunction(self.hComPort, win32.CLRDTR)
 
312
 
 
313
    def _GetCommModemStatus(self):
 
314
        stat = win32.DWORD()
 
315
        win32.GetCommModemStatus(self.hComPort, ctypes.byref(stat))
 
316
        return stat.value
274
317
 
275
318
    def getCTS(self):
276
319
        """Read terminal status line: Clear To Send"""
277
320
        if not self.hComPort: raise portNotOpenError
278
 
        return MS_CTS_ON & win32file.GetCommModemStatus(self.hComPort) != 0
 
321
        return win32.MS_CTS_ON & self._GetCommModemStatus() != 0
279
322
 
280
323
    def getDSR(self):
281
324
        """Read terminal status line: Data Set Ready"""
282
325
        if not self.hComPort: raise portNotOpenError
283
 
        return MS_DSR_ON & win32file.GetCommModemStatus(self.hComPort) != 0
 
326
        return win32.MS_DSR_ON & self._GetCommModemStatus() != 0
284
327
 
285
328
    def getRI(self):
286
329
        """Read terminal status line: Ring Indicator"""
287
330
        if not self.hComPort: raise portNotOpenError
288
 
        return MS_RING_ON & win32file.GetCommModemStatus(self.hComPort) != 0
 
331
        return win32.MS_RING_ON & self._GetCommModemStatus() != 0
289
332
 
290
333
    def getCD(self):
291
334
        """Read terminal status line: Carrier Detect"""
292
335
        if not self.hComPort: raise portNotOpenError
293
 
        return MS_RLSD_ON & win32file.GetCommModemStatus(self.hComPort) != 0
 
336
        return win32.MS_RLSD_ON & self._GetCommModemStatus() != 0
294
337
 
295
338
    # - - platform specific - - - -
296
339
 
298
341
        """Platform specific - set flow state."""
299
342
        if not self.hComPort: raise portNotOpenError
300
343
        if level:
301
 
            win32file.EscapeCommFunction(self.hComPort, win32file.SETXON)
 
344
            win32.EscapeCommFunction(self.hComPort, win32.SETXON)
302
345
        else:
303
 
            win32file.EscapeCommFunction(self.hComPort, win32file.SETXOFF)
304
 
 
305
 
#Nur Testfunktion!!
 
346
            win32.EscapeCommFunction(self.hComPort, win32.SETXOFF)
 
347
 
 
348
    def outWaiting(self):
 
349
        """return how many characters the in the outgoing buffer"""
 
350
        flags = win32.DWORD()
 
351
        comstat = win32.COMSTAT()
 
352
        if not win32.ClearCommError(self.hComPort, ctypes.byref(flags), ctypes.byref(comstat)):
 
353
            raise SerialException('call to ClearCommError failed')
 
354
        return comstat.cbOutQue
 
355
 
 
356
 
 
357
# assemble Serial class with the platform specific implementation and the base
 
358
# for file-like behavior. for Python 2.6 and newer, that provide the new I/O
 
359
# library, derive from io.RawIOBase
 
360
try:
 
361
    import io
 
362
except ImportError:
 
363
    # classic version with our own file-like emulation
 
364
    class Serial(Win32Serial, FileLike):
 
365
        pass
 
366
else:
 
367
    # io library present
 
368
    class Serial(Win32Serial, io.RawIOBase):
 
369
        pass
 
370
 
 
371
 
 
372
# Nur Testfunktion!!
306
373
if __name__ == '__main__':
307
374
    s = Serial(0)
308
 
    print s
309
 
    
 
375
    sys.stdout.write("%s\n" % s)
 
376
 
310
377
    s = Serial()
311
 
    print s
312
 
    
313
 
    
 
378
    sys.stdout.write("%s\n" % s)
 
379
 
314
380
    s.baudrate = 19200
315
381
    s.databits = 7
316
382
    s.close()
317
383
    s.port = 0
318
384
    s.open()
319
 
    print s
 
385
    sys.stdout.write("%s\n" % s)
320
386