Skip to content
Permalink
Browse files

fix CancelIoEx not found on old windows versions (2003, XP)

  • Loading branch information...
WorldException committed Oct 12, 2016
1 parent 8768655 commit c4dd5a2790ac8482ca5bd8bf7547e96a2259186d
Showing with 26 additions and 4 deletions.
  1. +20 −1 serial/serialwin32.py
  2. +6 −3 serial/win32.py
@@ -12,6 +12,7 @@
# pylint: disable=invalid-name,too-few-public-methods
import ctypes
import time
import platform
from serial import win32

import serial
@@ -240,10 +241,28 @@ def _close(self):
win32.CloseHandle(self._port_handle)
self._port_handle = None

def _close_xp(self):
""" works on xp an older """
if self._port_handle is not None:
# Restore original timeout values:
win32.SetCommTimeouts(self._port, self._orgTimeouts)
# Close COM-Port:
if self._overlapped_read is not None:
win32.CloseHandle(self._overlapped_read.hEvent)
self._overlapped_read = None
if self._overlapped_write is not None:
win32.CloseHandle(self._overlapped_write.hEvent)
self._overlapped_write = None
win32.CloseHandle(self._port_handle)
self._port_handle = None

def close(self):
"""Close port"""
if self.is_open:
self._close()
if platform.win32_ver() in ['XP','2003Server']:
self._close_xp()
else:
self._close()
self.is_open = False

# - - - - - - - - - - - - - - - - - - - - - - - -
@@ -179,9 +179,12 @@ class _COMMTIMEOUTS(Structure):
WaitForSingleObject.restype = DWORD
WaitForSingleObject.argtypes = [HANDLE, DWORD]

CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
CancelIoEx.restype = BOOL
CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]
try:
CancelIoEx = _stdcall_libraries['kernel32'].CancelIoEx
CancelIoEx.restype = BOOL
CancelIoEx.argtypes = [HANDLE, LPOVERLAPPED]
except:
CancelIoEx = None

ONESTOPBIT = 0 # Variable c_int
TWOSTOPBITS = 2 # Variable c_int

0 comments on commit c4dd5a2

Please sign in to comment.
You can’t perform that action at this time.