示例#1
0
        private bool WriteData(byte[] data, int timeout)
        {
            if (deviceCapabilities.OutputReportByteLength <= 0)
            {
                return(false);
            }

            var buffer       = new byte[Capabilities.OutputReportByteLength];
            var bytesWritten = 0;

            Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, deviceCapabilities.OutputReportByteLength));

            if (deviceWriteMode == DeviceMode.Overlapped)
            {
                var security   = new NativeMethods.SECURITY_ATTRIBUTES();
                var overlapped = new NativeMethods.OVERLAPPED();

                var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                security.lpSecurityDescriptor = IntPtr.Zero;
                security.bInheritHandle       = true;
                security.nLength = Marshal.SizeOf(security);

                overlapped.Offset     = 0;
                overlapped.OffsetHigh = 0;
                overlapped.hEvent     = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), "");

                try
                {
                    NativeMethods.WriteFileOverlapped(WriteHandle, ref buffer[0], buffer.Length, ref bytesWritten, ref overlapped);
                }
                catch { return(false); }

                var result = NativeMethods.WaitForSingleObject(overlapped.hEvent, overlapTimeout);

                switch (result)
                {
                case NativeMethods.WAIT_OBJECT_0:
                    return(true);

                case NativeMethods.WAIT_TIMEOUT:
                    return(false);

                case NativeMethods.WAIT_FAILED:
                    return(false);

                default:
                    return(false);
                }
            }
            try
            {
                return(NativeMethods.WriteFile(WriteHandle, ref buffer[0], buffer.Length, ref bytesWritten, 0));
            }
            catch { return(false); }
        }
示例#2
0
        private bool WriteData(byte[] data, int timeout)
        {
            if (_deviceCapabilities.OutputReportByteLength <= 0) return false;

            var buffer = CreateOutputBuffer();
            var bytesWritten = 0;

            Array.Copy(data, 0, buffer, 0, Math.Min(data.Length, _deviceCapabilities.OutputReportByteLength));

            if (_deviceWriteMode == DeviceMode.Overlapped)
            {
                var security = new NativeMethods.SECURITY_ATTRIBUTES();
                var overlapped = new NativeMethods.OVERLAPPED();

                var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                security.lpSecurityDescriptor = IntPtr.Zero;
                security.bInheritHandle = true;
                security.nLength = Marshal.SizeOf(security);

                overlapped.Offset = 0;
                overlapped.OffsetHigh = 0;
                overlapped.hEvent = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), "");

                try
                {
                    NativeMethods.WriteFileOverlapped(WriteHandle, ref buffer[0], buffer.Length, ref bytesWritten, ref overlapped);
                }
                catch { return false; }

                var result = NativeMethods.WaitForSingleObject(overlapped.hEvent, overlapTimeout);

                switch (result)
                {
                    case NativeMethods.WAIT_OBJECT_0:
                        return true;
                    case NativeMethods.WAIT_TIMEOUT:
                        return false;
                    case NativeMethods.WAIT_FAILED:
                        return false;
                    default:
                        return false;
                }
            }
            try
            {
                return NativeMethods.WriteFile(WriteHandle, ref buffer[0], buffer.Length, ref bytesWritten, 0);
            }
            catch { return false; }
        }
示例#3
0
        private HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[] { };
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                var bytesRead = 0;

                buffer = CreateInputBuffer();

                if (_deviceReadMode == DeviceMode.Overlapped)
                {
                    var security = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped = new NativeMethods.OVERLAPPED();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.Offset = 0;
                    overlapped.OffsetHigh = 0;
                    overlapped.hEvent = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        NativeMethods.ReadFileOverlapped(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.hEvent, overlapTimeout);

                        switch (result)
                        {
                            case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; break;
                            case NativeMethods.WAIT_TIMEOUT:
                                status = HidDeviceData.ReadStatus.WaitTimedOut;
                                buffer = new byte[] {};
                                break;
                            case NativeMethods.WAIT_FAILED:
                                status = HidDeviceData.ReadStatus.WaitFail;
                                buffer = new byte[] { };
                                break;
                            default:
                                status = HidDeviceData.ReadStatus.NoDataRead;
                                buffer = new byte[] { };
                                break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
                else
                {
                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, IntPtr.Zero);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return new HidDeviceData(buffer, status);
        }
示例#4
0
        private HidDeviceData ReadData(int timeout)
        {
            var buffer = new byte[Capabilities.InputReportByteLength];
            var status = HidDeviceData.ReadStatus.NoDataRead;

            if (deviceCapabilities.InputReportByteLength > 0)
            {
                var bytesRead = 0;

                if (deviceReadMode == DeviceMode.Overlapped)
                {
                    var security       = new NativeMethods.SECURITY_ATTRIBUTES();
                    var overlapped     = new NativeMethods.OVERLAPPED();
                    var overlapTimeout = timeout <= 0 ? NativeMethods.WAIT_INFINITE : timeout;

                    security.lpSecurityDescriptor = IntPtr.Zero;
                    security.bInheritHandle       = true;
                    security.nLength = Marshal.SizeOf(security);

                    overlapped.Offset     = 0;
                    overlapped.OffsetHigh = 0;
                    overlapped.hEvent     = NativeMethods.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        NativeMethods.ReadFileOverlapped(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, ref overlapped);

                        var result = NativeMethods.WaitForSingleObject(overlapped.hEvent, overlapTimeout);

                        switch (result)
                        {
                        case NativeMethods.WAIT_OBJECT_0: status = HidDeviceData.ReadStatus.Success; break;

                        case NativeMethods.WAIT_TIMEOUT:
                            status = HidDeviceData.ReadStatus.WaitTimedOut;
                            buffer = new byte[] {};
                            break;

                        case NativeMethods.WAIT_FAILED:
                            status = HidDeviceData.ReadStatus.WaitFail;
                            buffer = new byte[] { };
                            break;

                        default:
                            status = HidDeviceData.ReadStatus.NoDataRead;
                            buffer = new byte[] { };
                            break;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
                else
                {
                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, ref buffer[0], buffer.Length, ref bytesRead, IntPtr.Zero);
                        if (bytesRead > 0)
                        {
                            status = HidDeviceData.ReadStatus.Success;
                        }
                    }
                    catch { status = HidDeviceData.ReadStatus.ReadError; }
                }
            }
            return(new HidDeviceData(buffer, status));
        }