Read() public method

Reads HID Input Reports.
public Read ( ) : byte[]
return byte[]
Exemplo n.º 1
0
 public static void StartRead(Action <byte[]> onRead)
 {
     _onRead = onRead;
     System.Threading.Thread t = new System.Threading.Thread(() =>
     {
         while (_onRead != null)
         {
             if (stream != null)
             {
                 try
                 {
                     var buf = stream.Read();
                     _onRead(buf);
                 }
                 catch (System.IO.IOException err)//io异常,断开
                 {
                     atcdevice = null;
                     stream    = null;
                 }
                 catch (TimeoutException err)//超时异常,记录
                 {
                     readErrorCount++;
                 }
             }
         }
     });
     t.Start();
 }
Exemplo n.º 2
0
        private byte[] Read(HidStream steam, int length, BackgroundWorker worker = null)
        {
            var offset = 0;
            var result = new byte[length];
            while (offset < length)
            {
                var data = new byte[m_receiveBufferLength];
                steam.Read(data);
                var bufferLength = offset + data.Length < length
                    ? data.Length
                    : length - offset;

                Buffer.BlockCopy(data, 1, result, offset, bufferLength - 1);
                offset += bufferLength == data.Length
                    ? bufferLength - 1
                    : bufferLength;

                if (worker != null) worker.ReportProgress((int)(offset * 100f / length));
            }
            if (worker != null) worker.ReportProgress(100);
            return result;
        }
Exemplo n.º 3
0
        /* execute a HID bootloader command */
        private byte[] ExecuteHIDCommand(HidStream stream, int command, int address = 0, byte[] data = null)
        {
            var buffer = new byte[64];
            buffer[0] = 0;
            buffer[1] = (byte)command;
            buffer[2] = (byte)(address & 0xFF);
            buffer[3] = (byte)((address >> 8) & 0xFF);

            if (data != null)
            {
                if (data.Length > 60)
                    //return null;
                    throw new System.ArgumentException("Data length cannot be >60 bytes", "original");
                for (int i = 0; i < data.Length; i++)
                    buffer[4 + i] = data[i];
            }

            try
            {
                stream.Write(buffer);

                stream.ReadTimeout = 100;
                byte[] result = stream.Read();
                if (result.Length < 4)
                    return null;
                if (result[1] != (byte)(command | 0x80))
                    return null;
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return null;
            }
        }