コード例 #1
0
        protected HIDReport ReadData(int timeout)
        {
            var buffer  = new byte[] { };
            var status  = HIDReport.ReadStatus.NoDataRead;
            int error   = 0;
            var success = false;

            if (InputReportByteLength > 0)
            {
                uint bytesRead = 0;

                buffer = CreateInputBuffer();

                if (timeout > 0)
                {
                    var security       = new Native.SECURITY_ATTRIBUTES();
                    var overlapped     = new NativeOverlapped();
                    var overlapTimeout = timeout;

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

                    overlapped.OffsetLow   = 0;
                    overlapped.OffsetHigh  = 0;
                    overlapped.EventHandle = Native.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);

                    try
                    {
                        success = Native.ReadFile(ReadAsyncHandle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        // UnityEngine.Debug.Log("Read happend " + success + " " + bytesRead);

                        error = Marshal.GetLastWin32Error();



                        if (!success && (error == Native.ERROR_IO_PENDING || bytesRead < buffer.Length))
                        {
                            UnityEngine.Debug.LogWarning("Wait reading...");

                            var result = Native.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                            switch (result)
                            {
                            case Native.WAIT_OBJECT_0: status = HIDReport.ReadStatus.Success; break;

                            case Native.WAIT_TIMEOUT:
                                status = HIDReport.ReadStatus.WaitTimedOut;
                                UnityEngine.Debug.Log("ReadData_WAIT_TIMEOUT");
                                // buffer = new byte[] { };
                                break;

                            case Native.WAIT_FAILED:
                                status = HIDReport.ReadStatus.WaitFail;
                                UnityEngine.Debug.Log("ReadData_WAIT_FAILED");
                                //  buffer = new byte[] { };
                                break;

                            case Native.WAIT_ABANDONED:
                                status = HIDReport.ReadStatus.Success;

                                UnityEngine.Debug.Log("ReadData_WAIT_ABANDONED" + result);
                                //   buffer = new byte[] { };
                                break;

                            default:
                                status = HIDReport.ReadStatus.NoDataRead;

                                UnityEngine.Debug.Log("ReadData Default" + result);

                                break;
                            }
                        }
                        else
                        {
                            if (error > 0)
                            {
                                if (error == 1167)
                                {
                                    IsConnected = false;
                                    status      = HIDReport.ReadStatus.NotConnected;
                                }
                                else
                                {
                                    status = HIDReport.ReadStatus.ReadError;
                                    UnityEngine.Debug.LogWarning("Error during Read Data:" + error);
                                }
                            }
                            else
                            {
                                status = HIDReport.ReadStatus.Success;
                            }
                        }
                    }
                    catch { status = HIDReport.ReadStatus.ReadError; }
                    finally { CloseDeviceIO(overlapped.EventHandle); }
                }
                else
                {
                    try
                    {
                        var overlapped = new NativeOverlapped();

                        success = Native.ReadFile(ReadHandle, buffer, (uint)buffer.Length, out bytesRead, ref overlapped);

                        error = Marshal.GetLastWin32Error();

                        status = HIDReport.ReadStatus.Success;

                        if (error > 0)
                        {
                            if (error == 1167)
                            {
                                IsConnected = false;
                                status      = HIDReport.ReadStatus.NotConnected;
                            }
                            else
                            {
                                status = HIDReport.ReadStatus.ReadError;
                                UnityEngine.Debug.LogWarning("Error during Read Data_" + error);
                            }
                        }
                    }
                    catch { status = HIDReport.ReadStatus.ReadError; }
                }
            }

            //TODO add syncronization
            __lastHIDReport.Data = buffer;

            __lastHIDReport.index = this.index;

            __lastHIDReport.Status = status;


            return(__lastHIDReport);// new HIDReport(this.index, buffer, status);
        }
コード例 #2
0
        private bool WriteData(byte[] data, int timeout)
        {
            var  buffer       = CreateOutputBuffer();
            uint bytesWritten = 0;
            int  error        = 0;

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

            if (timeout > 0)
            {
                var security = new Native.SECURITY_ATTRIBUTES();

                var overlapped = new NativeOverlapped();

                // var overlapTimeout = timeout <= 0 ? Native.WAIT_INFINITE : timeout;
                var overlapTimeout = timeout;

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

                overlapped.OffsetLow   = 0;
                overlapped.OffsetHigh  = 0;
                overlapped.EventHandle = Native.CreateEvent(ref security, Convert.ToInt32(false), Convert.ToInt32(true), "");

                bool success;


                success = Native.WriteFile(WriteAsyncHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);
                UnityEngine.Debug.Log("WriteFile happend " + success + " " + bytesWritten);

                error = Marshal.GetLastWin32Error();


                // UnityEngine.Debug.LogError(Marshal.GetLastWin32Error());


                if (!success && error == Native.ERROR_IO_PENDING)
                {
                    var result = Native.WaitForSingleObject(overlapped.EventHandle, overlapTimeout);

                    //TODO clean overlapped
                    // System.Threading.Overlapped.Unpack(overlapped);

                    switch (result)
                    {
                    case Native.WAIT_OBJECT_0:


                        return(true);

                    case Native.WAIT_TIMEOUT:
                        UnityEngine.Debug.Log("WriteData WAIT_TIMEOUT");
                        return(false);

                    case Native.WAIT_FAILED:
                        UnityEngine.Debug.Log("WriteData WAIT_FAILED");
                        return(false);

                    default:
                        return(false);
                    }
                }

                if (error > 0)
                {
                    UnityEngine.Debug.LogWarning("Error during Write Data " + error);
                    //return false;
                }

                return(success);
            }
            else
            {
                try
                {
                    var  overlapped = new NativeOverlapped();
                    bool success;
                    success = Native.WriteFile(WriteHandle, buffer, (uint)buffer.Length, out bytesWritten, ref overlapped);

                    if (!success)
                    {
                        UnityEngine.Debug.LogWarning(Marshal.GetLastWin32Error().ToString());
                    }

                    return(success);
                }
                catch (Exception ex) {
                    UnityEngine.Debug.LogException(ex);
                    return(false);
                }
            }
        }