Exemplo n.º 1
0
        public void OpenDevice(DeviceMode readMode, DeviceMode writeMode, ShareMode shareMode)
        {
            if (IsOpen)
            {
                return;
            }

            _deviceReadMode  = readMode;
            _deviceWriteMode = writeMode;
            _deviceShareMode = shareMode;

            try
            {
                ReadHandle  = OpenDeviceIO(_devicePath, readMode, NativeMethods.GENERIC_READ, shareMode);
                WriteHandle = OpenDeviceIO(_devicePath, writeMode, NativeMethods.GENERIC_WRITE, shareMode);
            }
            catch (Exception exception)
            {
                IsOpen = false;
                throw new Exception("Error opening HID device.", exception);
            }

            IsOpen = (ReadHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE &&
                      WriteHandle.ToInt32() != NativeMethods.INVALID_HANDLE_VALUE);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 做pipe的重定向
        /// </summary>
        /// <param name="uvStreamHandle"></param>
        /// <param name="index"></param>
        private void WriteHandleFree(UvStreamHandle uvStreamHandle, int index)
        {
            UvPipeHandle uvPipeHandle = _pipeHandleList[index];
            WriteHandle  writeHandle  = new WriteHandle();

            writeHandle.Init(_loopHandle);
            writeHandle.Free(uvPipeHandle, uvStreamHandle, Write2_CallBack, uvStreamHandle);
        }
Exemplo n.º 3
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    DisposeTokenSource?.Cancel();
                    DisposeTokenSource?.Dispose();

                    ReadHandle?.Dispose();
                    WriteHandle?.Dispose();
                }

                disposedValue = true;
            }
        }
Exemplo n.º 4
0
        public bool WriteOpen()
        {
            bool result = false;

            if (!IsWriteOpened)
            {
                try
                {
                    WriteHandle.Dispose();
                    WriteHandle = Helper.OpenDevice(DeviceInfo.DevicePath, DesiredAccesses.GenericWrite,
                                                    ShareModes.FileShareRead | ShareModes.FileShareWrite, FileFlags.FileFlagOverlapped);
                    result = IsWriteOpened;
                }
                catch (Exception e)
                {
                    throw new DeviceCouldNotOpenedException($"This device could not {nameof(WriteOpen)}. Please check inner exception ({e.GetType()})", e);
                }
            }

            return(result);
        }
Exemplo n.º 5
0
        //**************************************************************************************************************************************
        public int Send(int TimeUpW)
        {
            try
            {
                int intCmdLen = 0;
                SafeFileHandle WriteHandle;
                bool lngAPIReVal = false;
                int lngEndOfByte = 0;

                //1. Calculate SendData Length
                intCmdLen = SendData[0] * 256 + SendData[1];
                if (intCmdLen > (MaxLengs + 1))
                {
                    return 255; //Error code
                }

                //2. Create File
                WriteHandle = CreateFile("\\\\.\\\\" + strPIPE_NAME, GENERIC_WRITE, 0, IntPtr.Zero,
                                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
                if (WriteHandle.IsInvalid == true)
                {
                    return 1; //Error code
                }

                //3. Write File
                lngAPIReVal = WriteFile(WriteHandle, SendData, intCmdLen + 2, ref lngEndOfByte, IntPtr.Zero); //Send only length of factory command & 2 first byte
                if (!(WriteHandle.IsInvalid))
                {
                    WriteHandle.Close();
                }

                //4. If everything ok. Return 0: successful code
                return 0; //Successful code
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.Message, "Send()");
                return 9999; //Unexpected Error code
            }
        }
Exemplo n.º 6
0
        public void OpenDevice()
        {
            if (IsOpen)
            {
                return;
            }



            try
            {
                ReadHandle       = OpenDeviceIO(this.DevicePath, DeviceMode.NonOverlapped, Native.GENERIC_READ);
                WriteHandle      = OpenDeviceIO(this.DevicePath, DeviceMode.NonOverlapped, Native.GENERIC_WRITE);
                ReadAsyncHandle  = OpenDeviceIO(this.DevicePath, DeviceMode.Overlapped, Native.GENERIC_READ);
                WriteAsyncHandle = OpenDeviceIO(this.DevicePath, DeviceMode.Overlapped, Native.GENERIC_WRITE);
            }
            catch (Exception exception)
            {
                IsOpen = false;
                throw new Exception("Error opening HID device.", exception);
            }

            IsOpen = ReadHandle.ToInt32() != Native.INVALID_HANDLE_VALUE & WriteHandle.ToInt32() != Native.INVALID_HANDLE_VALUE;
        }
Exemplo n.º 7
0
        private void WriteForPost(object state)
        {
            WriteStateForPost writeStateForPost = (WriteStateForPost)state;
            WriteHandle       writeHandle       = new WriteHandle();

            writeHandle.Init(_clientHandle.Loop);
            List <byte[]> buffers = writeStateForPost.Buffers;

            try
            {
                if (buffers.Count == 1)
                {
                    writeHandle.Write(_clientHandle, buffers[0], new Action <int, Exception, object>(WriteCallback), writeStateForPost.WriteObject);
                }
                else if (buffers.Count == 2)
                {
                    writeHandle.Write(_clientHandle, buffers[0], buffers[1], new Action <int, Exception, object>(WriteCallback), writeStateForPost.WriteObject);
                }
            }
            catch (Exception ex)
            {
                WriteCallback(-1, ex, writeStateForPost.WriteObject);
            }
        }
Exemplo n.º 8
0
 public void WriteClose()
 {
     WriteHandle?.Dispose();
 }