blob: cca6195c98f94550567803e18f3247624f8263d2 [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
cliechti183d4ae2009-07-23 22:03:51 +000010from ctypes import *
11from ctypes.wintypes import HANDLE
12from ctypes.wintypes import BOOL
13from ctypes.wintypes import LPCWSTR
cliechti183d4ae2009-07-23 22:03:51 +000014from ctypes.wintypes import DWORD
15from ctypes.wintypes import WORD
16from ctypes.wintypes import BYTE
Chris Liechti033f17c2015-08-30 21:28:04 +020017_stdcall_libraries = {}
18_stdcall_libraries['kernel32'] = WinDLL('kernel32')
cliechti183d4ae2009-07-23 22:03:51 +000019
cliechtie37b6a82009-07-24 12:19:50 +000020INVALID_HANDLE_VALUE = HANDLE(-1).value
cliechti183d4ae2009-07-23 22:03:51 +000021
Chris Liechti033f17c2015-08-30 21:28:04 +020022
cliechti251e5ce2011-08-25 01:24:49 +000023# some details of the windows API differ between 32 and 64 bit systems..
24def is_64bit():
25 """Returns true when running on a 64 bit system"""
26 return sizeof(c_ulong) != sizeof(c_void_p)
27
cliechtide432902011-08-18 22:51:41 +000028# ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
29# is either 32 or 64 bits, depending on the type of windows...
30# so test if this a 32 bit windows...
cliechti251e5ce2011-08-25 01:24:49 +000031if is_64bit():
cliechtide432902011-08-18 22:51:41 +000032 # assume 64 bits
33 ULONG_PTR = c_int64
cliechti251e5ce2011-08-25 01:24:49 +000034else:
35 # 32 bits
36 ULONG_PTR = c_ulong
cliechtide432902011-08-18 22:51:41 +000037
38
cliechti183d4ae2009-07-23 22:03:51 +000039class _SECURITY_ATTRIBUTES(Structure):
40 pass
41LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
42
cliechtie37b6a82009-07-24 12:19:50 +000043
cliechti8a345132011-08-05 02:33:14 +000044try:
45 CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
46except AttributeError:
47 # Fallback to non wide char version for old OS...
48 from ctypes.wintypes import LPCSTR
49 CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
50 CreateEventA.restype = HANDLE
51 CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020052 CreateEvent = CreateEventA
cliechti8a345132011-08-05 02:33:14 +000053
54 CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
55 CreateFileA.restype = HANDLE
56 CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
57 CreateFile = CreateFileA
58else:
59 CreateEventW.restype = HANDLE
60 CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
Chris Liechti033f17c2015-08-30 21:28:04 +020061 CreateEvent = CreateEventW # alias
cliechti8a345132011-08-05 02:33:14 +000062
63 CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
64 CreateFileW.restype = HANDLE
65 CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
Chris Liechti033f17c2015-08-30 21:28:04 +020066 CreateFile = CreateFileW # alias
67
cliechti183d4ae2009-07-23 22:03:51 +000068
69class _OVERLAPPED(Structure):
70 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020071
cliechti183d4ae2009-07-23 22:03:51 +000072OVERLAPPED = _OVERLAPPED
73
Chris Liechti033f17c2015-08-30 21:28:04 +020074
cliechti183d4ae2009-07-23 22:03:51 +000075class _COMSTAT(Structure):
76 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020077
cliechti183d4ae2009-07-23 22:03:51 +000078COMSTAT = _COMSTAT
79
Chris Liechti033f17c2015-08-30 21:28:04 +020080
cliechti183d4ae2009-07-23 22:03:51 +000081class _DCB(Structure):
82 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020083
cliechti183d4ae2009-07-23 22:03:51 +000084DCB = _DCB
85
Chris Liechti033f17c2015-08-30 21:28:04 +020086
cliechti183d4ae2009-07-23 22:03:51 +000087class _COMMTIMEOUTS(Structure):
88 pass
Chris Liechti033f17c2015-08-30 21:28:04 +020089
cliechti183d4ae2009-07-23 22:03:51 +000090COMMTIMEOUTS = _COMMTIMEOUTS
91
92GetLastError = _stdcall_libraries['kernel32'].GetLastError
93GetLastError.restype = DWORD
94GetLastError.argtypes = []
95
96LPOVERLAPPED = POINTER(_OVERLAPPED)
97LPDWORD = POINTER(DWORD)
98
99GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
100GetOverlappedResult.restype = BOOL
101GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
102
103ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
104ResetEvent.restype = BOOL
105ResetEvent.argtypes = [HANDLE]
106
107LPCVOID = c_void_p
108
109WriteFile = _stdcall_libraries['kernel32'].WriteFile
110WriteFile.restype = BOOL
111WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
112
113LPVOID = c_void_p
114
115ReadFile = _stdcall_libraries['kernel32'].ReadFile
116ReadFile.restype = BOOL
117ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
118
119CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
120CloseHandle.restype = BOOL
121CloseHandle.argtypes = [HANDLE]
122
123ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
124ClearCommBreak.restype = BOOL
125ClearCommBreak.argtypes = [HANDLE]
126
127LPCOMSTAT = POINTER(_COMSTAT)
128
129ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
130ClearCommError.restype = BOOL
131ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
132
133SetupComm = _stdcall_libraries['kernel32'].SetupComm
134SetupComm.restype = BOOL
135SetupComm.argtypes = [HANDLE, DWORD, DWORD]
136
137EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
138EscapeCommFunction.restype = BOOL
139EscapeCommFunction.argtypes = [HANDLE, DWORD]
140
141GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
142GetCommModemStatus.restype = BOOL
143GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
144
145LPDCB = POINTER(_DCB)
146
147GetCommState = _stdcall_libraries['kernel32'].GetCommState
148GetCommState.restype = BOOL
149GetCommState.argtypes = [HANDLE, LPDCB]
150
151LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
152
153GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
154GetCommTimeouts.restype = BOOL
155GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
156
157PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
158PurgeComm.restype = BOOL
159PurgeComm.argtypes = [HANDLE, DWORD]
160
161SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
162SetCommBreak.restype = BOOL
163SetCommBreak.argtypes = [HANDLE]
164
165SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
166SetCommMask.restype = BOOL
167SetCommMask.argtypes = [HANDLE, DWORD]
168
169SetCommState = _stdcall_libraries['kernel32'].SetCommState
170SetCommState.restype = BOOL
171SetCommState.argtypes = [HANDLE, LPDCB]
172
173SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
174SetCommTimeouts.restype = BOOL
175SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
176
cliechtie37b6a82009-07-24 12:19:50 +0000177WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
178WaitForSingleObject.restype = DWORD
179WaitForSingleObject.argtypes = [HANDLE, DWORD]
180
Chris Liechti033f17c2015-08-30 21:28:04 +0200181ONESTOPBIT = 0 # Variable c_int
182TWOSTOPBITS = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000183ONE5STOPBITS = 1
184
Chris Liechti033f17c2015-08-30 21:28:04 +0200185NOPARITY = 0 # Variable c_int
186ODDPARITY = 1 # Variable c_int
187EVENPARITY = 2 # Variable c_int
cliechti183d4ae2009-07-23 22:03:51 +0000188MARKPARITY = 3
189SPACEPARITY = 4
190
Chris Liechti033f17c2015-08-30 21:28:04 +0200191RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
192RTS_CONTROL_DISABLE = 0 # Variable c_int
193RTS_CONTROL_ENABLE = 1 # Variable c_int
194RTS_CONTROL_TOGGLE = 3 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000195SETRTS = 3
196CLRRTS = 4
197
Chris Liechti033f17c2015-08-30 21:28:04 +0200198DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
199DTR_CONTROL_DISABLE = 0 # Variable c_int
200DTR_CONTROL_ENABLE = 1 # Variable c_int
cliechtie37b6a82009-07-24 12:19:50 +0000201SETDTR = 5
202CLRDTR = 6
203
Chris Liechti033f17c2015-08-30 21:28:04 +0200204MS_DSR_ON = 32 # Variable c_ulong
205EV_RING = 256 # Variable c_int
206EV_PERR = 512 # Variable c_int
207EV_ERR = 128 # Variable c_int
208SETXOFF = 1 # Variable c_int
209EV_RXCHAR = 1 # Variable c_int
210GENERIC_WRITE = 1073741824 # Variable c_long
211PURGE_TXCLEAR = 4 # Variable c_int
212FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
213EV_DSR = 16 # Variable c_int
214MAXDWORD = 4294967295 # Variable c_uint
215EV_RLSD = 32 # Variable c_int
Chris Liechti7731e982015-11-02 23:46:07 +0100216ERROR_SUCCESS = 0
Chris Liechti033f17c2015-08-30 21:28:04 +0200217ERROR_IO_PENDING = 997 # Variable c_long
218MS_CTS_ON = 16 # Variable c_ulong
219EV_EVENT1 = 2048 # Variable c_int
220EV_RX80FULL = 1024 # Variable c_int
221PURGE_RXABORT = 2 # Variable c_int
222FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
223PURGE_TXABORT = 1 # Variable c_int
224SETXON = 2 # Variable c_int
225OPEN_EXISTING = 3 # Variable c_int
226MS_RING_ON = 64 # Variable c_ulong
227EV_TXEMPTY = 4 # Variable c_int
228EV_RXFLAG = 2 # Variable c_int
229MS_RLSD_ON = 128 # Variable c_ulong
230GENERIC_READ = 2147483648 # Variable c_ulong
231EV_EVENT2 = 4096 # Variable c_int
232EV_CTS = 8 # Variable c_int
233EV_BREAK = 64 # Variable c_int
234PURGE_RXCLEAR = 8 # Variable c_int
Chris Liechti68340d72015-08-03 14:15:48 +0200235INFINITE = 0xFFFFFFFF
cliechti183d4ae2009-07-23 22:03:51 +0000236
cliechtide432902011-08-18 22:51:41 +0000237
cliechti183d4ae2009-07-23 22:03:51 +0000238class N11_OVERLAPPED4DOLLAR_48E(Union):
239 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200240
241
cliechti183d4ae2009-07-23 22:03:51 +0000242class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
243 pass
Chris Liechti033f17c2015-08-30 21:28:04 +0200244
245
cliechti183d4ae2009-07-23 22:03:51 +0000246N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
247 ('Offset', DWORD),
248 ('OffsetHigh', DWORD),
249]
250
251PVOID = c_void_p
252
253N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
254N11_OVERLAPPED4DOLLAR_48E._fields_ = [
255 ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
256 ('Pointer', PVOID),
257]
258_OVERLAPPED._anonymous_ = ['_0']
259_OVERLAPPED._fields_ = [
260 ('Internal', ULONG_PTR),
261 ('InternalHigh', ULONG_PTR),
262 ('_0', N11_OVERLAPPED4DOLLAR_48E),
263 ('hEvent', HANDLE),
264]
265_SECURITY_ATTRIBUTES._fields_ = [
266 ('nLength', DWORD),
267 ('lpSecurityDescriptor', LPVOID),
268 ('bInheritHandle', BOOL),
269]
270_COMSTAT._fields_ = [
271 ('fCtsHold', DWORD, 1),
272 ('fDsrHold', DWORD, 1),
273 ('fRlsdHold', DWORD, 1),
274 ('fXoffHold', DWORD, 1),
275 ('fXoffSent', DWORD, 1),
276 ('fEof', DWORD, 1),
277 ('fTxim', DWORD, 1),
278 ('fReserved', DWORD, 25),
279 ('cbInQue', DWORD),
280 ('cbOutQue', DWORD),
281]
282_DCB._fields_ = [
283 ('DCBlength', DWORD),
284 ('BaudRate', DWORD),
285 ('fBinary', DWORD, 1),
286 ('fParity', DWORD, 1),
287 ('fOutxCtsFlow', DWORD, 1),
288 ('fOutxDsrFlow', DWORD, 1),
289 ('fDtrControl', DWORD, 2),
290 ('fDsrSensitivity', DWORD, 1),
291 ('fTXContinueOnXoff', DWORD, 1),
292 ('fOutX', DWORD, 1),
293 ('fInX', DWORD, 1),
294 ('fErrorChar', DWORD, 1),
295 ('fNull', DWORD, 1),
296 ('fRtsControl', DWORD, 2),
297 ('fAbortOnError', DWORD, 1),
298 ('fDummy2', DWORD, 17),
299 ('wReserved', WORD),
300 ('XonLim', WORD),
301 ('XoffLim', WORD),
302 ('ByteSize', BYTE),
303 ('Parity', BYTE),
304 ('StopBits', BYTE),
305 ('XonChar', c_char),
306 ('XoffChar', c_char),
307 ('ErrorChar', c_char),
308 ('EofChar', c_char),
309 ('EvtChar', c_char),
310 ('wReserved1', WORD),
311]
312_COMMTIMEOUTS._fields_ = [
313 ('ReadIntervalTimeout', DWORD),
314 ('ReadTotalTimeoutMultiplier', DWORD),
315 ('ReadTotalTimeoutConstant', DWORD),
316 ('WriteTotalTimeoutMultiplier', DWORD),
317 ('WriteTotalTimeoutConstant', DWORD),
318]
319__all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
320 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
321 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
322 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
323 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
324 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
325 '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
326 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
327 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
328 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
329 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
330 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
331 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
332 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
333 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
334 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
335 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
336 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
337 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
338 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
339 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
340 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
341 'MS_DSR_ON', 'MS_RING_ON',
342 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
343 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']