def ReceiveThread(self): from win32event import ( ResetEvent, MsgWaitForMultipleObjects, QS_ALLINPUT, WAIT_OBJECT_0, WAIT_TIMEOUT, ) from win32file import ReadFile, AllocateReadBuffer, GetOverlappedResult from win32api import GetLastError continueLoop = True overlapped = self.serial._overlappedRead hComPort = self.serial.hComPort hEvent = overlapped.hEvent stopEvent = self.stopEvent n = 1 waitingOnRead = False buf = AllocateReadBuffer(n) while continueLoop: if not waitingOnRead: ResetEvent(hEvent) hr, _ = ReadFile(hComPort, buf, overlapped) if hr == 997: waitingOnRead = True elif hr == 0: pass #n = GetOverlappedResult(hComPort, overlapped, 1) #self.HandleChar(str(buf)) else: self.PrintError("error") raise rc = MsgWaitForMultipleObjects( (hEvent, stopEvent), 0, 1000, QS_ALLINPUT ) if rc == WAIT_OBJECT_0: n = GetOverlappedResult(hComPort, overlapped, 1) if n: self.HandleChar(str(buf)) #else: # print "WAIT_OBJECT_0", n, str(buf[:n]) waitingOnRead = False elif rc == WAIT_OBJECT_0+1: continueLoop = False elif rc == WAIT_TIMEOUT: pass else: self.PrintError("unknown message")
def _tx_task(self): SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) while self._runing: end_time = self.tx_poll_interval + time.time() with self._tx_lock: for sender in self._senders: msg = sender() if type(msg) is list: for m in msg: self._msg_send(m) elif msg is not None: self._msg_send(msg) time_left = end_time - time.time() time_left = max(0, int(time_left * 1000)) WaitForSingleObject(self._tx_event, time_left) ResetEvent(self._tx_event)
from win32event import SetEvent, ResetEvent, CreateEvent if __name__ == '__main__': evt = CreateEvent(None, 0, 0, 'hackathon_recording_event') SetEvent(evt) ResetEvent(evt)