//Use this constructor to initialize a device public Colorimeter(SafeFileHandle handle, string pathName) { // Get handles to use in requesting Input and Output reports. readHandle = handle; colorimeterPathName = pathName; //get the Hid Handle from the pathName hidHandle = FileIO.CreateFile(colorimeterPathName, 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); // Learn the capabilities of the device. MyHid.Capabilities = MyHid.GetDeviceCapabilities(hidHandle); MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes); Hid.HidD_GetAttributes(hidHandle, ref MyHid.DeviceAttributes); // Initialize input report buffer inputBuffer = new byte[MyHid.Capabilities.InputReportByteLength]; newInputData = false; writeHandle = FileIO.CreateFile(colorimeterPathName, FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); // Flush any waiting reports in the input buffer. (optional) MyHid.FlushQueue(readHandle); }
private void Form1_Load(object sender, EventArgs e) { Guid hidGuid = Guid.Empty; Hid.HidD_GetHidGuid(ref hidGuid); IntPtr deviceNotificationHandle; deviceNotificationHandle = IntPtr.Zero; RegisterForDeviceNotifications("", this.Handle, hidGuid, ref deviceNotificationHandle); }
// If IsRange is false, UsageMin is the Usage and UsageMax is unused. // If IsStringRange is false, StringMin is the String index and StringMax is unused. // If IsDesignatorRange is false, DesignatorMin is the designator index and DesignatorMax is unused. public static string FindDevicePath(short productID, short vendorID, String[] devicePathNames) { try { if (devicePathNames.Length > 0) { foreach (var devicePathName in devicePathNames) { //iterate through all possible device paths to see if the hidhandle matches the ones for out devices var hidHandle = FileIO.CreateFile(devicePathName, 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0); if (!hidHandle.IsInvalid) { //we need to set this in another location, as it has nothing to do with this method //moving this to Colorimeter constructor //MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes); var hidDeviceAttributes = new Hid.HIDD_ATTRIBUTES(); if (Hid.HidD_GetAttributes(hidHandle, ref hidDeviceAttributes)) { // Find out if the device matches the one we're looking for. if ((hidDeviceAttributes.VendorID == vendorID) && (hidDeviceAttributes.ProductID == productID)) { return(devicePathName); } else { hidHandle.Close(); } } else { hidHandle.Close(); } } } } } catch (Exception ex) { } return(null); }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_DEVICECHANGE) { if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL)) { //// Look for a colorimeter if one isn't already attached if (colorimeter == null) { Guid colorimeterGuid = Guid.Empty; Hid.HidD_GetHidGuid(ref colorimeterGuid); String[] devicePathNames = MyDeviceManagement.FindDevicePathsFromGuid(colorimeterGuid, NUMBER_OF_DEVICE_PATHNAMES); var devicePath = Hid.FindDevicePath(Colorimeter.COLORIMETER_PRODUCT_ID, Colorimeter.COLORIMETER_VENDOR_ID, devicePathNames); var readHandle = FileIO.CreateFile(devicePath, FileIO.GENERIC_READ, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, FileIO.FILE_FLAG_OVERLAPPED, 0); //we have found a viable colorimeter with a working path and handle if (!devicePath.Equals(null) && !readHandle.IsInvalid) { //colorimeterDetected = true; colorimeter = new Colorimeter(readHandle, devicePath); // Start checkColorimeterDeviceStateBackgroundWorker // When the Background Worker completes, a different backgroundworker // is called to get the firmware and test file versions var request = new ColorimeterRequest() { ColorimeterRequestActionType = ColorimeterActionType.FirmwareVersion | ColorimeterActionType.TestFileVersion | ColorimeterActionType.DeviceState }; if (!colorimeterConnectedBackgroundWorker.IsBusy) { colorimeterConnectedBackgroundWorker.RunWorkerAsync(request); } } } //// Find out if it's the device we're communicating with. if (MyDeviceManagement.DeviceNameMatch(m, colorimeter.colorimeterPathName)) { listBox1.Items.Add("Colorimeter attached."); } } // If WParam contains DBT_DEVICEREMOVAL, a device has been removed. We only care if the removed device is a colorimeter we are already talking to else if ((m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE) && colorimeter != null) { if (MyDeviceManagement.DeviceNameMatch(m, colorimeter.colorimeterPathName)) { listBox1.Items.Add("Colorimeter removed."); //colorimeterDetected = false; colorimeter = null; } } listBox1.SelectedIndex = listBox1.Items.Count - 1; } }