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); } }
public static int CheckDevice() { var devices = loader.GetDevices(0x0483, 0x572b); var signerCount = devices.Count(); if (devices.Count() == 0)//直接移除设备 { atcdevice = null; stream = null; return(0); } if (atcdevice == null)//打开设备 { atcdevice = devices.First(); stream = atcdevice.Open(); return(signerCount); } bool bFind = false; foreach (var d in devices) { if (d.DevicePath == atcdevice.DevicePath) {//same bFind = true; } } //找不到设备了 if (!bFind) { atcdevice = null; stream = null; } return(signerCount); }
public void Check() { var devices = loader.GetDevices(vid, pid); signerCount = devices.Count(); if (devices.Count() == 0)//直接移除设备 { atcdevice = null; stream = null; return; } if (atcdevice == null)//打开设备 { atcdevice = devices.First(); stream = atcdevice.Open(); return; } bool bFind = false; foreach (var d in devices) { if (d.DevicePath == atcdevice.DevicePath) {//same bFind = true; } } //找不到设备了 if (!bFind) { atcdevice = null; stream = null; } }
/// <inheritdoc/> public bool TryOpen(OpenConfiguration openConfig, out HidStream stream) { DeviceStream baseStream; bool result = base.TryOpen(openConfig, out baseStream); stream = (HidStream)baseStream; return(result); }
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(); }
/// <summary> /// Tries to make a connection to the USB HID class device. /// </summary> /// <param name="stream">The stream to use to communicate with the device.</param> /// <returns>True if the connetion was successful.</returns> public bool TryOpen(out HidStream stream) { try { stream = Open(); return(true); } catch (Exception e) { #if DEBUG Console.WriteLine(e); #endif stream = null; return(false); } }
/// <summary> /// Tries to make a connection to the USB HID class device. /// </summary> /// <param name="stream">The stream to use to communicate with the device.</param> /// <returns>True if the connetion was successful.</returns> public bool TryOpen(out HidStream stream) { try { stream = Open(); return true; } catch (Exception e) { #if DEBUG Console.WriteLine(e); #endif stream = null; return false; } }
public void StartRead(Action <byte[]> onRead) { //用異步接口,不用開綫程 byte[] buf = new byte[65]; _onRead = onRead; System.Threading.Thread t = new System.Threading.Thread(() => { while (_onRead != null) { System.Threading.Thread.Sleep(5); if (stream != null) { //try { int read = 0; Action readlast = null; readlast = () => { stream.BeginRead(buf, read, 65 - read, (a) => { if (_onRead == null || stream == null) { return; } int c = 0; try { c = stream.EndRead(a); } catch (System.IO.IOException err)//io异常,断开 { atcdevice = null; stream = null; return; } catch (TimeoutException err)//超时异常,记录 { readErrorCount++; } read += c; if (read < 65) { readlast(); } else { _onRead(buf.Skip(1).ToArray()); read = 0; readlast(); } }, null); while (true) { if (stream == null) { break; } System.Threading.Thread.Sleep(5); } }; readlast(); //var buf = stream.Read(); //_onRead(buf); } //catch (System.IO.IOException err)//io异常,断开 //{ // atcdevice = null; // stream = null; //} //catch (TimeoutException err)//超时异常,记录 //{ // readErrorCount++; //} } } }); t.Start(); }
/// <summary> /// Cleans up all open connections. This should be called when you are done using the card. /// </summary> public void dispose() { if (_stream != null) { _stream.Close(); _stream = null; } }
/// <inheritdoc/> public bool TryOpen(out HidStream stream) { return(TryOpen(null, out stream)); }
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); }
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; }
/* 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; } }
/* check the bootloader before use */ private bool CheckBootloader(HidStream stream) { // check bootloader version var version_res = ExecuteHIDCommand(stream, (int)BootloaderCommands.READ_BOOTLAODER_VERSION); if (version_res == null) return false; Console.WriteLine("Bootloader version: " + version_res[3].ToString()); if (version_res[3] < MinBootloaderVersion) { Console.WriteLine("Bootloader too old.\n"); return false; } // get serial number var serial_res = ExecuteHIDCommand(stream, (int)BootloaderCommands.READ_SERIAL); if (serial_res == null) return false; if (serial_res.Length <= 5) return false; Console.Write("Serial: "); for (int i = 5; i < serial_res.Length; i++) Console.Write((char)serial_res[i]); Console.WriteLine(""); return true; }