Beispiel #1
0
        string GetDevicePath(uint MemberIndex)
        {
            string sDevPath     = "";
            uint   RequiredSize = 0;
            Guid   USB_GUID     = new Guid("7B68107E-5CED-4A55-BFC3-7ECDBCA3ADF9");

            // dd 7B68107Eh
            // dw 5CEDh; Data2
            // dw 4A55h; Data3
            // db 0BFh, 0C3h, 7Eh, 0CDh, 0BCh, 0A3h, 0ADh, 0F9h; Data4

            // "7E10687B-ED5C-554A-BFC37ECDBCA3ADF9"

            var DeviceInfoSet = Win32.SetupDiGetClassDevs(ref USB_GUID, null, IntPtr.Zero, Win32.DIGCF_DEVICEINTERFACE | Win32.DIGCF_PRESENT);

            if (DeviceInfoSet == Win32.INVALID_HANDLE_VALUE)
            {
                return(sDevPath);
            }

            var DeviceInterfaceData = new Win32.SP_DEVINFO_DATA();

            DeviceInterfaceData.cbSize = Marshal.SizeOf(DeviceInterfaceData);
            if (IntPtr.Size == 8)              // 64-bit
            {
                DeviceInterfaceData.cbSize += 4;
            }

            if (Win32.SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, ref USB_GUID, MemberIndex, ref DeviceInterfaceData))
            {
                if (!Win32.SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref DeviceInterfaceData, IntPtr.Zero, 0, ref RequiredSize, IntPtr.Zero))
                {
                    var da = new Win32.SP_DEVINFO_DATA();
                    da.cbSize = Marshal.SizeOf(da);
                    if (IntPtr.Size == 8)                      // 64-bit
                    {
                        da.cbSize += 4;
                    }

                    var DeviceInterfaceDetailData = new Win32.SP_DEVICE_INTERFACE_DETAIL_DATA();
                    DeviceInterfaceDetailData.cbSize = ((IntPtr.Size == 4) ? (5) : (8));

                    if (Win32.SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref DeviceInterfaceData, ref DeviceInterfaceDetailData, RequiredSize, ref RequiredSize, ref da))
                    {
                        sDevPath = DeviceInterfaceDetailData.DevicePath;
                    }
                }
            }

            // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
            //
            Win32.SetupDiDestroyDeviceInfoList(DeviceInfoSet);

            return(sDevPath);
        }
Beispiel #2
0
        void ComPortOpen_()
        {
            bool   bRes      = false;
            string sPortName = "";

            _serialPort.DataReceived -= serialPort_DataReceived;

            try
            {
                // USB
                //
                // Gets a list of all HID devices currently connected to the computer (InfoSet)
                //
                IntPtr hInfoSet = Win32.SetupDiGetClassDevs(ref Win32.GUID_DEVINTERFACE_MODEM, null, IntPtr.Zero, Win32.DIGCF_DEVICEINTERFACE | Win32.DIGCF_PRESENT);
                if (hInfoSet != Win32.INVALID_HANDLE_VALUE)
                {
                    int nIndex = 0;

                    try
                    {
                        var devInfoData = new Win32.SP_DEVINFO_DATA();                          // build up a device interface data block
                        devInfoData.cbSize = Marshal.SizeOf(devInfoData);

                        // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
                        // to get device details for each device connected
                        while (Win32.SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref Win32.GUID_DEVINTERFACE_MODEM, (uint)nIndex, ref devInfoData))                                // this gets the device interface information for a device at index 'nIndex' in the memory block
                        {
                            uint nRequiredSize = 0;

                            // Get the device interface details
                            if (!Win32.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref devInfoData, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
                            {
                                // build a DevInfo Data structure
                                var da = new Win32.SP_DEVINFO_DATA();
                                da.cbSize = Marshal.SizeOf(da);

                                var oDetail = new Win32.SP_DEVICE_INTERFACE_DETAIL_DATA();
                                oDetail.cbSize = ((IntPtr.Size == 4)?(5):(8));

                                if (Win32.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref devInfoData, ref oDetail, nRequiredSize, ref nRequiredSize, ref da))
                                {
                                    if (oDetail.DevicePath.ToUpper().Contains(_usbDeviceID))                                            // do a string search, if we find the VID/PID string then we found our device!
                                    {
                                        IntPtr hKey = Win32.SetupDiOpenDevRegKey(hInfoSet, ref da, Win32.DICS_FLAG_GLOBAL, 0, Win32.DIREG_DEV, (int)Win32.RegSAM.AllAccess);
                                        if (hKey != Win32.INVALID_HANDLE_VALUE)
                                        {
                                            // читаем имя порта (COMx, LPTx)
                                            //
                                            uint          size = 256;
                                            uint          type;
                                            StringBuilder keyBuffer = new StringBuilder();
                                            if (Win32.RegQueryValueEx(hKey, "PortName", IntPtr.Zero, out type, keyBuffer, ref size) == 0)
                                            {
                                                sPortName = keyBuffer.ToString();

                                                bRes = true;

                                                break;
                                            }

                                            Win32.RegCloseKey(hKey);
                                        }
                                    }
                                }
                            }

                            nIndex++;                                   // if we get here, we didn't find our device. So move on to the next one.
                        }
                    }
                    finally
                    {
                        // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
                        //
                        Win32.SetupDiDestroyDeviceInfoList(hInfoSet);
                    }
                }

                if (bRes)
                {
                    try
                    {
                        _serialPort.Close();
                    }
                    catch {}

                    _serialPort.PortName = sPortName;
                    _serialPort.Encoding = Encoding.GetEncoding("iso-8859-1");
                    _serialPort.BaudRate = 115200;
                    _serialPort.Parity   = Parity.None;
                    _serialPort.StopBits = StopBits.One;

                    _serialPort.Open();

                    _serialPort.DataReceived  += serialPort_DataReceived;
                    _serialPort.ErrorReceived += _serialPort_ErrorReceived;

                    _sc.Post(delegate
                    {
                        USBInfoToggle(true);
                    },
                             null);
                }
                else
                {
                    /*
                     * throw new ApplicationException();
                     *
                     * _modeRs = MODE_RS.NO_REMOTE;
                     *
                     * MessageBox.Show(
                     *      "В системе отсутствует COM порт (" +
                     *              ( ( Settings.Default.Config_iIRinterface == 0 )?( "USB" ):( "RS232" ) ) + ") !\r\n\r\n" +
                     *                      "Программа будет работать без пульта ДУ.",
                     *              "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning );
                     */
                }
            }
            catch (ApplicationException)
            {
            }
            catch
            {
            }
        }