blob: 43caae1fffb651e9b39c1d558dbbeb1466bc6519 [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
Chris Liechti6ba7d6f2016-01-28 21:02:49 +010012from ctypes import c_ulong, c_void_p, c_int64, c_char, \
13 WinDLL, sizeof, Structure, Union, POINTER
cliechti183d4ae2009-07-23 22:03:51 +000014from ctypes.wintypes import HANDLE
15from ctypes.wintypes import BOOL
16from ctypes.wintypes import LPCWSTR
cliechti183d4ae2009-07-23 22:03:51 +000017from ctypes.wintypes import DWORD
18from ctypes.wintypes import WORD
19from ctypes.wintypes import BYTE
Chris Liechti033f17c2015-08-30 21:28:04 +020020_stdcall_libraries = {}
21_stdcall_libraries['kernel32'] = WinDLL('kernel32')
cliechti183d4ae2009-07-23 22:03:51 +000022
cliechtie37b6a82009-07-24 12:19:50 +000023INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000024
Chris Liechti033f17c2015-08-30 21:28:04 +020025
cliechti251e5ce2011-08-25 01:24:49 +000026# some details of the windows API differ between 32 and 64 bit systems..
27def is_64bit():
28 """Returns true when running on a 64 bit system"""
29 return sizeof(c_ulong) != sizeof(c_void_p)
30
cliechtide432902011-08-18 22:51:41 +000031# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
32# is either 32 or 64 bits, depending on the type of windows...
33# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000034if is_64bit():
cliechtide432902011-08-18 22:51:41 +000035 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000036else:
cliechti251e5ce2011-08-25 01:24:49 +000037 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000038
39
cliechti183d4ae2009-07-23 22:03:51 +000040class _SECURITY_ATTRIBUTES(Structure):
41 pass
42LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
43
cliechtie37b6a82009-07-24 12:19:50 +000044
cliechti8a345132011-08-05 02:33:14 +000045try:
46 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
47except AttributeError:
48 # Fallback to non wide char version for old OS...
49 from ctypes.wintypes import LPCSTR
50 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
51 CreateEventA.restype = HANDLE
52 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020053 CreateEvent = CreateEventA
cliechti8a345132011-08-05 02:33:14 +000054
55 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
56 CreateFileA.restype = HANDLE
57 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
58 CreateFile = CreateFileA
59else:
60 CreateEventW.restype = HANDLE
61 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020062 CreateEvent = CreateEventW # alias
cliechti8a345132011-08-05 02:33:14 +000063
64 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
65 CreateFileW.restype = HANDLE
66 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
Chris Liechti033f17c2015-08-30 21:28:04 +020067 CreateFile = CreateFileW # alias
68
cliechti183d4ae2009-07-23 22:03:51 +000069
70class _OVERLAPPED(Structure):
71 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020072
cliechti183d4ae2009-07-23 22:03:51 +000073OVERLAPPED = _OVERLAPPED
74
Chris Liechti033f17c2015-08-30 21:28:04 +020075
cliechti183d4ae2009-07-23 22:03:51 +000076class _COMSTAT(Structure):
77 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020078
cliechti183d4ae2009-07-23 22:03:51 +000079COMSTAT = _COMSTAT
80
Chris Liechti033f17c2015-08-30 21:28:04 +020081
cliechti183d4ae2009-07-23 22:03:51 +000082class _DCB(Structure):
83 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020084
cliechti183d4ae2009-07-23 22:03:51 +000085DCB = _DCB
86
Chris Liechti033f17c2015-08-30 21:28:04 +020087
cliechti183d4ae2009-07-23 22:03:51 +000088class _COMMTIMEOUTS(Structure):
89 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020090
cliechti183d4ae2009-07-23 22:03:51 +000091COMMTIMEOUTS = _COMMTIMEOUTS
92
93GetLastError = _stdcall_libraries['kernel32'].GetLastError
94GetLastError.restype = DWORD
95GetLastError.argtypes = []
96
97LPOVERLAPPED = POINTER(_OVERLAPPED)
98LPDWORD = POINTER(DWORD)
99
100GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
101GetOverlappedResult.restype = BOOL
102GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
103
104ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
105ResetEvent.restype = BOOL
106ResetEvent.argtypes = [HANDLE]
107
108LPCVOID = c_void_p
109
110WriteFile = _stdcall_libraries['kernel32'].WriteFile
111WriteFile.restype = BOOL
112WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
113
114LPVOID = c_void_p
115
116ReadFile = _stdcall_libraries['kernel32'].ReadFile
117ReadFile.restype = BOOL
118ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
119
120CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
121CloseHandle.restype = BOOL
122CloseHandle.argtypes = [HANDLE]
123
124ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
125ClearCommBreak.restype = BOOL
126ClearCommBreak.argtypes = [HANDLE]
127
128LPCOMSTAT = POINTER(_COMSTAT)
129
130ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
131ClearCommError.restype = BOOL
132ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
133
134SetupComm = _stdcall_libraries['kernel32'].SetupComm
135SetupComm.restype = BOOL
136SetupComm.argtypes = [HANDLE, DWORD, DWORD]
137
138EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
139EscapeCommFunction.restype = BOOL
140EscapeCommFunction.argtypes = [HANDLE, DWORD]
141
142GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
143GetCommModemStatus.restype = BOOL
144GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
145
146LPDCB = POINTER(_DCB)
147
148GetCommState = _stdcall_libraries['kernel32'].GetCommState
149GetCommState.restype = BOOL
150GetCommState.argtypes = [HANDLE, LPDCB]
151
152LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
153
154GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
155GetCommTimeouts.restype = BOOL
156GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
157
158PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
159PurgeComm.restype = BOOL
160PurgeComm.argtypes = [HANDLE, DWORD]
161
162SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
163SetCommBreak.restype = BOOL
164SetCommBreak.argtypes = [HANDLE]
165
166SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
167SetCommMask.restype = BOOL
168SetCommMask.argtypes = [HANDLE, DWORD]
169
170SetCommState = _stdcall_libraries['kernel32'].SetCommState
171SetCommState.restype = BOOL
172SetCommState.argtypes = [HANDLE, LPDCB]
173
174SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
175SetCommTimeouts.restype = BOOL
176SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
177
cliechtie37b6a82009-07-24 12:19:50 +0000178WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
179WaitForSingleObject.restype = DWORD
180WaitForSingleObject.argtypes = [HANDLE, DWORD]
181
Chris Liechti033f17c2015-08-30 21:28:04 +0200182ONESTOPBIT = 0 # Variable c_int
183TWOSTOPBITS = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000184ONE5STOPBITS = 1
185
Chris Liechti033f17c2015-08-30 21:28:04 +0200186NOPARITY = 0 # Variable c_int
187ODDPARITY = 1 # Variable c_int
188EVENPARITY = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000189MARKPARITY = 3
190SPACEPARITY = 4
191
Chris Liechti033f17c2015-08-30 21:28:04 +0200192RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
193RTS_CONTROL_DISABLE = 0 # Variable c_int
194RTS_CONTROL_ENABLE = 1 # Variable c_int
195RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000196SETRTS = 3
197CLRRTS = 4
198
Chris Liechti033f17c2015-08-30 21:28:04 +0200199DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
200DTR_CONTROL_DISABLE = 0 # Variable c_int
201DTR_CONTROL_ENABLE = 1 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000202SETDTR = 5
203CLRDTR = 6
204
Chris Liechti033f17c2015-08-30 21:28:04 +0200205MS_DSR_ON = 32 # Variable c_ulong
206EV_RING = 256 # Variable c_int
207EV_PERR = 512 # Variable c_int
208EV_ERR = 128 # Variable c_int
209SETXOFF = 1 # Variable c_int
210EV_RXCHAR = 1 # Variable c_int
211GENERIC_WRITE = 1073741824 # Variable c_long
212PURGE_TXCLEAR = 4 # Variable c_int
213FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
214EV_DSR = 16 # Variable c_int
215MAXDWORD = 4294967295 # Variable c_uint
216EV_RLSD = 32 # Variable c_int
Chris Liechti7731e982015-11-02 23:46:07 +0100217ERROR_SUCCESS = 0
Chris Liechti033f17c2015-08-30 21:28:04 +0200218ERROR_IO_PENDING = 997 # Variable c_long
219MS_CTS_ON = 16 # Variable c_ulong
220EV_EVENT1 = 2048 # Variable c_int
221EV_RX80FULL = 1024 # Variable c_int
222PURGE_RXABORT = 2 # Variable c_int
223FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
224PURGE_TXABORT = 1 # Variable c_int
225SETXON = 2 # Variable c_int
226OPEN_EXISTING = 3 # Variable c_int
227MS_RING_ON = 64 # Variable c_ulong
228EV_TXEMPTY = 4 # Variable c_int
229EV_RXFLAG = 2 # Variable c_int
230MS_RLSD_ON = 128 # Variable c_ulong
231GENERIC_READ = 2147483648 # Variable c_ulong
232EV_EVENT2 = 4096 # Variable c_int
233EV_CTS = 8 # Variable c_int
234EV_BREAK = 64 # Variable c_int
235PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200236INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000237
cliechtide432902011-08-18 22:51:41 +0000238
cliechti183d4ae2009-07-23 22:03:51 +0000239class N11_OVERLAPPED4DOLLAR_48E(Union):
240 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200241
242
cliechti183d4ae2009-07-23 22:03:51 +0000243class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
244 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200245
246
cliechti183d4ae2009-07-23 22:03:51 +0000247N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
248 ('Offset', DWORD),
249 ('OffsetHigh', DWORD),
250]
251
252PVOID = c_void_p
253
254N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
255N11_OVERLAPPED4DOLLAR_48E._fields_ = [
256 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
257 ('Pointer', PVOID),
258]
259_OVERLAPPED._anonymous_ = ['_0']
260_OVERLAPPED._fields_ = [
261 ('Internal', ULONG_PTR),
262 ('InternalHigh', ULONG_PTR),
263 ('_0', N11_OVERLAPPED4DOLLAR_48E),
264 ('hEvent', HANDLE),
265]
266_SECURITY_ATTRIBUTES._fields_ = [
267 ('nLength', DWORD),
268 ('lpSecurityDescriptor', LPVOID),
269 ('bInheritHandle', BOOL),
270]
271_COMSTAT._fields_ = [
272 ('fCtsHold', DWORD, 1),
273 ('fDsrHold', DWORD, 1),
274 ('fRlsdHold', DWORD, 1),
275 ('fXoffHold', DWORD, 1),
276 ('fXoffSent', DWORD, 1),
277 ('fEof', DWORD, 1),
278 ('fTxim', DWORD, 1),
279 ('fReserved', DWORD, 25),
280 ('cbInQue', DWORD),
281 ('cbOutQue', DWORD),
282]
283_DCB._fields_ = [
284 ('DCBlength', DWORD),
285 ('BaudRate', DWORD),
286 ('fBinary', DWORD, 1),
287 ('fParity', DWORD, 1),
288 ('fOutxCtsFlow', DWORD, 1),
289 ('fOutxDsrFlow', DWORD, 1),
290 ('fDtrControl', DWORD, 2),
291 ('fDsrSensitivity', DWORD, 1),
292 ('fTXContinueOnXoff', DWORD, 1),
293 ('fOutX', DWORD, 1),
294 ('fInX', DWORD, 1),
295 ('fErrorChar', DWORD, 1),
296 ('fNull', DWORD, 1),
297 ('fRtsControl', DWORD, 2),
298 ('fAbortOnError', DWORD, 1),
299 ('fDummy2', DWORD, 17),
300 ('wReserved', WORD),
301 ('XonLim', WORD),
302 ('XoffLim', WORD),
303 ('ByteSize', BYTE),
304 ('Parity', BYTE),
305 ('StopBits', BYTE),
306 ('XonChar', c_char),
307 ('XoffChar', c_char),
308 ('ErrorChar', c_char),
309 ('EofChar', c_char),
310 ('EvtChar', c_char),
311 ('wReserved1', WORD),
312]
313_COMMTIMEOUTS._fields_ = [
314 ('ReadIntervalTimeout', DWORD),
315 ('ReadTotalTimeoutMultiplier', DWORD),
316 ('ReadTotalTimeoutConstant', DWORD),
317 ('WriteTotalTimeoutMultiplier', DWORD),
318 ('WriteTotalTimeoutConstant', DWORD),
319]
320__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
321 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
322 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
323 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
324 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
325 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
326 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
327 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
328 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
329 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
330 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
331 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
332 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
333 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
334 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
335 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
336 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
337 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
338 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
339 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
340 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
341 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
342 'MS_DSR_ON', 'MS_RING_ON',
343 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
344 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']