void HID() { HID_DEVICE[] pDevice = new HID_DEVICE[1]; while (true) { Thread.Sleep(1); if (nbrDevices != FindNumberDevices() || CheckNewDevice) { nbrDevices = FindNumberDevices(); pDevice = new HID_DEVICE[nbrDevices]; FindKnownHidDevices(ref pDevice); var i = 0; while (i < nbrDevices) { var count = 0; if (pDevice[i].Attributes.VendorID == DEVICE_VID && DEVICE_VID != 0) { count++; } if (pDevice[i].Attributes.ProductID == DEVICE_PID) { count++; } if (pDevice[i].Caps.UsagePage == USAGE_PAGE) { count++; } if (pDevice[i].Caps.Usage == USAGE) { count++; } if (count == 4) { iHIDD = i; isConnected = true; break; } else { isConnected = false; } i++; } CheckNewDevice = false; } if (isConnected) { // Read(pDevice[iHIDD]); } } }
static HID_DEVICE OpenHidDevice(string DevicePath) { /*++ * RoutineDescription: * Given the HardwareDeviceInfo, representing a handle to the plug and * play information, and deviceInfoData, representing a specific hid device, * open that device and fill in all the relivant information in the given * HID_DEVICE structure. * --*/ var HidDevice = new HID_DEVICE(); HidDevice.DevicePath = DevicePath; // // The hid.dll api's do not pass the overlapped structure into deviceiocontrol // so to use them we must have a non overlapped device. If the request is for // an overlapped device we will close the device below and get a handle to an // overlapped device // HidDevice.HidDevice = CreateFile(HidDevice.DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, IntPtr.Zero); HidDevice.Caps = new HIDP_CAPS(); HidDevice.Attributes = new HIDD_ATTRIBUTES(); // // If the device was not opened as overlapped, then fill in the rest of the // HidDevice structure. However, if opened as overlapped, this handle cannot // be used in the calls to the HidD_ exported functions since each of these // functions does synchronous I/O. // HidD_FreePreparsedData(ref HidDevice.Ppd); HidDevice.Ppd = IntPtr.Zero; HidD_GetPreparsedData(HidDevice.HidDevice, ref HidDevice.Ppd); HidD_GetAttributes(HidDevice.HidDevice, ref HidDevice.Attributes); if (HidDevice.HidDevice != IntPtr.Zero) { HidP_GetCaps(HidDevice.Ppd, ref HidDevice.Caps); } //MessageBox.Show(GetLastError().ToString()); // // At this point the client has a choice. It may chose to look at the // Usage and Page of the top level collection found in the HIDP_CAPS // structure. In this way --------*it could just use the usages it knows about. // If either HidP_GetUsages or HidP_GetUsageValue return an error then // that particular usage does not exist in the report. // This is most likely the preferred method as the application can only // use usages of which it already knows. // In this case the app need not even call GetButtonCaps or GetValueCaps. // // In this example, however, we will call FillDeviceInfo to look for all // of the usages in the device. // //FillDeviceInfo(ref HidDevice); return(HidDevice); }
public static string GetProductString(HID_DEVICE HidDevice) { var chars = new byte[255]; if (HidD_GetProductString(HidDevice.HidDevice, chars, 255)) { return(Encoding.UTF8.GetString(chars)); } return(null); }
internal void HID_Write(byte[] Data) { HID_DEVICE[] pDevice = new HID_DEVICE[1]; nbrDevices = FindNumberDevices(); pDevice = new HID_DEVICE[nbrDevices]; FindKnownHidDevices(ref pDevice); byte[] test = new byte[5]; var idx = 0; while (idx < nbrDevices) { var count = 0; if (pDevice[idx].Attributes.VendorID == DEVICE_VID && DEVICE_VID != 0) { count++; } if (pDevice[idx].Attributes.ProductID == DEVICE_PID) { count++; } if (count == 2) { isConnected = true; break; } else { idx++; } isConnected = false; } if (isConnected == true) { byte[] Report = new byte[pDevice[idx].Caps.InputReportByteLength]; byte i = 0; while (i < pDevice[idx].Caps.InputReportByteLength) { try { Report[i] = Data[i]; i++; } catch { } } Report[0] = REPORT_IDw; uint tmp = 0; bool result = WriteFile(pDevice[idx].HidDevice, Report, pDevice[idx].Caps.OutputReportByteLength, ref tmp, IntPtr.Zero); } }
void Write(HID_DEVICE HidDevice) { byte[] Report = new byte[HidDevice.Caps.OutputReportByteLength]; uint tmp = 0; try { Report[0] = REPORT_IDw; Report[1] = WriteData; } catch { } WriteFile(HidDevice.HidDevice, Report, HidDevice.Caps.OutputReportByteLength, ref tmp, IntPtr.Zero); }
/// <summary> /// Writes a request for a short vibration pulse to selected device /// </summary> /// <param name="HidDevice">Device to vibrate</param> public static void Write(HID_DEVICE HidDevice) { byte[] Report = new byte[16]; uint tmp = 0; Report[0] = 0x03; // HID report ID (3 for bluetooth, any for USB) Report[1] = 0x0F; // Motor flag mask(?) Report[2] = 0x10; // Left trigger Report[3] = 0x10; // Right trigger Report[4] = 0x05; // Left rumble Report[5] = 0x05; // Right rumble // "Pulse" Report[6] = 0x0F; // On time Report[7] = 0x00; // Off time Report[8] = 0x00; // Number of repeats WriteFile(HidDevice.HidDevice, Report, 16, ref tmp, IntPtr.Zero); }
internal void HID_Inst(ushort VID, ushort PID, byte IDw, byte IDr) { HID_DEVICE[] pDevice = new HID_DEVICE[1]; DEVICE_VID = VID; DEVICE_PID = PID; REPORT_IDw = IDw; REPORT_IDr = IDr; nbrDevices = FindNumberDevices(); pDevice = new HID_DEVICE[nbrDevices]; FindKnownHidDevices(ref pDevice); var i = 0; while (i < nbrDevices) { var count = 0; if (pDevice[i].Attributes.VendorID == DEVICE_VID && DEVICE_VID != 0) { count++; } if (pDevice[i].Attributes.ProductID == DEVICE_PID) { count++; } //if (pDevice[i].Caps.UsagePage == USAGE_PAGE) // count++; // if (pDevice[i].Caps.Usage == USAGE) // count++; if (count == 2) { isConnected = true; break; } else { i++; isConnected = false; } } }
public static void CloseHidDevice(HID_DEVICE HidDevice) { CloseHandle(HidDevice.HidDevice); }
internal byte[] HID_Read() { HID_DEVICE[] pDevice = new HID_DEVICE[1]; nbrDevices = FindNumberDevices(); pDevice = new HID_DEVICE[nbrDevices]; FindKnownHidDevices(ref pDevice); byte[] test = new byte[5]; var idx = 0; while (idx < nbrDevices) { var count = 0; if (pDevice[idx].Attributes.VendorID == DEVICE_VID && DEVICE_VID != 0) { count++; } if (pDevice[idx].Attributes.ProductID == DEVICE_PID) { count++; } if (count == 2) { isConnected = true; break; } else { idx++; } isConnected = false; } if (isConnected == true) { string meseg = ""; //TEST meseg byte[] Data_USB = new byte[66]; byte[] Report = new byte[pDevice[idx].Caps.InputReportByteLength]; uint tmp = 0; try { Report[0] = REPORT_IDr; } catch { } ReadFile(pDevice[idx].HidDevice, Report, pDevice[idx].Caps.InputReportByteLength, ref tmp, IntPtr.Zero); try { //extBox16.Clear(); } catch { } byte i = 0; while (i < pDevice[idx].Caps.InputReportByteLength) { try { Data_USB[i] = Convert.ToByte(Report[i++].ToString()); // meseg += Report[i++].ToString(); } catch { } } return(Data_USB); //MessageBox.Show(meseg); } return(test); }