Ejemplo n.º 1
0
        public override bool start(uint index)
        {
            Monitor.Enter(mLock);
            try
            {
                uint i        = 0;
                uint devCount = SiUSBController.count();
                for (uint j = 0; j < devCount; j++)
                {
                    var vidSB = new StringBuilder();
                    var pidSB = new StringBuilder();
                    if (SiUSBController.getVID(j, vidSB) > 0 && SiUSBController.getPID(j, pidSB) > 0)
                    {
                        string vidString = vidSB.ToString();
                        string pidString = pidSB.ToString();

                        var vidHex = Util.getHexBytes(vidString);
                        var pidHex = Util.getHexBytes(pidString);
                        Array.Reverse(vidHex);
                        Array.Reverse(pidHex);

                        ushort vID = BitConverter.ToUInt16(vidHex, 0);
                        ushort pID = BitConverter.ToUInt16(pidHex, 0);

                        if (vID == (ushort)VendorID && pID == (ushort)ProductID)
                        {
                            if (i == index)
                            {
                                if (SiUSBController.open(j, ref mDeviceHandle) == false)
                                {
                                    Console.WriteLine("SiUSBController.start() : Failed open (SiUSB index : {0})", j);
                                    Monitor.Exit(mLock);
                                    this.stop();
                                    return(false);
                                }

                                Console.WriteLine("SiUSBController.start() : Success open (SiUSB index : {0})", j);

                                mThreadState = true;
                                mThread      = new Thread(threadFunc);
                                mThread.Start();
                                Monitor.Exit(mLock);
                                return(true);
                            }
                            i++;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("SiUSBController.start() : Failed catch({0})", e.Message);
            }
            Monitor.Exit(mLock);
            this.stop();
            return(false);
        }
Ejemplo n.º 2
0
        public static uint getDeviceCount(USBVendorID vendorID, USBProductID productID)
        {
            uint count = 0;

            try
            {
                uint devCount = SiUSBController.count();
                for (uint i = 0; i < devCount; i++)
                {
                    var vidSB = new StringBuilder();
                    var pidSB = new StringBuilder();
                    if (SiUSBController.getVID(i, vidSB) > 0 && SiUSBController.getPID(i, pidSB) > 0)
                    {
                        string vidString = vidSB.ToString();
                        string pidString = pidSB.ToString();

                        Console.WriteLine("SiUSBController.getDeviceCount() : SiUSB index({0}), VendorID(0x{1:X4}), ProductID(0x{2:X4})", i, vidString, pidString);

                        var vidHex = Util.getHexBytes(vidString);
                        var pidHex = Util.getHexBytes(pidString);
                        Array.Reverse(vidHex);
                        Array.Reverse(pidHex);

                        ushort vID = BitConverter.ToUInt16(vidHex, 0);
                        ushort pID = BitConverter.ToUInt16(pidHex, 0);

                        if (vID == (ushort)vendorID && pID == (ushort)productID)
                        {
                            count++;
                        }
                    }
                }
            }
            catch { }
            return(count);
        }
Ejemplo n.º 3
0
        public override bool start()
        {
            Monitor.Enter(mLock);
            try
            {
                uint count = SiUSBController.count();
                if (count == 0)
                {
                    Console.WriteLine("SiUSBController.start() : USB device is zero");
                    Monitor.Exit(mLock);
                    this.stop();
                    return(false);
                }

                Console.WriteLine("SiUSBController.start() : USB device count({0})", count);

                bool isDeviceOpen = false;
                for (uint i = 0; i < count; i++)
                {
                    var vidSB = new StringBuilder();
                    var pidSB = new StringBuilder();
                    if (SiUSBController.getVID(i, vidSB) > 0 && SiUSBController.getPID(i, pidSB) > 0)
                    {
                        string vidString = vidSB.ToString();
                        string pidString = pidSB.ToString();

                        Console.WriteLine("SiUSBController.start() : Device index({0}), VendorID(0x{1:X4}), ProductID(0x{2:X4})", i, vidString, pidString);

                        var vidHex = Util.getHexBytes(vidString);
                        var pidHex = Util.getHexBytes(pidString);
                        Array.Reverse(vidHex);
                        Array.Reverse(pidHex);

                        ushort vID = BitConverter.ToUInt16(vidHex, 0);
                        ushort pID = BitConverter.ToUInt16(pidHex, 0);

                        if (vID == (ushort)VendorID && pID == (ushort)ProductID)
                        {
                            if (SiUSBController.open(i, ref mDeviceHandle) == true)
                            {
                                Console.WriteLine("SiUSBController.start() : Success open({0})", i);
                                isDeviceOpen = true;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("SiUSBController.start() : Failed open({0})", i);
                            }
                        }
                    }
                }

                if (isDeviceOpen == false)
                {
                    Console.WriteLine("SiUSBController.start() : Failed device open");
                    Monitor.Exit(mLock);
                    this.stop();
                    return(false);
                }

                mThreadState = true;
                mThread      = new Thread(threadFunc);
                mThread.Start();
                Monitor.Exit(mLock);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("SiUSBController.start() : Failed catch({0})", e.Message);
                Monitor.Exit(mLock);
                this.stop();
                return(false);
            }
        }