win32.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. from ctypes import *
  10. from ctypes.wintypes import HANDLE
  11. from ctypes.wintypes import BOOL
  12. from ctypes.wintypes import LPCWSTR
  13. from ctypes.wintypes import DWORD
  14. from ctypes.wintypes import WORD
  15. from ctypes.wintypes import BYTE
  16. _stdcall_libraries = {}
  17. _stdcall_libraries['kernel32'] = WinDLL('kernel32')
  18. INVALID_HANDLE_VALUE = HANDLE(-1).value
  19. # some details of the windows API differ between 32 and 64 bit systems..
  20. def is_64bit():
  21. """Returns true when running on a 64 bit system"""
  22. return sizeof(c_ulong) != sizeof(c_void_p)
  23. # ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
  24. # is either 32 or 64 bits, depending on the type of windows...
  25. # so test if this a 32 bit windows...
  26. if is_64bit():
  27. # assume 64 bits
  28. ULONG_PTR = c_int64
  29. else:
  30. # 32 bits
  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. ONESTOPBIT = 0 # Variable c_int
  133. TWOSTOPBITS = 2 # Variable c_int
  134. ONE5STOPBITS = 1
  135. NOPARITY = 0 # Variable c_int
  136. ODDPARITY = 1 # Variable c_int
  137. EVENPARITY = 2 # Variable c_int
  138. MARKPARITY = 3
  139. SPACEPARITY = 4
  140. RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
  141. RTS_CONTROL_DISABLE = 0 # Variable c_int
  142. RTS_CONTROL_ENABLE = 1 # Variable c_int
  143. RTS_CONTROL_TOGGLE = 3 # Variable c_int
  144. SETRTS = 3
  145. CLRRTS = 4
  146. DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
  147. DTR_CONTROL_DISABLE = 0 # Variable c_int
  148. DTR_CONTROL_ENABLE = 1 # Variable c_int
  149. SETDTR = 5
  150. CLRDTR = 6
  151. MS_DSR_ON = 32 # Variable c_ulong
  152. EV_RING = 256 # Variable c_int
  153. EV_PERR = 512 # Variable c_int
  154. EV_ERR = 128 # Variable c_int
  155. SETXOFF = 1 # Variable c_int
  156. EV_RXCHAR = 1 # Variable c_int
  157. GENERIC_WRITE = 1073741824 # Variable c_long
  158. PURGE_TXCLEAR = 4 # Variable c_int
  159. FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
  160. EV_DSR = 16 # Variable c_int
  161. MAXDWORD = 4294967295 # Variable c_uint
  162. EV_RLSD = 32 # Variable c_int
  163. ERROR_SUCCESS = 0
  164. ERROR_IO_PENDING = 997 # Variable c_long
  165. MS_CTS_ON = 16 # Variable c_ulong
  166. EV_EVENT1 = 2048 # Variable c_int
  167. EV_RX80FULL = 1024 # Variable c_int
  168. PURGE_RXABORT = 2 # Variable c_int
  169. FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
  170. PURGE_TXABORT = 1 # Variable c_int
  171. SETXON = 2 # Variable c_int
  172. OPEN_EXISTING = 3 # Variable c_int
  173. MS_RING_ON = 64 # Variable c_ulong
  174. EV_TXEMPTY = 4 # Variable c_int
  175. EV_RXFLAG = 2 # Variable c_int
  176. MS_RLSD_ON = 128 # Variable c_ulong
  177. GENERIC_READ = 2147483648 # Variable c_ulong
  178. EV_EVENT2 = 4096 # Variable c_int
  179. EV_CTS = 8 # Variable c_int
  180. EV_BREAK = 64 # Variable c_int
  181. PURGE_RXCLEAR = 8 # Variable c_int
  182. INFINITE = 0xFFFFFFFF
  183. class N11_OVERLAPPED4DOLLAR_48E(Union):
  184. pass
  185. class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
  186. pass
  187. N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
  188. ('Offset', DWORD),
  189. ('OffsetHigh', DWORD),
  190. ]
  191. PVOID = c_void_p
  192. N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
  193. N11_OVERLAPPED4DOLLAR_48E._fields_ = [
  194. ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
  195. ('Pointer', PVOID),
  196. ]
  197. _OVERLAPPED._anonymous_ = ['_0']
  198. _OVERLAPPED._fields_ = [
  199. ('Internal', ULONG_PTR),
  200. ('InternalHigh', ULONG_PTR),
  201. ('_0', N11_OVERLAPPED4DOLLAR_48E),
  202. ('hEvent', HANDLE),
  203. ]
  204. _SECURITY_ATTRIBUTES._fields_ = [
  205. ('nLength', DWORD),
  206. ('lpSecurityDescriptor', LPVOID),
  207. ('bInheritHandle', BOOL),
  208. ]
  209. _COMSTAT._fields_ = [
  210. ('fCtsHold', DWORD, 1),
  211. ('fDsrHold', DWORD, 1),
  212. ('fRlsdHold', DWORD, 1),
  213. ('fXoffHold', DWORD, 1),
  214. ('fXoffSent', DWORD, 1),
  215. ('fEof', DWORD, 1),
  216. ('fTxim', DWORD, 1),
  217. ('fReserved', DWORD, 25),
  218. ('cbInQue', DWORD),
  219. ('cbOutQue', DWORD),
  220. ]
  221. _DCB._fields_ = [
  222. ('DCBlength', DWORD),
  223. ('BaudRate', DWORD),
  224. ('fBinary', DWORD, 1),
  225. ('fParity', DWORD, 1),
  226. ('fOutxCtsFlow', DWORD, 1),
  227. ('fOutxDsrFlow', DWORD, 1),
  228. ('fDtrControl', DWORD, 2),
  229. ('fDsrSensitivity', DWORD, 1),
  230. ('fTXContinueOnXoff', DWORD, 1),
  231. ('fOutX', DWORD, 1),
  232. ('fInX', DWORD, 1),
  233. ('fErrorChar', DWORD, 1),
  234. ('fNull', DWORD, 1),
  235. ('fRtsControl', DWORD, 2),
  236. ('fAbortOnError', DWORD, 1),
  237. ('fDummy2', DWORD, 17),
  238. ('wReserved', WORD),
  239. ('XonLim', WORD),
  240. ('XoffLim', WORD),
  241. ('ByteSize', BYTE),
  242. ('Parity', BYTE),
  243. ('StopBits', BYTE),
  244. ('XonChar', c_char),
  245. ('XoffChar', c_char),
  246. ('ErrorChar', c_char),
  247. ('EofChar', c_char),
  248. ('EvtChar', c_char),
  249. ('wReserved1', WORD),
  250. ]
  251. _COMMTIMEOUTS._fields_ = [
  252. ('ReadIntervalTimeout', DWORD),
  253. ('ReadTotalTimeoutMultiplier', DWORD),
  254. ('ReadTotalTimeoutConstant', DWORD),
  255. ('WriteTotalTimeoutMultiplier', DWORD),
  256. ('WriteTotalTimeoutConstant', DWORD),
  257. ]
  258. __all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
  259. 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
  260. 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
  261. 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
  262. 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
  263. 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
  264. '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
  265. 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
  266. 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
  267. 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
  268. 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
  269. 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
  270. 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
  271. 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
  272. 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
  273. 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
  274. 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
  275. 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
  276. 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
  277. 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
  278. 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
  279. 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
  280. 'MS_DSR_ON', 'MS_RING_ON',
  281. 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
  282. 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']