blob: 15e6550563cc08fd4ab631ec271f6f06ed811358 [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 Liechti6ba7d6f2016-01-28 21:02:49 +010010from ctypes import c_ulong, c_void_p, c_int64, c_char, \
11 WinDLL, sizeof, Structure, Union, POINTER
cliechti183d4ae2009-07-23 22:03:51 +000012from ctypes.wintypes import HANDLE
13from ctypes.wintypes import BOOL
14from ctypes.wintypes import LPCWSTR
cliechti183d4ae2009-07-23 22:03:51 +000015from ctypes.wintypes import DWORD
16from ctypes.wintypes import WORD
17from ctypes.wintypes import BYTE
Chris Liechti033f17c2015-08-30 21:28:04 +020018_stdcall_libraries = {}
19_stdcall_libraries['kernel32'] = WinDLL('kernel32')
cliechti183d4ae2009-07-23 22:03:51 +000020
cliechtie37b6a82009-07-24 12:19:50 +000021INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000022
Chris Liechti033f17c2015-08-30 21:28:04 +020023
cliechti251e5ce2011-08-25 01:24:49 +000024# some details of the windows API differ between 32 and 64 bit systems..
25def is_64bit():
26 """Returns true when running on a 64 bit system"""
27 return sizeof(c_ulong) != sizeof(c_void_p)
28
cliechtide432902011-08-18 22:51:41 +000029# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
30# is either 32 or 64 bits, depending on the type of windows...
31# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000032if is_64bit():
cliechtide432902011-08-18 22:51:41 +000033 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000034else:
cliechti251e5ce2011-08-25 01:24:49 +000035 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000036
37
cliechti183d4ae2009-07-23 22:03:51 +000038class _SECURITY_ATTRIBUTES(Structure):
39 pass
40LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
41
cliechtie37b6a82009-07-24 12:19:50 +000042
cliechti8a345132011-08-05 02:33:14 +000043try:
44 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
45except AttributeError:
46 # Fallback to non wide char version for old OS...
47 from ctypes.wintypes import LPCSTR
48 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
49 CreateEventA.restype = HANDLE
50 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020051 CreateEvent = CreateEventA
cliechti8a345132011-08-05 02:33:14 +000052
53 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
54 CreateFileA.restype = HANDLE
55 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
56 CreateFile = CreateFileA
57else:
58 CreateEventW.restype = HANDLE
59 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020060 CreateEvent = CreateEventW # alias
cliechti8a345132011-08-05 02:33:14 +000061
62 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
63 CreateFileW.restype = HANDLE
64 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
Chris Liechti033f17c2015-08-30 21:28:04 +020065 CreateFile = CreateFileW # alias
66
cliechti183d4ae2009-07-23 22:03:51 +000067
68class _OVERLAPPED(Structure):
69 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020070
cliechti183d4ae2009-07-23 22:03:51 +000071OVERLAPPED = _OVERLAPPED
72
Chris Liechti033f17c2015-08-30 21:28:04 +020073
cliechti183d4ae2009-07-23 22:03:51 +000074class _COMSTAT(Structure):
75 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020076
cliechti183d4ae2009-07-23 22:03:51 +000077COMSTAT = _COMSTAT
78
Chris Liechti033f17c2015-08-30 21:28:04 +020079
cliechti183d4ae2009-07-23 22:03:51 +000080class _DCB(Structure):
81 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020082
cliechti183d4ae2009-07-23 22:03:51 +000083DCB = _DCB
84
Chris Liechti033f17c2015-08-30 21:28:04 +020085
cliechti183d4ae2009-07-23 22:03:51 +000086class _COMMTIMEOUTS(Structure):
87 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020088
cliechti183d4ae2009-07-23 22:03:51 +000089COMMTIMEOUTS = _COMMTIMEOUTS
90
91GetLastError = _stdcall_libraries['kernel32'].GetLastError
92GetLastError.restype = DWORD
93GetLastError.argtypes = []
94
95LPOVERLAPPED = POINTER(_OVERLAPPED)
96LPDWORD = POINTER(DWORD)
97
98GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
99GetOverlappedResult.restype = BOOL
100GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
101
102ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
103ResetEvent.restype = BOOL
104ResetEvent.argtypes = [HANDLE]
105
106LPCVOID = c_void_p
107
108WriteFile = _stdcall_libraries['kernel32'].WriteFile
109WriteFile.restype = BOOL
110WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
111
112LPVOID = c_void_p
113
114ReadFile = _stdcall_libraries['kernel32'].ReadFile
115ReadFile.restype = BOOL
116ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
117
118CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
119CloseHandle.restype = BOOL
120CloseHandle.argtypes = [HANDLE]
121
122ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
123ClearCommBreak.restype = BOOL
124ClearCommBreak.argtypes = [HANDLE]
125
126LPCOMSTAT = POINTER(_COMSTAT)
127
128ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
129ClearCommError.restype = BOOL
130ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
131
132SetupComm = _stdcall_libraries['kernel32'].SetupComm
133SetupComm.restype = BOOL
134SetupComm.argtypes = [HANDLE, DWORD, DWORD]
135
136EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
137EscapeCommFunction.restype = BOOL
138EscapeCommFunction.argtypes = [HANDLE, DWORD]
139
140GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
141GetCommModemStatus.restype = BOOL
142GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
143
144LPDCB = POINTER(_DCB)
145
146GetCommState = _stdcall_libraries['kernel32'].GetCommState
147GetCommState.restype = BOOL
148GetCommState.argtypes = [HANDLE, LPDCB]
149
150LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
151
152GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
153GetCommTimeouts.restype = BOOL
154GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
155
156PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
157PurgeComm.restype = BOOL
158PurgeComm.argtypes = [HANDLE, DWORD]
159
160SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
161SetCommBreak.restype = BOOL
162SetCommBreak.argtypes = [HANDLE]
163
164SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
165SetCommMask.restype = BOOL
166SetCommMask.argtypes = [HANDLE, DWORD]
167
168SetCommState = _stdcall_libraries['kernel32'].SetCommState
169SetCommState.restype = BOOL
170SetCommState.argtypes = [HANDLE, LPDCB]
171
172SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
173SetCommTimeouts.restype = BOOL
174SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
175
cliechtie37b6a82009-07-24 12:19:50 +0000176WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
177WaitForSingleObject.restype = DWORD
178WaitForSingleObject.argtypes = [HANDLE, DWORD]
179
Chris Liechti033f17c2015-08-30 21:28:04 +0200180ONESTOPBIT = 0 # Variable c_int
181TWOSTOPBITS = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000182ONE5STOPBITS = 1
183
Chris Liechti033f17c2015-08-30 21:28:04 +0200184NOPARITY = 0 # Variable c_int
185ODDPARITY = 1 # Variable c_int
186EVENPARITY = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000187MARKPARITY = 3
188SPACEPARITY = 4
189
Chris Liechti033f17c2015-08-30 21:28:04 +0200190RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
191RTS_CONTROL_DISABLE = 0 # Variable c_int
192RTS_CONTROL_ENABLE = 1 # Variable c_int
193RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000194SETRTS = 3
195CLRRTS = 4
196
Chris Liechti033f17c2015-08-30 21:28:04 +0200197DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
198DTR_CONTROL_DISABLE = 0 # Variable c_int
199DTR_CONTROL_ENABLE = 1 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000200SETDTR = 5
201CLRDTR = 6
202
Chris Liechti033f17c2015-08-30 21:28:04 +0200203MS_DSR_ON = 32 # Variable c_ulong
204EV_RING = 256 # Variable c_int
205EV_PERR = 512 # Variable c_int
206EV_ERR = 128 # Variable c_int
207SETXOFF = 1 # Variable c_int
208EV_RXCHAR = 1 # Variable c_int
209GENERIC_WRITE = 1073741824 # Variable c_long
210PURGE_TXCLEAR = 4 # Variable c_int
211FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
212EV_DSR = 16 # Variable c_int
213MAXDWORD = 4294967295 # Variable c_uint
214EV_RLSD = 32 # Variable c_int
Chris Liechti7731e982015-11-02 23:46:07 +0100215ERROR_SUCCESS = 0
Chris Liechti033f17c2015-08-30 21:28:04 +0200216ERROR_IO_PENDING = 997 # Variable c_long
217MS_CTS_ON = 16 # Variable c_ulong
218EV_EVENT1 = 2048 # Variable c_int
219EV_RX80FULL = 1024 # Variable c_int
220PURGE_RXABORT = 2 # Variable c_int
221FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
222PURGE_TXABORT = 1 # Variable c_int
223SETXON = 2 # Variable c_int
224OPEN_EXISTING = 3 # Variable c_int
225MS_RING_ON = 64 # Variable c_ulong
226EV_TXEMPTY = 4 # Variable c_int
227EV_RXFLAG = 2 # Variable c_int
228MS_RLSD_ON = 128 # Variable c_ulong
229GENERIC_READ = 2147483648 # Variable c_ulong
230EV_EVENT2 = 4096 # Variable c_int
231EV_CTS = 8 # Variable c_int
232EV_BREAK = 64 # Variable c_int
233PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200234INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000235
cliechtide432902011-08-18 22:51:41 +0000236
cliechti183d4ae2009-07-23 22:03:51 +0000237class N11_OVERLAPPED4DOLLAR_48E(Union):
238 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200239
240
cliechti183d4ae2009-07-23 22:03:51 +0000241class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
242 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200243
244
cliechti183d4ae2009-07-23 22:03:51 +0000245N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
246 ('Offset', DWORD),
247 ('OffsetHigh', DWORD),
248]
249
250PVOID = c_void_p
251
252N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
253N11_OVERLAPPED4DOLLAR_48E._fields_ = [
254 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
255 ('Pointer', PVOID),
256]
257_OVERLAPPED._anonymous_ = ['_0']
258_OVERLAPPED._fields_ = [
259 ('Internal', ULONG_PTR),
260 ('InternalHigh', ULONG_PTR),
261 ('_0', N11_OVERLAPPED4DOLLAR_48E),
262 ('hEvent', HANDLE),
263]
264_SECURITY_ATTRIBUTES._fields_ = [
265 ('nLength', DWORD),
266 ('lpSecurityDescriptor', LPVOID),
267 ('bInheritHandle', BOOL),
268]
269_COMSTAT._fields_ = [
270 ('fCtsHold', DWORD, 1),
271 ('fDsrHold', DWORD, 1),
272 ('fRlsdHold', DWORD, 1),
273 ('fXoffHold', DWORD, 1),
274 ('fXoffSent', DWORD, 1),
275 ('fEof', DWORD, 1),
276 ('fTxim', DWORD, 1),
277 ('fReserved', DWORD, 25),
278 ('cbInQue', DWORD),
279 ('cbOutQue', DWORD),
280]
281_DCB._fields_ = [
282 ('DCBlength', DWORD),
283 ('BaudRate', DWORD),
284 ('fBinary', DWORD, 1),
285 ('fParity', DWORD, 1),
286 ('fOutxCtsFlow', DWORD, 1),
287 ('fOutxDsrFlow', DWORD, 1),
288 ('fDtrControl', DWORD, 2),
289 ('fDsrSensitivity', DWORD, 1),
290 ('fTXContinueOnXoff', DWORD, 1),
291 ('fOutX', DWORD, 1),
292 ('fInX', DWORD, 1),
293 ('fErrorChar', DWORD, 1),
294 ('fNull', DWORD, 1),
295 ('fRtsControl', DWORD, 2),
296 ('fAbortOnError', DWORD, 1),
297 ('fDummy2', DWORD, 17),
298 ('wReserved', WORD),
299 ('XonLim', WORD),
300 ('XoffLim', WORD),
301 ('ByteSize', BYTE),
302 ('Parity', BYTE),
303 ('StopBits', BYTE),
304 ('XonChar', c_char),
305 ('XoffChar', c_char),
306 ('ErrorChar', c_char),
307 ('EofChar', c_char),
308 ('EvtChar', c_char),
309 ('wReserved1', WORD),
310]
311_COMMTIMEOUTS._fields_ = [
312 ('ReadIntervalTimeout', DWORD),
313 ('ReadTotalTimeoutMultiplier', DWORD),
314 ('ReadTotalTimeoutConstant', DWORD),
315 ('WriteTotalTimeoutMultiplier', DWORD),
316 ('WriteTotalTimeoutConstant', DWORD),
317]
318__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
319 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
320 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
321 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
322 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
323 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
324 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
325 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
326 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
327 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
328 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
329 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
330 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
331 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
332 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
333 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
334 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
335 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
336 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
337 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
338 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
339 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
340 'MS_DSR_ON', 'MS_RING_ON',
341 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
342 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']