public static bool GetDeviceIndex(ushort vid, ushort pid, string serial, ref uint deviceIndex) { uint index = 0; bool found = false; // Iterate through each connected device and search for a device // with a serial string matching the user selected device in the // device list for (uint i = 0; i < Hid.GetNumHidDevices(vid, pid); i++) { StringBuilder str = new StringBuilder((int)Hid.MAX_SERIAL_STRING_LENGTH); if (Hid.GetHidString(i, vid, pid, Hid.HID_SERIAL_STRING, str, Hid.MAX_SERIAL_STRING_LENGTH) == Hid.HID_DEVICE_SUCCESS) { // Device serial strings match if (serial == str.ToString()) { index = i; found = true; break; } } } deviceIndex = index; return(found); }
public HIDHandle(Hid handle) { this.hnd = handle; }
public HIDHandle() { this.hnd = null; }
private DeviceHandle openDevice() { try { DeviceTypeID eid; if ((Thread.CurrentThread.Name == string.Empty) || (Thread.CurrentThread.Name == null)) { Thread.CurrentThread.Name = "MAIN thread"; } Hid hid = new Hid(); foreach (ushort num in new ushort[] { ushort.Parse(SRW006.PID, NumberStyles.HexNumber), ushort.Parse(UDP001.PID, NumberStyles.HexNumber) }) { hid = Hid.Connect(ushort.Parse(SRW006.VID, NumberStyles.HexNumber), num, 20, 20); if (hid != null) { break; } } if (hid == null) { return null; } if (!this.isDeviceAlive(hid.ADT.PortAddress, out eid)) { throw new Exception("HID device opened but not responding..."); } HIDHandle handle = new HIDHandle(hid); DeviceHandle handle2 = new DeviceHandle(handle); handle2.HIDhnd.hnd.ADT.DeviceInfo = this.queryDeviceInfo(handle2); _log.Info("Device Successfully Opened: " + hid.ADT.PortAddress.hidAddr.Serial); try { WDSFrameWindow window = WDS_MainProg.frameWin(); switch (((AddDeviceResult) window.Invoke(new AddDevice(window.addDevice), new object[] { handle2.HIDhnd.hnd.ADT }))) { case AddDeviceResult.AppStarted: case AddDeviceResult.FirmwareReplaced: case AddDeviceResult.ExcludeOrCancel: case AddDeviceResult.Aborted: goto Label_019D; } } catch (Exception exception) { _log.Error("Invoking AddDevice failed " + exception.Message); } Label_019D: this.open(); return handle2; } catch (Exception exception2) { _log.Error(exception2.Message); return null; } }
public DeviceHandle open(ADTRecord adtRecord, char mod) { try { Hid handle = new Hid(); bool flag = false; int num = 10; do { foreach (ushort num2 in new ushort[] { ushort.Parse(SRW006.PID, NumberStyles.HexNumber), ushort.Parse(UDP001.PID, NumberStyles.HexNumber) }) { if (handle.Connect(ushort.Parse(SRW006.VID, NumberStyles.HexNumber), num2, adtRecord.PortAddress.hidAddr.Serial, 20, 20)) { flag = true; num = 0; break; } } Thread.Sleep(50); } while (--num > 0); if (flag) { DeviceTypeID eid; handle.ADT = adtRecord; handle.ADT.PortAddress.handle = new DeviceHandle(new HIDHandle(handle)); handle.ReceiveBuffer = new DataBuffer(2 * handle.GetInputReportBufferLength()); handle.ADT.DeviceInfo = this.queryDeviceInfo(handle.ADT.PortAddress.handle); if (!this.isDeviceAlive(handle.ADT.PortAddress, out eid)) { throw new Exception("HID device opened but not responding..."); } _log.Info("Device Successfully Opened: " + handle.ADT.PortAddress.hidAddr.Serial); return handle.ADT.PortAddress.handle; } return null; } catch (Exception exception) { _log.Error(exception.Message); return null; } }
public static Hid Connect(ushort vid, ushort pid, uint getReportTimeout, uint setReportTimeout) { Hid handle = new Hid(); lock (singleLock) { StringBuilder deviceString = new StringBuilder(260); uint deviceIndex = 0; ADTManager manager = ADTManager.instance(); ADTRecord record = new ADTRecord(); IOPortAddress portAddress = new IOPortAddress { portType = PortType.HID }; portAddress.hidAddr.PID = pid; portAddress.hidAddr.VID = vid; for (uint i = 0; i < GetNumHidDevices(vid, pid); i++) { byte num3 = GetHidString(i, vid, pid, 4, deviceString, 0x100); if (num3 != 3) { if (num3 != 0) { return null; } portAddress.hidAddr.Serial = deviceString.ToString(); if (manager.findADTRecord(portAddress) != null) { return null; } break; } } if (!GetDeviceIndex(vid, pid, portAddress.hidAddr.Serial, ref deviceIndex)) { return null; } if (handle.Open(deviceIndex, vid, pid, 0x200) != 0) { return null; } handle.SetTimeouts(getReportTimeout, setReportTimeout); handle.ADT = new ADTRecord(portAddress, new DeviceDescriptor()); handle.ADT.PortAddress.handle = new DeviceHandle(new HIDHandle(handle)); deviceString = new StringBuilder(260); if (handle.GetString(3, deviceString, 260) != 0) { return null; } handle.ADT.PortAddress.hidAddr.DeviceString = deviceString.ToString(); handle.ReceiveBuffer = new DataBuffer(handle.GetInputReportBufferLength()); } return handle; }