public List <string> GetDevicesList() { List <string> devices = new List <string>(); this.DevicePath = string.Empty; myUSB.CT_HidGuid(); myUSB.CT_SetupDiGetClassDevs(); bool?result = null; bool resultb = false; int device_count = 0; int size = 0; int requiredSize = 0; int numberOfDevices = 0; //search the device until you have found it or no more devices in list while (result.HasValue == false || result.Value == true) { //open the device result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count); //get size of device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceBuffer(ref requiredSize, 0); size = requiredSize; //get device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size); //is this the device i want? string deviceID = this.VID; if (myUSB.DevicePathName.IndexOf(deviceID) > 0) { //create HID Device Handel resultb = myUSB.CT_CreateFile(myUSB.DevicePathName); // Check the serial Number myUSB.CT_HidD_GetHIDSerialNumber(out string device_sn); myUSB.CT_CloseFile(); if (device_sn != "") { devices.Add(device_sn); } numberOfDevices++; } device_count++; } myUSB.CT_SetupDiDestroyDeviceInfoList(); return(devices); }
//---#+************************************************************************ //---NOTATION: //- bool searchDevice() //- //--- DESCRIPTION: //-- tries to find the device with specified vendorID and productID // Autor: F.L. //-*************************************************************************+#* /// <summary> /// Searches the device with soecified vendor and product id an connect to it. /// </summary> /// <returns></returns> private bool searchDevice() { //no device found yet bool deviceFound = false; this.deviceCount = 0; this.devicePath = string.Empty; myUSB.CT_HidGuid(); myUSB.CT_SetupDiGetClassDevs(); int result = -1; int resultb = -1; int device_count = 0; int size = 0; int requiredSize = 0; //search the device until you have found it or no more devices in list while (result != 0) { //open the device result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count); //get size of device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, 0); size = requiredSize; //get device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetailx(ref requiredSize, size); //is this the device i want? string deviceID = this.vendorID + "&" + this.productID; if (myUSB.DevicePathName.IndexOf(deviceID) > 0) { //yes it is //store device information this.deviceCount = device_count; this.devicePath = myUSB.DevicePathName; deviceFound = true; //init device myUSB.CT_SetupDiEnumDeviceInterfaces(this.deviceCount); size = 0; requiredSize = 0; resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size); resultb = 0; resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetailx(ref requiredSize, size); resultb = 0; //create HID Device Handel resultb = myUSB.CT_CreateFile(this.devicePath); //we have found our device so stop searching break; } device_count++; } //set connection state this.setConnectionState(deviceFound); //return state return(this.getConnectionState()); }
bool SearchDevice() { this.DevicePath = string.Empty; myUSB.CT_HidGuid(); // Get the GUID of the HID device Class myUSB.CT_SetupDiGetClassDevs(); // Get the device information set bool?result = null; bool resultb = false; int device_count = 0; int size = 0; int requiredSize = 0; // Reset the IsConnected to false this.IsConnected = false; //search the device until you have found it or no more devices in list while (!result.HasValue || result.Value == true) { // //if (result == false) // break; //open the device result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count); //get size of device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceBuffer(ref requiredSize, 0); size = requiredSize; //get device path resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size); if (resultb == false) { int err = Marshal.GetLastWin32Error(); } this.DevicePath = myUSB.DevicePathName; //is this the device i want? string deviceID = this.VID + "&" + this.PID; if (this.DevicePath.ToLower().IndexOf(deviceID.ToLower()) > 0) { //create HID Device Handel resultb = myUSB.CT_CreateFile(this.DevicePath); // Check the serial Number myUSB.CT_HidD_GetHIDSerialNumber(out string device_sn); if (this.SerialNumber == null || this.SerialNumber == string.Empty || device_sn == this.SerialNumber) { IntPtr myPtrToPreparsedData = default(IntPtr); if (myUSB.CT_HidD_GetPreparsedData(myUSB.hHidFile, ref myPtrToPreparsedData)) { bool code = myUSB.CT_HidP_GetCaps(myPtrToPreparsedData); int reportLength = myUSB.myHIDP_CAPS.InputReportByteLength; //we have found our device so stop searching this.IsConnected = true; this.SerialNumber = device_sn; } break; } else { myUSB.CT_CloseFile(); } } device_count++; } myUSB.CT_SetupDiDestroyDeviceInfoList(); //return state return(this.IsConnected); }