|
20 | 20 | boottimeEpoch = 0 |
21 | 21 |
|
22 | 22 | try: |
23 | | - import win32event |
| 23 | + # Try builtin Python 3 Windows API |
| 24 | + from _overlapped import CreateEvent |
| 25 | + from _winapi import WaitForSingleObject, WAIT_OBJECT_0, INFINITE |
| 26 | + HAS_EVENTS = True |
24 | 27 | except ImportError: |
25 | | - win32event = None |
| 28 | + try: |
| 29 | + # Try pywin32 package |
| 30 | + from win32event import CreateEvent |
| 31 | + from win32event import WaitForSingleObject, WAIT_OBJECT_0, INFINITE |
| 32 | + HAS_EVENTS = True |
| 33 | + except ImportError: |
| 34 | + # Use polling instead |
| 35 | + HAS_EVENTS = False |
26 | 36 |
|
27 | 37 | if sys.version_info >= (3, 3): |
28 | 38 | # new in 3.3 |
@@ -87,8 +97,8 @@ def __init__(self, channel, *args, **kwargs): |
87 | 97 | if result != PCAN_ERROR_OK: |
88 | 98 | raise PcanError(self._get_formatted_error(result)) |
89 | 99 |
|
90 | | - if win32event is not None: |
91 | | - self._recv_event = win32event.CreateEvent(None, 0, 0, None) |
| 100 | + if HAS_EVENTS: |
| 101 | + self._recv_event = CreateEvent(None, 0, 0, None) |
92 | 102 | result = self.m_objPCANBasic.SetValue( |
93 | 103 | self.m_PcanHandle, PCAN_RECEIVE_EVENT, self._recv_event) |
94 | 104 | if result != PCAN_ERROR_OK: |
@@ -153,28 +163,25 @@ def reset(self): |
153 | 163 | return status == PCAN_ERROR_OK |
154 | 164 |
|
155 | 165 | def recv(self, timeout=None): |
156 | | - if win32event is not None: |
| 166 | + if HAS_EVENTS: |
157 | 167 | # We will utilize events for the timeout handling |
158 | | - timeout_ms = int(timeout * 1000) if timeout is not None else win32event.INFINITE |
| 168 | + timeout_ms = int(timeout * 1000) if timeout is not None else INFINITE |
159 | 169 | elif timeout is not None: |
160 | 170 | # Calculate max time |
161 | 171 | end_time = timeout_clock() + timeout |
162 | | - else: |
163 | | - # Skip timeout handling |
164 | | - end_time = 0 |
165 | 172 |
|
166 | 173 | log.debug("Trying to read a msg") |
167 | 174 |
|
168 | 175 | result = None |
169 | 176 | while result is None: |
170 | 177 | result = self.m_objPCANBasic.Read(self.m_PcanHandle) |
171 | 178 | if result[0] == PCAN_ERROR_QRCVEMPTY: |
172 | | - if win32event is not None: |
| 179 | + if HAS_EVENTS: |
173 | 180 | result = None |
174 | | - val = win32event.WaitForSingleObject(self._recv_event, timeout_ms) |
175 | | - if val != win32event.WAIT_OBJECT_0: |
| 181 | + val = WaitForSingleObject(self._recv_event, timeout_ms) |
| 182 | + if val != WAIT_OBJECT_0: |
176 | 183 | return None |
177 | | - elif timeout_clock() >= end_time: |
| 184 | + elif timeout is not None and timeout_clock() >= end_time: |
178 | 185 | return None |
179 | 186 | else: |
180 | 187 | result = None |
|
0 commit comments