public Write ( byte buffer ) : void | ||
buffer | byte | The buffer containing the report. Place the Report ID in the first byte. |
return | void |
public bool Send(byte[] bs) { if (stream == null) { return(false); } try { byte[] outb = new byte[atcdevice.MaxOutputReportLength]; outb[0] = 0;//fix hiddriver Array.Copy(bs, 0, outb, 1, Math.Min(bs.Length, outb.Length - 1)); stream.Write(outb); return(true); } catch (System.IO.IOException err)//io异常,断开 { atcdevice = null; stream = null; return(false); } catch (TimeoutException err)//超时异常,记录 { writeErrorCount++; return(false); } }
private void Write(HidStream steam, byte[] data, BackgroundWorker worker = null) { var offset = 0; while (offset < data.Length) { var bufferLength = data.Length - offset > m_sentBufferLength ? m_sentBufferLength : data.Length - offset; var buffer = new byte[bufferLength + 1]; { buffer[0] = 0; Buffer.BlockCopy(data, offset, buffer, 1, bufferLength); } steam.Write(buffer); offset += bufferLength; if (worker != null) worker.ReportProgress((int)(offset * 100f / data.Length)); } if (worker != null) worker.ReportProgress(100); }
/* 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; } }