win32.py 11 KB

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