blob: 08b6e6773f3ead551e3172310be61603885827a8 [file] [log] [blame]
Chris Liechtifbdd8a02015-08-09 02:37:45 +02001#! python
2#
Chris Liechti3e02f702015-12-16 23:06:04 +01003# Constants and types for use with Windows API, used by serialwin32.py
4#
5# This file is part of pySerial. https://github.com/pyserial/pyserial
Chris Liechtifbdd8a02015-08-09 02:37:45 +02006# (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
7#
8# SPDX-License-Identifier: BSD-3-Clause
9
Chris Liechti9eaa40c2016-02-12 23:32:59 +010010# pylint: disable=invalid-name,too-few-public-methods,protected-access,too-many-instance-attributes
11
Kurt McKee057387c2018-02-07 22:10:38 -060012from __future__ import absolute_import
13
Chris Liechti6ba7d6f2016-01-28 21:02:49 +010014from ctypes import c_ulong, c_void_p, c_int64, c_char, \
15 WinDLL, sizeof, Structure, Union, POINTER
cliechti183d4ae2009-07-23 22:03:51 +000016from ctypes.wintypes import HANDLE
17from ctypes.wintypes import BOOL
18from ctypes.wintypes import LPCWSTR
cliechti183d4ae2009-07-23 22:03:51 +000019from ctypes.wintypes import DWORD
20from ctypes.wintypes import WORD
21from ctypes.wintypes import BYTE
Chris Liechti033f17c2015-08-30 21:28:04 +020022_stdcall_libraries = {}
23_stdcall_libraries['kernel32'] = WinDLL('kernel32')
cliechti183d4ae2009-07-23 22:03:51 +000024
cliechtie37b6a82009-07-24 12:19:50 +000025INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000026
Chris Liechti033f17c2015-08-30 21:28:04 +020027
cliechti251e5ce2011-08-25 01:24:49 +000028# some details of the windows API differ between 32 and 64 bit systems..
29def is_64bit():
30 """Returns true when running on a 64 bit system"""
31 return sizeof(c_ulong) != sizeof(c_void_p)
32
cliechtide432902011-08-18 22:51:41 +000033# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
34# is either 32 or 64 bits, depending on the type of windows...
35# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000036if is_64bit():
cliechtide432902011-08-18 22:51:41 +000037 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000038else:
cliechti251e5ce2011-08-25 01:24:49 +000039 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000040
41
cliechti183d4ae2009-07-23 22:03:51 +000042class _SECURITY_ATTRIBUTES(Structure):
43 pass
44LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
45
cliechtie37b6a82009-07-24 12:19:50 +000046
cliechti8a345132011-08-05 02:33:14 +000047try:
48 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
49except AttributeError:
50 # Fallback to non wide char version for old OS...
51 from ctypes.wintypes import LPCSTR
52 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
53 CreateEventA.restype = HANDLE
54 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020055 CreateEvent = CreateEventA
cliechti8a345132011-08-05 02:33:14 +000056
57 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
58 CreateFileA.restype = HANDLE
59 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
60 CreateFile = CreateFileA
61else:
62 CreateEventW.restype = HANDLE
63 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020064 CreateEvent = CreateEventW # alias
cliechti8a345132011-08-05 02:33:14 +000065
66 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
67 CreateFileW.restype = HANDLE
68 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
Chris Liechti033f17c2015-08-30 21:28:04 +020069 CreateFile = CreateFileW # alias
70
cliechti183d4ae2009-07-23 22:03:51 +000071
72class _OVERLAPPED(Structure):
73 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020074
cliechti183d4ae2009-07-23 22:03:51 +000075OVERLAPPED = _OVERLAPPED
76
Chris Liechti033f17c2015-08-30 21:28:04 +020077
cliechti183d4ae2009-07-23 22:03:51 +000078class _COMSTAT(Structure):
79 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020080
cliechti183d4ae2009-07-23 22:03:51 +000081COMSTAT = _COMSTAT
82
Chris Liechti033f17c2015-08-30 21:28:04 +020083
cliechti183d4ae2009-07-23 22:03:51 +000084class _DCB(Structure):
85 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020086
cliechti183d4ae2009-07-23 22:03:51 +000087DCB = _DCB
88
Chris Liechti033f17c2015-08-30 21:28:04 +020089
cliechti183d4ae2009-07-23 22:03:51 +000090class _COMMTIMEOUTS(Structure):
91 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020092
cliechti183d4ae2009-07-23 22:03:51 +000093COMMTIMEOUTS = _COMMTIMEOUTS
94
95GetLastError = _stdcall_libraries['kernel32'].GetLastError
96GetLastError.restype = DWORD
97GetLastError.argtypes = []
98
99LPOVERLAPPED = POINTER(_OVERLAPPED)
100LPDWORD = POINTER(DWORD)
101
102GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
103GetOverlappedResult.restype = BOOL
104GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
105
106ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
107ResetEvent.restype = BOOL
108ResetEvent.argtypes = [HANDLE]
109
110LPCVOID = c_void_p
111
112WriteFile = _stdcall_libraries['kernel32'].WriteFile
113WriteFile.restype = BOOL
114WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
115
116LPVOID = c_void_p
117
118ReadFile = _stdcall_libraries['kernel32'].ReadFile
119ReadFile.restype = BOOL
120ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
121
122CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
123CloseHandle.restype = BOOL
124CloseHandle.argtypes = [HANDLE]
125
126ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
127ClearCommBreak.restype = BOOL
128ClearCommBreak.argtypes = [HANDLE]
129
130LPCOMSTAT = POINTER(_COMSTAT)
131
132ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
133ClearCommError.restype = BOOL
134ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
135
136SetupComm = _stdcall_libraries['kernel32'].SetupComm
137SetupComm.restype = BOOL
138SetupComm.argtypes = [HANDLE, DWORD, DWORD]
139
140EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
141EscapeCommFunction.restype = BOOL
142EscapeCommFunction.argtypes = [HANDLE, DWORD]
143
144GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
145GetCommModemStatus.restype = BOOL
146GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
147
148LPDCB = POINTER(_DCB)
149
150GetCommState = _stdcall_libraries['kernel32'].GetCommState
151GetCommState.restype = BOOL
152GetCommState.argtypes = [HANDLE, LPDCB]
153
154LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
155
156GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
157GetCommTimeouts.restype = BOOL
158GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
159
160PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
161PurgeComm.restype = BOOL
162PurgeComm.argtypes = [HANDLE, DWORD]
163
164SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
165SetCommBreak.restype = BOOL
166SetCommBreak.argtypes = [HANDLE]
167
168SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
169SetCommMask.restype = BOOL
170SetCommMask.argtypes = [HANDLE, DWORD]
171
172SetCommState = _stdcall_libraries['kernel32'].SetCommState
173SetCommState.restype = BOOL
174SetCommState.argtypes = [HANDLE, LPDCB]
175
176SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
177SetCommTimeouts.restype = BOOL
178SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
179
cliechtie37b6a82009-07-24 12:19:50 +0000180WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
181WaitForSingleObject.restype = DWORD
182WaitForSingleObject.argtypes = [HANDLE, DWORD]
183
Chris Liechti5a39b882016-05-03 22:10:39 +0200184CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
185CancelIoEx.restype = BOOL
186CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]
187
Chris Liechti033f17c2015-08-30 21:28:04 +0200188ONESTOPBIT = 0 # Variable c_int
189TWOSTOPBITS = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000190ONE5STOPBITS = 1
191
Chris Liechti033f17c2015-08-30 21:28:04 +0200192NOPARITY = 0 # Variable c_int
193ODDPARITY = 1 # Variable c_int
194EVENPARITY = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000195MARKPARITY = 3
196SPACEPARITY = 4
197
Chris Liechti033f17c2015-08-30 21:28:04 +0200198RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
199RTS_CONTROL_DISABLE = 0 # Variable c_int
200RTS_CONTROL_ENABLE = 1 # Variable c_int
201RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000202SETRTS = 3
203CLRRTS = 4
204
Chris Liechti033f17c2015-08-30 21:28:04 +0200205DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
206DTR_CONTROL_DISABLE = 0 # Variable c_int
207DTR_CONTROL_ENABLE = 1 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000208SETDTR = 5
209CLRDTR = 6
210
Chris Liechti033f17c2015-08-30 21:28:04 +0200211MS_DSR_ON = 32 # Variable c_ulong
212EV_RING = 256 # Variable c_int
213EV_PERR = 512 # Variable c_int
214EV_ERR = 128 # Variable c_int
215SETXOFF = 1 # Variable c_int
216EV_RXCHAR = 1 # Variable c_int
217GENERIC_WRITE = 1073741824 # Variable c_long
218PURGE_TXCLEAR = 4 # Variable c_int
219FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
220EV_DSR = 16 # Variable c_int
221MAXDWORD = 4294967295 # Variable c_uint
222EV_RLSD = 32 # Variable c_int
Chris Liechti2a7ed532016-09-17 16:42:27 +0200223
Chris Liechti7731e982015-11-02 23:46:07 +0100224ERROR_SUCCESS = 0
Chris Liechti2a7ed532016-09-17 16:42:27 +0200225ERROR_NOT_ENOUGH_MEMORY = 8
Chris Liechti34018402016-05-25 01:51:42 +0200226ERROR_OPERATION_ABORTED = 995
Chris Liechtic0d6a0f2016-05-12 23:48:01 +0200227ERROR_IO_INCOMPLETE = 996
Chris Liechti033f17c2015-08-30 21:28:04 +0200228ERROR_IO_PENDING = 997 # Variable c_long
Chris Liechti2a7ed532016-09-17 16:42:27 +0200229ERROR_INVALID_USER_BUFFER = 1784
230
Chris Liechti033f17c2015-08-30 21:28:04 +0200231MS_CTS_ON = 16 # Variable c_ulong
232EV_EVENT1 = 2048 # Variable c_int
233EV_RX80FULL = 1024 # Variable c_int
234PURGE_RXABORT = 2 # Variable c_int
235FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
236PURGE_TXABORT = 1 # Variable c_int
237SETXON = 2 # Variable c_int
238OPEN_EXISTING = 3 # Variable c_int
239MS_RING_ON = 64 # Variable c_ulong
240EV_TXEMPTY = 4 # Variable c_int
241EV_RXFLAG = 2 # Variable c_int
242MS_RLSD_ON = 128 # Variable c_ulong
243GENERIC_READ = 2147483648 # Variable c_ulong
244EV_EVENT2 = 4096 # Variable c_int
245EV_CTS = 8 # Variable c_int
246EV_BREAK = 64 # Variable c_int
247PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200248INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000249
cliechtide432902011-08-18 22:51:41 +0000250
cliechti183d4ae2009-07-23 22:03:51 +0000251class N11_OVERLAPPED4DOLLAR_48E(Union):
252 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200253
254
cliechti183d4ae2009-07-23 22:03:51 +0000255class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
256 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200257
258
cliechti183d4ae2009-07-23 22:03:51 +0000259N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
260 ('Offset', DWORD),
261 ('OffsetHigh', DWORD),
262]
263
264PVOID = c_void_p
265
266N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
267N11_OVERLAPPED4DOLLAR_48E._fields_ = [
268 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
269 ('Pointer', PVOID),
270]
271_OVERLAPPED._anonymous_ = ['_0']
272_OVERLAPPED._fields_ = [
273 ('Internal', ULONG_PTR),
274 ('InternalHigh', ULONG_PTR),
275 ('_0', N11_OVERLAPPED4DOLLAR_48E),
276 ('hEvent', HANDLE),
277]
278_SECURITY_ATTRIBUTES._fields_ = [
279 ('nLength', DWORD),
280 ('lpSecurityDescriptor', LPVOID),
281 ('bInheritHandle', BOOL),
282]
283_COMSTAT._fields_ = [
284 ('fCtsHold', DWORD, 1),
285 ('fDsrHold', DWORD, 1),
286 ('fRlsdHold', DWORD, 1),
287 ('fXoffHold', DWORD, 1),
288 ('fXoffSent', DWORD, 1),
289 ('fEof', DWORD, 1),
290 ('fTxim', DWORD, 1),
291 ('fReserved', DWORD, 25),
292 ('cbInQue', DWORD),
293 ('cbOutQue', DWORD),
294]
295_DCB._fields_ = [
296 ('DCBlength', DWORD),
297 ('BaudRate', DWORD),
298 ('fBinary', DWORD, 1),
299 ('fParity', DWORD, 1),
300 ('fOutxCtsFlow', DWORD, 1),
301 ('fOutxDsrFlow', DWORD, 1),
302 ('fDtrControl', DWORD, 2),
303 ('fDsrSensitivity', DWORD, 1),
304 ('fTXContinueOnXoff', DWORD, 1),
305 ('fOutX', DWORD, 1),
306 ('fInX', DWORD, 1),
307 ('fErrorChar', DWORD, 1),
308 ('fNull', DWORD, 1),
309 ('fRtsControl', DWORD, 2),
310 ('fAbortOnError', DWORD, 1),
311 ('fDummy2', DWORD, 17),
312 ('wReserved', WORD),
313 ('XonLim', WORD),
314 ('XoffLim', WORD),
315 ('ByteSize', BYTE),
316 ('Parity', BYTE),
317 ('StopBits', BYTE),
318 ('XonChar', c_char),
319 ('XoffChar', c_char),
320 ('ErrorChar', c_char),
321 ('EofChar', c_char),
322 ('EvtChar', c_char),
323 ('wReserved1', WORD),
324]
325_COMMTIMEOUTS._fields_ = [
326 ('ReadIntervalTimeout', DWORD),
327 ('ReadTotalTimeoutMultiplier', DWORD),
328 ('ReadTotalTimeoutConstant', DWORD),
329 ('WriteTotalTimeoutMultiplier', DWORD),
330 ('WriteTotalTimeoutConstant', DWORD),
331]
332__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
333 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
334 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
335 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
336 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
337 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
338 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
339 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
340 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
341 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
342 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
343 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
344 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
345 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
346 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
347 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
348 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
349 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
350 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
351 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
352 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
353 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
354 'MS_DSR_ON', 'MS_RING_ON',
355 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
356 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']