コード例 #1
0
 /// <summary>Callback function from the PI Engineering DLL, raised each time the device pushes a data packet</summary>
 /// <param name="data">The callback data</param>
 /// <param name="sourceDevice">The source device</param>
 /// <param name="error">The last error generated (if any)</param>
 public void HandlePIEHidData(byte[] data, PIEHid32Net.PIEDevice sourceDevice, int error)
 {
     if (myDevice == sourceDevice)
     {
         for (int r = 0; r < sourceDevice.ReadLength; r++)
         {
             currentState[r] = data[r];
         }
     }
 }
コード例 #2
0
 /// <summary>Callback function from the PI Engineering DLL, raised if an error is encountered</summary>
 /// <param name="sourceDevices">The source device</param>
 /// <param name="error">The error</param>
 public void HandlePIEHidError(PIEHid32Net.PIEDevice sourceDevices, int error)
 {
 }
コード例 #3
0
        /// <summary>
        /// Enumerates all valid USB devics of the specified Vid.
        /// </summary>
        /// <returns>list of all devices found, ordered by USB port connection</returns>
        public static PIEDevice[] EnumeratePIE(Int32 vid)
        {
            LinkedList <PIEDevice> devices = new LinkedList <PIEDevice>();

            // Get all device paths
            Guid guid = Guid.Empty;

            HidApiDeclarations.HidD_GetHidGuid(ref guid);
            IntPtr deviceInfoSet = DeviceManagementApiDeclarations.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero,
                                                                                       DeviceManagementApiDeclarations.DIGCF_PRESENT
                                                                                       | DeviceManagementApiDeclarations.DIGCF_DEVICEINTERFACE);

            DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new DeviceManagementApiDeclarations.SP_DEVICE_INTERFACE_DATA();
            deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); //28;

            LinkedList <String> paths = new LinkedList <String>();

            for (int i = 0; 0 != DeviceManagementApiDeclarations.SetupDiEnumDeviceInterfaces(
                     deviceInfoSet, 0, ref guid, i, ref deviceInterfaceData); i++)
            {
                int buffSize = 0;
                DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                ref deviceInterfaceData, IntPtr.Zero, 0, ref buffSize, IntPtr.Zero);
                // Use IntPtr to simulate detail data structure
                IntPtr detailBuffer = Marshal.AllocHGlobal(buffSize);
                // Simulate setting cbSize to 4 bytes + one character (seems to be what everyone has always done, even though it makes no sense)
                //onur modified for 64-bit compatibility - March 2009
                if (IntPtr.Size == 8) //64-bit
                {
                    Marshal.WriteInt32(detailBuffer, Marshal.SizeOf(typeof(IntPtr)));
                }
                else //32-bit
                {
                    Marshal.WriteInt32(detailBuffer, 4 + Marshal.SystemDefaultCharSize);
                }

                if (DeviceManagementApiDeclarations.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                                                                                    ref deviceInterfaceData, detailBuffer, buffSize, ref buffSize, IntPtr.Zero))
                {
                    // convert buffer (starting past the cbsize variable) to string path
                    paths.AddLast(Marshal.PtrToStringAuto(new IntPtr(detailBuffer.ToInt32() + 4)));
                }
            }
            DeviceManagementApiDeclarations.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            //Security attributes not used anymore - not necessary Onur March 2009
            // Open each device file and test for vid
            FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttributes = new FileIOApiDeclarations.SECURITY_ATTRIBUTES();
            securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
            securityAttributes.bInheritHandle       = System.Convert.ToInt32(true);
            securityAttributes.nLength = Marshal.SizeOf(securityAttributes);

            for (LinkedList <String> .Enumerator en = paths.GetEnumerator(); en.MoveNext();)
            {
                String path = en.Current;

                SafeFileHandle fileHandle = FileIOApiDeclarations.CreateFile(path, FileIOApiDeclarations.GENERIC_WRITE,
                                                                             FileIOApiDeclarations.FILE_SHARE_READ | FileIOApiDeclarations.FILE_SHARE_WRITE,
                                                                             IntPtr.Zero, FileIOApiDeclarations.OPEN_EXISTING, 0, 0);
                if (fileHandle.IsInvalid)
                {
                    // Bad handle, try next path
                    continue;
                }
                try
                {
                    HidApiDeclarations.HIDD_ATTRIBUTES hidAttributes = new HidApiDeclarations.HIDD_ATTRIBUTES();
                    hidAttributes.Size = Marshal.SizeOf(hidAttributes);
                    if (0 != HidApiDeclarations.HidD_GetAttributes(fileHandle, ref hidAttributes) &&
                        hidAttributes.VendorID == vid)
                    {
                        // Good attributes and right vid, try to get caps
                        IntPtr pPerparsedData = new IntPtr();
                        if (HidApiDeclarations.HidD_GetPreparsedData(fileHandle, ref pPerparsedData))
                        {
                            HidApiDeclarations.HIDP_CAPS hidCaps = new HidApiDeclarations.HIDP_CAPS();
                            if (0 != HidApiDeclarations.HidP_GetCaps(pPerparsedData, ref hidCaps))
                            {
                                // Got Capabilities, add device to list
                                byte[] Mstring = new byte[128];
                                String ssss    = "";;
                                if (0 != HidApiDeclarations.HidD_GetManufacturerString(fileHandle, ref Mstring[0], 128))
                                {
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Mstring[2 * i];
                                        t[1] = Mstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        ssss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                }
                                byte[] Pstring = new byte[128];
                                String psss    = "";
                                if (0 != HidApiDeclarations.HidD_GetProductString(fileHandle, ref Pstring[0], 128))
                                {
                                    // Pstring[0] = 0xa0;  Test unicode
                                    // Pstring[1] = 0x03;
                                    for (int i = 0; i < 64; i++)
                                    {
                                        byte[] t = new byte[2];
                                        t[0] = Pstring[2 * i];
                                        t[1] = Pstring[2 * i + 1];
                                        if (t[0] == 0)
                                        {
                                            break;
                                        }
                                        psss += System.Text.Encoding.Unicode.GetString(t);
                                    }
                                    // psss=(System.Text.Encoding.Unicode.GetString(Pstring)).Trim(new char[]{' '});
                                }

                                devices.AddLast(new PIEDevice(path, hidAttributes.VendorID, hidAttributes.ProductID, hidAttributes.VersionNumber,
                                                              hidCaps.Usage, hidCaps.UsagePage, hidCaps.InputReportByteLength, hidCaps.OutputReportByteLength,
                                                              ssss, psss));
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    continue;
                }
                finally
                {
                    fileHandle.Close();
                }
            }
            PIEDevice[] ret = new PIEDevice[devices.Count];
            devices.CopyTo(ret, 0);
            return(ret);
        }