summaryrefslogtreecommitdiff
path: root/pyserial/serial/serialwin32.py
blob: c7800692b46944e49c67e2b9f67d844fbe698052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#! python
#serial driver for win32
#see serial.py
#
#(C) 2001 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt

import win32file  # The base COM port and file IO functions.
import win32event # We use events and the WaitFor[Single|Multiple]Objects functions.
import win32con   # constants.
import sys, string

VERSION = string.split("$Revision: 1.1.1.1 $")[1]     #extract CVS version

PARITY_NONE, PARITY_EVEN, PARITY_ODD = range(3)
STOPBITS_ONE, STOPBITS_TWO = (1, 2)
FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5,6,7,8)

portNotOpenError = ValueError('port not open')

class Serial:
    def __init__(self,
                 port,                  #number of device, numbering starts at
                                        #zero. if everything fails, the user
                                        #can specify a device string, note
                                        #that this isn't portable anymore
                 baudrate=9600,         #baudrate
                 bytesize=EIGHTBITS,    #number of databits
                 parity=PARITY_NONE,    #enable parity checking
                 stopbits=STOPBITS_ONE, #number of stopbits
                 timeout=None,          #set a timeout value, None for waiting forever
                 xonxoff=0,             #enable software flow control
                 rtscts=0,              #enable RTS/CTS flow control
                 ):
        """initialize comm port"""

        self.timeout = timeout

        if type(port) == type(''):       #strings are taken directly
            self.portstr = port
        else:
            self.portstr = 'COM%d' % (port+1) #numbers are transformed to a string
            #self.portstr = '\\\\.\\COM%d' % (port+1) #WIN NT format??

        self.hComPort = win32file.CreateFile(self.portstr,
               win32con.GENERIC_READ | win32con.GENERIC_WRITE,
               0, # exclusive access
               None, # no security
               win32con.OPEN_EXISTING,
               win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_OVERLAPPED,
               None)
        # Setup a 4k buffer
        win32file.SetupComm(self.hComPort, 4096, 4096)

        #Save original timeout values:
        self.orgTimeouts = win32file.GetCommTimeouts(self.hComPort)

        #Set Windows timeout values
        #timeouts is a tuple with the following items:
        #(ReadIntervalTimeout,ReadTotalTimeoutMultiplier,
        # ReadTotalTimeoutConstant,WriteTotalTimeoutMultiplier,
        # WriteTotalTimeoutConstant)
        if timeout:
            timeouts = (timeout*1000, 0, timeout*1000, 0, 0)
        else:
            #timeouts = (win32con.MAXDWORD, 1, 0, 1, 0)
            timeouts = (win32con.MAXDWORD, 0, 0, 0, 1000)
        win32file.SetCommTimeouts(self.hComPort, timeouts)

        #win32file.SetCommMask(self.hComPort, win32file.EV_RXCHAR | win32file.EV_TXEMPTY |
        #    win32file.EV_RXFLAG | win32file.EV_ERR)
        win32file.SetCommMask(self.hComPort,
                win32file.EV_RXCHAR | win32file.EV_RXFLAG | win32file.EV_ERR)
        #win32file.SetCommMask(self.hComPort, win32file.EV_ERR)

        # Setup the connection info.
        # Get state and modify it:
        comDCB = win32file.GetCommState(self.hComPort)
        comDCB.BaudRate = baudrate

        if bytesize == FIVEBITS:
            comDCB.ByteSize     = 5
        elif bytesize == SIXBITS:
            comDCB.ByteSize     = 6
        elif bytesize == SEVENBITS:
            comDCB.ByteSize     = 7
        elif bytesize == EIGHTBITS:
            comDCB.ByteSize     = 8

        if parity == PARITY_NONE:
            comDCB.Parity       = win32file.NOPARITY
            comDCB.fParity      = 0 # Dis/Enable Parity Check
        elif parity == PARITY_EVEN:
            comDCB.Parity       = win32file.EVENPARITY
            comDCB.fParity      = 1 # Dis/Enable Parity Check
        elif parity == PARITY_ODD:
            comDCB.Parity       = win32file.ODDPARITY
            comDCB.fParity      = 1 # Dis/Enable Parity Check

        if stopbits == STOPBITS_ONE:
            comDCB.StopBits     = win32file.ONESTOPBIT
        elif stopbits == STOPBITS_TWO:
            comDCB.StopBits     = win32file.TWOSTOPBITS
        comDCB.fBinary          = 1 # Enable Binary Transmission
        # Char. w/ Parity-Err are replaced with 0xff (if fErrorChar is set to TRUE)
        if rtscts:
            comDCB.fRtsControl  = win32file.RTS_CONTROL_HANDSHAKE
            comDCB.fDtrControl  = win32file.DTR_CONTROL_HANDSHAKE
        else:
            comDCB.fRtsControl  = win32file.RTS_CONTROL_ENABLE
            comDCB.fDtrControl  = win32file.DTR_CONTROL_ENABLE
        comDCB.fOutxCtsFlow     = rtscts
        comDCB.fOutxDsrFlow     = rtscts
        comDCB.fOutX            = xonxoff
        comDCB.fInX             = xonxoff
        comDCB.fNull            = 0
        comDCB.fErrorChar       = 0
        comDCB.fAbortOnError    = 0

        win32file.SetCommState(self.hComPort, comDCB)

        # Clear buffers:
        # Remove anything that was there
        win32file.PurgeComm(self.hComPort,
                            win32file.PURGE_TXCLEAR | win32file.PURGE_TXABORT |
                            win32file.PURGE_RXCLEAR | win32file.PURGE_RXABORT)

        #print win32file.ClearCommError(self.hComPort) #flags, comState =

        self.overlapped = win32file.OVERLAPPED()
        self.overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)

    def __del__(self):
        self.close()

    def close(self):
        """close port"""
        if self.hComPort:
            #Wait until data is transmitted, but not too long... (Timeout-Time)
            #while 1:
            #    flags, comState = win32file.ClearCommError(hComPort)
            #    if comState.cbOutQue <= 0 or calcTimeout(startTime) > timeout:
            #        break

            self.setRTS(0)
            self.setDTR(0)
            #Clear buffers:
            win32file.PurgeComm(self.hComPort,
                                win32file.PURGE_TXCLEAR | win32file.PURGE_TXABORT |
                                win32file.PURGE_RXCLEAR | win32file.PURGE_RXABORT)
            #Restore original timeout values:
            win32file.SetCommTimeouts(self.hComPort, self.orgTimeouts)
            #Close COM-Port:
            win32file.CloseHandle(self.hComPort)
            self.hComPort = None

    def inWaiting(self):
        """returns the number of bytes waiting to be read"""
        flags, comstat = win32file.ClearCommError(self.hComPort)
        return comstat.cbInQue

    def _read(self, size=1):
        flags, comstat = win32file.ClearCommError( self.hComPort )
        #print "1:",comstat.cbInQue,
        if comstat.cbInQue < size:
            rc, mask = win32file.WaitCommEvent(self.hComPort, self.overlapped)
            if rc == 0: # Character already ready!
                win32event.SetEvent(self.overlapped.hEvent)

            rc = win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)
            flags, comstat = win32file.ClearCommError( self.hComPort )
            #print "2:",comstat.cbInQue,

        rc, data = win32file.ReadFile(self.hComPort, size, self.overlapped)
        win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)
        print "read %r" % str(data)
        return str(data)

    def work_read(self,num=1):
        "read num bytes from serial port"
        if not self.hComPort: raise portNotOpenError
        flags, comstat = win32file.ClearCommError( self.hComPort )
        #print "1:",comstat.cbInQue,
        if comstat.cbInQue < num:
            rc, mask = win32file.WaitCommEvent(self.hComPort, self.overlapped)
            if rc == 0: # Character already ready!
                win32event.SetEvent(self.overlapped.hEvent)

            if self.timeout:
                rc = win32event.WaitForSingleObject(self.overlapped.hEvent, self.timeout*1000)
            else:
                rc = win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)
            flags, comstat = win32file.ClearCommError( self.hComPort )
            #print "2:",comstat.cbInQue,

        #rc, data = win32file.ReadFile(self.hComPort, comstat.cbInQue, self.overlapped)
        rc, data = win32file.ReadFile(self.hComPort, num, self.overlapped)
        if self.timeout:
            win32event.WaitForSingleObject(self.overlapped.hEvent, self.timeout*1000)
        else:
            win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)

        #old: hr, data = win32file.ReadFile(self.hComPort, num)
        #print '.%s.' % str(data)
        #print "read() soll %d ist %d" % (num, len(str(data))), repr(data)
        return str(data)

    def read(self, size=1):
        "read num bytes from serial port"
        if not self.hComPort: raise portNotOpenError
        #print "read %d" %size           ####debug
        read = ''
        if size > 0:
            while len(read) < size:
                flags, comstat = win32file.ClearCommError( self.hComPort )
                #print "1:",comstat.cbInQue,
                #self.overlapped = win32file.OVERLAPPED()
                #self.overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)
                #win32event.ResetEvent(self.overlapped.hEvent)
##                if comstat.cbInQue < size:
##                    #print "read <size"            ####debug
##                    rc, mask = win32file.WaitCommEvent(self.hComPort, self.overlapped)
##                    if rc == 0: # Character already ready!
##                        win32event.SetEvent(self.overlapped.hEvent)
##
##                    if self.timeout:
##                        rc = win32event.WaitForSingleObject(self.overlapped.hEvent, self.timeout*1000)
##                        if rc == win32event.WAIT_TIMEOUT: break
##                    else:
##                        rc = win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)
##                    flags, comstat = win32file.ClearCommError( self.hComPort )
##                    #print "2:",comstat.cbInQue,

                rc, buf = win32file.ReadFile(self.hComPort, size-len(read), self.overlapped)
                if self.timeout:
                    rc = win32event.WaitForSingleObject(self.overlapped.hEvent, self.timeout*1000)
                    if rc == win32event.WAIT_TIMEOUT: break
                    #TODO: if reading more than 1 byte data can be lost when a timeout occours!!!!
                else:
                    win32event.WaitForSingleObject(self.overlapped.hEvent, win32event.INFINITE)
                read = read + str(buf)
                #print "read() soll %d ist %d" % (num, len(str(data))), repr(data)
        #print "read %r" % str(read)
        return str(read)

    def write(self, s):
        "write string to serial port"
        if not self.hComPort: raise portNotOpenError
        #print repr(s),
        overlapped = win32file.OVERLAPPED()
        overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None)
        win32file.WriteFile(self.hComPort, s, overlapped)
        # Wait for the write to complete.
        win32event.WaitForSingleObject(overlapped.hEvent, win32event.INFINITE)
        #old: win32file.WriteFile(self.hComPort, s) #, 1,  NULL)
        #print "ok"

    def flushInput(self):
        if not self.hComPort: raise portNotOpenError
        win32file.PurgeComm(self.hComPort, win32file.PURGE_RXCLEAR | win32file.PURGE_RXABORT)

    def flushOutput(self):
        if not self.hComPort: raise portNotOpenError
        win32file.PurgeComm(self.hComPort, win32file.PURGE_TXCLEAR | win32file.PURGE_TXABORT)

    def sendBreak(self):
        if not self.hComPort: raise portNotOpenError
        raise "not implemented"

    def setRTS(self,level=1):
        if not self.hComPort: raise portNotOpenError
        comDCB = win32file.GetCommState(self.hComPort)
        if level:
            comDCB.fRtsControl = win32file.RTS_CONTROL_ENABLE;
        else:
            comDCB.fRtsControl = win32file.RTS_CONTROL_DISABLE;
        win32file.SetCommState(self.hComPort, comDCB)

    def setDTR(self,level=1):
        if not self.hComPort: raise portNotOpenError
        comDCB = win32file.GetCommState(self.hComPort)
        if level:
            comDCB.fDtrControl = win32file.DTR_CONTROL_ENABLE;
        else:
            comDCB.fDtrControl = win32file.DTR_CONTROL_DISABLE;
        win32file.SetCommState(self.hComPort, comDCB)

    def getCTS(self):
        if not self.hComPort: raise portNotOpenError
        comDCB = win32file.GetCommState(self.hComPort)
        return comDCB.fOutxCtsFlow

    def getDSR(self):
        if not self.hComPort: raise portNotOpenError
        comDCB = win32file.GetCommState(self.hComPort)
        return comDCB.fOutxDsrFlow



#Nur Testfunktion!!
if __name__ == '__main__':
    print __name__
    s = Serial(0)