Exemple #1
0
        public static int[] FindEV3s()
        {
            Guid guid;
            int  index = 0;

            List <int> devices = new List <int>();


            HidImports.HidD_GetHidGuid(out guid);

            IntPtr hDevInfo = HidImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HidImports.DIGCF_DEVICEINTERFACE | HidImports.DIGCF_PRESENT);

            HidImports.SP_DEVICE_INTERFACE_DATA diData = new HidImports.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            while (HidImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                UInt32 size;

                HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA();

                diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);

                if (HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    HidImports.HIDD_ATTRIBUTES attrib = new HidImports.HIDD_ATTRIBUTES();
                    attrib.Size = Marshal.SizeOf(attrib);

                    SafeFileHandle handle = HidImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.None /*.ReadWrite*/, IntPtr.Zero, FileMode.Open, 0 /*HidImports.EFileAttributes.Overlapped*/, IntPtr.Zero);

                    if (HidImports.HidD_GetAttributes(handle.DangerousGetHandle(), ref attrib))
                    {
                        if (attrib.VendorID == VID && attrib.ProductID == PID)
                        {
                            devices.Add(index);
                        }
                    }
                    handle.Close();
                }
                else
                {
                    throw new Exception("SetupDiGetDeviceInterfaceDetail failed on index " + index);
                }

                index++;
            }

            HidImports.SetupDiDestroyDeviceInfoList(hDevInfo);


            return(devices.ToArray());
        }
Exemple #2
0
        public EV3ConnectionUSB(int index)
            : base()
        {
            Guid guid;

            HidImports.HidD_GetHidGuid(out guid);

            IntPtr hDevInfo = HidImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, HidImports.DIGCF_DEVICEINTERFACE | HidImports.DIGCF_PRESENT);

            HidImports.SP_DEVICE_INTERFACE_DATA diData = new HidImports.SP_DEVICE_INTERFACE_DATA();
            diData.cbSize = Marshal.SizeOf(diData);

            if (HidImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, index, ref diData))
            {
                UInt32 size;

                HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, IntPtr.Zero, 0, out size, IntPtr.Zero);

                HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA diDetail = new HidImports.SP_DEVICE_INTERFACE_DETAIL_DATA();

                diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5);

                if (HidImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref diData, ref diDetail, size, out size, IntPtr.Zero))
                {
                    _handle = HidImports.CreateFile(diDetail.DevicePath, FileAccess.ReadWrite, FileShare.None /*.ReadWrite*/, IntPtr.Zero, FileMode.Open, 0 /*HidImports.EFileAttributes.Overlapped*/, IntPtr.Zero);
                    HidImports.HIDD_ATTRIBUTES attrib = new HidImports.HIDD_ATTRIBUTES();
                    attrib.Size = Marshal.SizeOf(attrib);

                    if (HidImports.HidD_GetAttributes(_handle.DangerousGetHandle(), ref attrib))
                    {
                        if (attrib.VendorID == VID && attrib.ProductID == PID)
                        {
                            IntPtr preparsedData;
                            if (!HidImports.HidD_GetPreparsedData(_handle.DangerousGetHandle(), out preparsedData))
                            {
                                throw new Exception("Could not get preparsed data for HID device");
                            }

                            HidImports.HIDP_CAPS caps;
                            if (HidImports.HidP_GetCaps(preparsedData, out caps) != HidImports.HIDP_STATUS_SUCCESS)
                            {
                                throw new Exception("Could not get CAPS for HID device");
                            }

                            HidImports.HidD_FreePreparsedData(ref preparsedData);

                            _inputReport  = new byte[caps.InputReportByteLength];
                            _outputReport = new byte[caps.OutputReportByteLength];

                            _stream = new FileStream(_handle, FileAccess.ReadWrite, _inputReport.Length);

                            HidImports.SetupDiDestroyDeviceInfoList(hDevInfo);
                            return;
                        }
                    }
                    _handle.Close();
                }
                else
                {
                    throw new Exception("SetupDiGetDeviceInterfaceDetail failed on index " + index);
                }
            }

            HidImports.SetupDiDestroyDeviceInfoList(hDevInfo);

            throw new Exception("No LEGO EV3s found in HID device list.");
        }