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