win32.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #! python
  2. #
  3. # 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
  6. # (C) 2001-2015 Chris Liechti <cliechti@gmx.net>
  7. #
  8. # SPDX-License-Identifier: BSD-3-Clause
  9. # pylint: disable=invalid-name,too-few-public-methods,protected-access,too-many-instance-attributes
  10. from __future__ import absolute_import
  11. from ctypes import c_ulong, c_void_p, c_int64, c_char, \
  12. WinDLL, sizeof, Structure, Union, POINTER
  13. from ctypes.wintypes import HANDLE
  14. from ctypes.wintypes import BOOL
  15. from ctypes.wintypes import LPCWSTR
  16. from ctypes.wintypes import DWORD
  17. from ctypes.wintypes import WORD
  18. from ctypes.wintypes import BYTE
  19. _stdcall_libraries = {}
  20. _stdcall_libraries['kernel32'] = WinDLL('kernel32')
  21. INVALID_HANDLE_VALUE = HANDLE(-1).value
  22. # some details of the windows API differ between 32 and 64 bit systems..
  23. def is_64bit():
  24. """Returns true when running on a 64 bit system"""
  25. return sizeof(c_ulong) != sizeof(c_void_p)
  26. # ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
  27. # is either 32 or 64 bits, depending on the type of windows...
  28. # so test if this a 32 bit windows...
  29. if is_64bit():
  30. ULONG_PTR = c_int64
  31. else:
  32. ULONG_PTR = c_ulong
  33. class _SECURITY_ATTRIBUTES(Structure):
  34. pass
  35. LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
  36. try:
  37. CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
  38. except AttributeError:
  39. # Fallback to non wide char version for old OS...
  40. from ctypes.wintypes import LPCSTR
  41. CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
  42. CreateEventA.restype = HANDLE
  43. CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
  44. CreateEvent = CreateEventA
  45. CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
  46. CreateFileA.restype = HANDLE
  47. CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
  48. CreateFile = CreateFileA
  49. else:
  50. CreateEventW.restype = HANDLE
  51. CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
  52. CreateEvent = CreateEventW # alias
  53. CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
  54. CreateFileW.restype = HANDLE
  55. CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
  56. CreateFile = CreateFileW # alias
  57. class _OVERLAPPED(Structure):
  58. pass
  59. OVERLAPPED = _OVERLAPPED
  60. class _COMSTAT(Structure):
  61. pass
  62. COMSTAT = _COMSTAT
  63. class _DCB(Structure):
  64. pass
  65. DCB = _DCB
  66. class _COMMTIMEOUTS(Structure):
  67. pass
  68. COMMTIMEOUTS = _COMMTIMEOUTS
  69. GetLastError = _stdcall_libraries['kernel32'].GetLastError
  70. GetLastError.restype = DWORD
  71. GetLastError.argtypes = []
  72. LPOVERLAPPED = POINTER(_OVERLAPPED)
  73. LPDWORD = POINTER(DWORD)
  74. GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
  75. GetOverlappedResult.restype = BOOL
  76. GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
  77. ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
  78. ResetEvent.restype = BOOL
  79. ResetEvent.argtypes = [HANDLE]
  80. LPCVOID = c_void_p
  81. WriteFile = _stdcall_libraries['kernel32'].WriteFile
  82. WriteFile.restype = BOOL
  83. WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
  84. LPVOID = c_void_p
  85. ReadFile = _stdcall_libraries['kernel32'].ReadFile
  86. ReadFile.restype = BOOL
  87. ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
  88. CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
  89. CloseHandle.restype = BOOL
  90. CloseHandle.argtypes = [HANDLE]
  91. ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
  92. ClearCommBreak.restype = BOOL
  93. ClearCommBreak.argtypes = [HANDLE]
  94. LPCOMSTAT = POINTER(_COMSTAT)
  95. ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
  96. ClearCommError.restype = BOOL
  97. ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
  98. SetupComm = _stdcall_libraries['kernel32'].SetupComm
  99. SetupComm.restype = BOOL
  100. SetupComm.argtypes = [HANDLE, DWORD, DWORD]
  101. EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
  102. EscapeCommFunction.restype = BOOL
  103. EscapeCommFunction.argtypes = [HANDLE, DWORD]
  104. GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
  105. GetCommModemStatus.restype = BOOL
  106. GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
  107. LPDCB = POINTER(_DCB)
  108. GetCommState = _stdcall_libraries['kernel32'].GetCommState
  109. GetCommState.restype = BOOL
  110. GetCommState.argtypes = [HANDLE, LPDCB]
  111. LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
  112. GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
  113. GetCommTimeouts.restype = BOOL
  114. GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
  115. PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
  116. PurgeComm.restype = BOOL
  117. PurgeComm.argtypes = [HANDLE, DWORD]
  118. SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
  119. SetCommBreak.restype = BOOL
  120. SetCommBreak.argtypes = [HANDLE]
  121. SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
  122. SetCommMask.restype = BOOL
  123. SetCommMask.argtypes = [HANDLE, DWORD]
  124. SetCommState = _stdcall_libraries['kernel32'].SetCommState
  125. SetCommState.restype = BOOL
  126. SetCommState.argtypes = [HANDLE, LPDCB]
  127. SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
  128. SetCommTimeouts.restype = BOOL
  129. SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
  130. WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
  131. WaitForSingleObject.restype = DWORD
  132. WaitForSingleObject.argtypes = [HANDLE, DWORD]
  133. WaitCommEvent = _stdcall_libraries['kernel32'].WaitCommEvent
  134. WaitCommEvent.restype = BOOL
  135. WaitCommEvent.argtypes = [HANDLE, LPDWORD, LPOVERLAPPED]
  136. CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
  137. CancelIoEx.restype = BOOL
  138. CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]
  139. ONESTOPBIT = 0 # Variable c_int
  140. TWOSTOPBITS = 2 # Variable c_int
  141. ONE5STOPBITS = 1
  142. NOPARITY = 0 # Variable c_int
  143. ODDPARITY = 1 # Variable c_int
  144. EVENPARITY = 2 # Variable c_int
  145. MARKPARITY = 3
  146. SPACEPARITY = 4
  147. RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
  148. RTS_CONTROL_DISABLE = 0 # Variable c_int
  149. RTS_CONTROL_ENABLE = 1 # Variable c_int
  150. RTS_CONTROL_TOGGLE = 3 # Variable c_int
  151. SETRTS = 3
  152. CLRRTS = 4
  153. DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
  154. DTR_CONTROL_DISABLE = 0 # Variable c_int
  155. DTR_CONTROL_ENABLE = 1 # Variable c_int
  156. SETDTR = 5
  157. CLRDTR = 6
  158. MS_DSR_ON = 32 # Variable c_ulong
  159. EV_RING = 256 # Variable c_int
  160. EV_PERR = 512 # Variable c_int
  161. EV_ERR = 128 # Variable c_int
  162. SETXOFF = 1 # Variable c_int
  163. EV_RXCHAR = 1 # Variable c_int
  164. GENERIC_WRITE = 1073741824 # Variable c_long
  165. PURGE_TXCLEAR = 4 # Variable c_int
  166. FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
  167. EV_DSR = 16 # Variable c_int
  168. MAXDWORD = 4294967295 # Variable c_uint
  169. EV_RLSD = 32 # Variable c_int
  170. ERROR_SUCCESS = 0
  171. ERROR_NOT_ENOUGH_MEMORY = 8
  172. ERROR_OPERATION_ABORTED = 995
  173. ERROR_IO_INCOMPLETE = 996
  174. ERROR_IO_PENDING = 997 # Variable c_long
  175. ERROR_INVALID_USER_BUFFER = 1784
  176. MS_CTS_ON = 16 # Variable c_ulong
  177. EV_EVENT1 = 2048 # Variable c_int
  178. EV_RX80FULL = 1024 # Variable c_int
  179. PURGE_RXABORT = 2 # Variable c_int
  180. FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
  181. PURGE_TXABORT = 1 # Variable c_int
  182. SETXON = 2 # Variable c_int
  183. OPEN_EXISTING = 3 # Variable c_int
  184. MS_RING_ON = 64 # Variable c_ulong
  185. EV_TXEMPTY = 4 # Variable c_int
  186. EV_RXFLAG = 2 # Variable c_int
  187. MS_RLSD_ON = 128 # Variable c_ulong
  188. GENERIC_READ = 2147483648 # Variable c_ulong
  189. EV_EVENT2 = 4096 # Variable c_int
  190. EV_CTS = 8 # Variable c_int
  191. EV_BREAK = 64 # Variable c_int
  192. PURGE_RXCLEAR = 8 # Variable c_int
  193. INFINITE = 0xFFFFFFFF
  194. CE_RXOVER = 0x0001
  195. CE_OVERRUN = 0x0002
  196. CE_RXPARITY = 0x0004
  197. CE_FRAME = 0x0008
  198. CE_BREAK = 0x0010
  199. class N11_OVERLAPPED4DOLLAR_48E(Union):
  200. pass
  201. class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
  202. pass
  203. N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
  204. ('Offset', DWORD),
  205. ('OffsetHigh', DWORD),
  206. ]
  207. PVOID = c_void_p
  208. N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
  209. N11_OVERLAPPED4DOLLAR_48E._fields_ = [
  210. ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
  211. ('Pointer', PVOID),
  212. ]
  213. _OVERLAPPED._anonymous_ = ['_0']
  214. _OVERLAPPED._fields_ = [
  215. ('Internal', ULONG_PTR),
  216. ('InternalHigh', ULONG_PTR),
  217. ('_0', N11_OVERLAPPED4DOLLAR_48E),
  218. ('hEvent', HANDLE),
  219. ]
  220. _SECURITY_ATTRIBUTES._fields_ = [
  221. ('nLength', DWORD),
  222. ('lpSecurityDescriptor', LPVOID),
  223. ('bInheritHandle', BOOL),
  224. ]
  225. _COMSTAT._fields_ = [
  226. ('fCtsHold', DWORD, 1),
  227. ('fDsrHold', DWORD, 1),
  228. ('fRlsdHold', DWORD, 1),
  229. ('fXoffHold', DWORD, 1),
  230. ('fXoffSent', DWORD, 1),
  231. ('fEof', DWORD, 1),
  232. ('fTxim', DWORD, 1),
  233. ('fReserved', DWORD, 25),
  234. ('cbInQue', DWORD),
  235. ('cbOutQue', DWORD),
  236. ]
  237. _DCB._fields_ = [
  238. ('DCBlength', DWORD),
  239. ('BaudRate', DWORD),
  240. ('fBinary', DWORD, 1),
  241. ('fParity', DWORD, 1),
  242. ('fOutxCtsFlow', DWORD, 1),
  243. ('fOutxDsrFlow', DWORD, 1),
  244. ('fDtrControl', DWORD, 2),
  245. ('fDsrSensitivity', DWORD, 1),
  246. ('fTXContinueOnXoff', DWORD, 1),
  247. ('fOutX', DWORD, 1),
  248. ('fInX', DWORD, 1),
  249. ('fErrorChar', DWORD, 1),
  250. ('fNull', DWORD, 1),
  251. ('fRtsControl', DWORD, 2),
  252. ('fAbortOnError', DWORD, 1),
  253. ('fDummy2', DWORD, 17),
  254. ('wReserved', WORD),
  255. ('XonLim', WORD),
  256. ('XoffLim', WORD),
  257. ('ByteSize', BYTE),
  258. ('Parity', BYTE),
  259. ('StopBits', BYTE),
  260. ('XonChar', c_char),
  261. ('XoffChar', c_char),
  262. ('ErrorChar', c_char),
  263. ('EofChar', c_char),
  264. ('EvtChar', c_char),
  265. ('wReserved1', WORD),
  266. ]
  267. _COMMTIMEOUTS._fields_ = [
  268. ('ReadIntervalTimeout', DWORD),
  269. ('ReadTotalTimeoutMultiplier', DWORD),
  270. ('ReadTotalTimeoutConstant', DWORD),
  271. ('WriteTotalTimeoutMultiplier', DWORD),
  272. ('WriteTotalTimeoutConstant', DWORD),
  273. ]
  274. __all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
  275. 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
  276. 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
  277. 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
  278. 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
  279. 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
  280. '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
  281. 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
  282. 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
  283. 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
  284. 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
  285. 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
  286. 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
  287. 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
  288. 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
  289. 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
  290. 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
  291. 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
  292. 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
  293. 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
  294. 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
  295. 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
  296. 'MS_DSR_ON', 'MS_RING_ON',
  297. 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
  298. 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']