'Output the given byte string over the serial port.'

def write(self, data):

    if (not self.is_open):
        raise portNotOpenError
    data = to_bytes(data)
    if data:
        n = win32.DWORD()
        success = win32.WriteFile(self._port_handle, data, len(data), ctypes.byref(n), self._overlapped_write)
        if (self._write_timeout != 0):
            if ((not success) and (win32.GetLastError() != win32.ERROR_IO_PENDING)):
                raise SerialException('WriteFile    failed    ({!r})'.format(ctypes.WinError()))
            win32.GetOverlappedResult(self._port_handle, self._overlapped_write, ctypes.byref(n), True)
            if (win32.GetLastError() == win32.ERROR_OPERATION_ABORTED):
                return n.value
            if (n.value != len(data)):
                raise writeTimeoutError
            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)):
                return len(data)
            else:
                raise SerialException('WriteFile    failed    ({!r})'.format(ctypes.WinError()))
    else:
        return 0
