public bool do_connect(List <string> msgs) { if (m_DeviceDetected) { msgs.Add("USB device: was already open"); return(m_DeviceDetected); } msgs.Add("USB device: connecting..."); Boolean deviceFound = false; Guid hidGuid = Guid.Empty; String[] devicePathName = new String[128]; Int32 myVendorID = 0x16c0; Int32 myProductID = 0x05df; Int32 memberIndex = 0; Boolean success = false; Hid.HidD_GetHidGuid(ref hidGuid); deviceFound = m_DeviceManagement.FindDeviceFromGuid(hidGuid, ref devicePathName); if (deviceFound) { memberIndex = 0; do { m_hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); if (!m_hidHandle.IsInvalid) { m_Hid.DeviceAttributes.Size = Marshal.SizeOf(m_Hid.DeviceAttributes); success = Hid.HidD_GetAttributes(m_hidHandle, ref m_Hid.DeviceAttributes); if (success) { if ((m_Hid.DeviceAttributes.VendorID == myVendorID) && (m_Hid.DeviceAttributes.ProductID == myProductID)) { m_DeviceDetected = true; // Save the DevicePathName for OnDeviceChange(). m_DevicePathName = devicePathName[memberIndex]; } else { // It's not a match, so close the handle. m_DeviceDetected = false; m_hidHandle.Close(); } } else { m_DeviceDetected = false; m_hidHandle.Close(); } } memberIndex = memberIndex + 1; }while (!((m_DeviceDetected || (memberIndex == devicePathName.Length)))); if (m_DeviceDetected) { msgs.Add("USB device: found the device on the bus"); m_Hid.Capabilities = m_Hid.GetDeviceCapabilities(m_hidHandle); m_readHandle = FileIO.CreateFile(m_DevicePathName, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0); m_writeHandle = FileIO.CreateFile(m_DevicePathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); m_Hid.FlushQueue(m_readHandle); Hid.InFeatureReport myInFeatureReport = new Hid.InFeatureReport(); Hid.OutFeatureReport myOutFeatureReport = new Hid.OutFeatureReport(); newOutBuffer(); newInBuffer(); } else { msgs.Add("USB device: cannot find the device on the bus"); } } return(m_DeviceDetected); }
public static bool FindNia(IntPtr Handle) { bool success = false; try { // Get the guid for the system hid class Guid hidGuid = Guid.Empty; GenericHid.Hid.HidD_GetHidGuid(ref hidGuid); // Find all devices of type hid string[] deviceCollection = new String[128]; DeviceManagement deviceManagement = new DeviceManagement(); bool devicesFound = deviceManagement.FindDeviceFromGuid(hidGuid, ref deviceCollection); // Did we find any hid devices ? if (devicesFound) { int memberIndex = 0; do { // try to get a handle on the current hid device in the list hidHandle = GenericHid.FileIO.CreateFile(deviceCollection[memberIndex], 0, GenericHid.FileIO.FILE_SHARE_READ | GenericHid.FileIO.FILE_SHARE_WRITE, null, GenericHid.FileIO.OPEN_EXISTING, 0, 0); if (!hidHandle.IsInvalid) { // Set the Size property of DeviceAttributes to the number of bytes in the structure. MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes); // try to get the hid's information success = GenericHid.Hid.HidD_GetAttributes(hidHandle, ref MyHid.DeviceAttributes); if (success) { if ((MyHid.DeviceAttributes.VendorID == 4660) & (MyHid.DeviceAttributes.ProductID == 0)) { NiaDetected = true; // Save the DevicePathName for OnDeviceChange(). NiaPathName = deviceCollection[memberIndex]; } else { NiaDetected = false; hidHandle.Close(); } } else { NiaDetected = false; hidHandle.Close(); } } // Keep looking until we find the device or there are no devices left to examine. memberIndex = memberIndex + 1; } while (!((NiaDetected | (memberIndex == deviceCollection.Length)))); // Did we find a NIA ? if (NiaDetected) { // The device was detected. // Register to receive notifications if the device is removed or attached. success = deviceManagement.RegisterForDeviceNotifications(NiaPathName, Handle, hidGuid, ref deviceNotificationHandle); if (success) { // Get handles to use in requesting Input and Output reports. readHandle = GenericHid.FileIO.CreateFile(NiaPathName, GenericHid.FileIO.GENERIC_READ, GenericHid.FileIO.FILE_SHARE_READ | GenericHid.FileIO.FILE_SHARE_WRITE, null, GenericHid.FileIO.OPEN_EXISTING, GenericHid.FileIO.FILE_FLAG_OVERLAPPED, 0); if (!readHandle.IsInvalid) { writeHandle = GenericHid.FileIO.CreateFile(NiaPathName, GenericHid.FileIO.GENERIC_WRITE, GenericHid.FileIO.FILE_SHARE_READ | GenericHid.FileIO.FILE_SHARE_WRITE, null, GenericHid.FileIO.OPEN_EXISTING, 0, 0); MyHid.FlushQueue(readHandle); } } } } return(NiaDetected); } catch (Exception ex) { throw ex; } }