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