예제 #1
0
 def out_waiting(self):
     """Return how many bytes the in the outgoing buffer"""
     flags = win32.DWORD()
     comstat = win32.COMSTAT()
     if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)):
         raise SerialException("ClearCommError failed ({!r})".format(win32.WinError()))
     return comstat.cbOutQue
예제 #2
0
 def in_waiting(self):
     """Return the number of bytes currently in the input buffer."""
     flags = win32.DWORD()
     comstat = win32.COMSTAT()
     if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)):
         raise SerialException("ClearCommError failed ({!r})".format(win32.WinError()))
     return comstat.cbInQue
예제 #3
0
 def _cancel_overlapped_io(self, overlapped):
     """Cancel a blocking read operation, may be called from other thread"""
     # check if read operation is pending
     rc = win32.DWORD()
     err = win32.GetOverlappedResult(
         self._port_handle,
         ctypes.byref(overlapped),
         ctypes.byref(rc),
         False)
     if not err and win32.GetLastError() in (win32.ERROR_IO_PENDING, win32.ERROR_IO_INCOMPLETE):
         # cancel, ignoring any errors (e.g. it may just have finished on its own)
         win32.CancelIoEx(self._port_handle, overlapped)
예제 #4
0
 def read(self, size=1):
     """\
     Read size bytes from the serial port. If a timeout is set it may
     return less characters as requested. With no timeout it will block
     until the requested number of bytes is read.
     """
     if not self.is_open:
         raise PortNotOpenError()
     if size > 0:
         win32.ResetEvent(self._overlapped_read.hEvent)
         flags = win32.DWORD()
         comstat = win32.COMSTAT()
         if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)):
             raise SerialException("ClearCommError failed ({!r})".format(win32.WinError()))
         n = min(comstat.cbInQue, size) if self.timeout == 0 else size
         if n > 0:
             buf = ctypes.create_string_buffer(n)
             rc = win32.DWORD()
             read_ok = win32.ReadFile(
                 self._port_handle,
                 buf,
                 n,
                 ctypes.byref(rc),
                 ctypes.byref(self._overlapped_read))
             if not read_ok and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING):
                 raise SerialException("ReadFile failed ({!r})".format(win32.WinError()))
             result_ok = win32.GetOverlappedResult(
                 self._port_handle,
                 ctypes.byref(self._overlapped_read),
                 ctypes.byref(rc),
                 True)
             if not result_ok:
                 if win32.GetLastError() != win32.ERROR_OPERATION_ABORTED:
                     raise SerialException("GetOverlappedResult failed ({!r})".format(win32.WinError()))
             read = buf.raw[:rc.value]
         else:
             read = bytes()
     else:
         read = bytes()
     return bytes(read)
예제 #5
0
    def write(self, data):
        """Output the given byte string over the serial port."""
        if not self.is_open:
            raise PortNotOpenError()
        #~ if not isinstance(data, (bytes, bytearray)):
            #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
        # convert data (needed in case of memoryview instance: Py 3.1 io lib), ctypes doesn't like memoryview
        data = to_bytes(data)
        if data:
            #~ win32event.ResetEvent(self._overlapped_write.hEvent)
            n = win32.DWORD()
            success = win32.WriteFile(self._port_handle, data, len(data), ctypes.byref(n), self._overlapped_write)
            if self._write_timeout != 0:  # if blocking (None) or w/ write timeout (>0)
                if not success and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING):
                    raise SerialException("WriteFile failed ({!r})".format(win32.WinError()))

                # Wait for the write to complete.
                #~ win32.WaitForSingleObject(self._overlapped_write.hEvent, win32.INFINITE)
                win32.GetOverlappedResult(self._port_handle, self._overlapped_write, ctypes.byref(n), True)
                if win32.GetLastError() == win32.ERROR_OPERATION_ABORTED:
                    return n.value  # canceled IO is no error
                if n.value != len(data):
                    raise SerialTimeoutException('Write timeout')
                return n.value
            else:
                errorcode = win32.ERROR_SUCCESS if success else win32.GetLastError()
                if errorcode in (win32.ERROR_INVALID_USER_BUFFER, win32.ERROR_NOT_ENOUGH_MEMORY,
                                 win32.ERROR_OPERATION_ABORTED):
                    return 0
                elif errorcode in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING):
                    # no info on true length provided by OS function in async mode
                    return len(data)
                else:
                    raise SerialException("WriteFile failed ({!r})".format(win32.WinError()))
        else:
            return 0
예제 #6
0
 def _GetCommModemStatus(self):
     if not self.is_open:
         raise PortNotOpenError()
     stat = win32.DWORD()
     win32.GetCommModemStatus(self._port_handle, ctypes.byref(stat))
     return stat.value