Ejemplo n.º 1
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    /// </param>
    /// <exception cref="OneWireException"> </exception>
    /// <exception cref="OneWireIOException">
    ///  </exception>
    public static void Main1(string[] args)
    {
        bool          usedefault   = false;
        DSPortAdapter access       = null;
        string        adapter_name = null;
        string        port_name    = null;

        if ((args == null) || (args.Length < 1))
        {
            try
            {
                access = OneWireAccessProvider.DefaultAdapter;

                if (access == null)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Debug.WriteLine("Couldn't get default adapter!");
                printUsageString();

                return;
            }

            usedefault = true;
        }

        if (!usedefault)
        {
            string[] st = args[0].Split(new char[] { '_' });

            if (st.Length != 2)
            {
                printUsageString();

                return;
            }

            adapter_name = st[0];
            port_name    = st[1];

            Debug.WriteLine("Adapter Name: " + adapter_name);
            Debug.WriteLine("Port Name: " + port_name);
        }

        if (access == null)
        {
            try
            {
                access = OneWireAccessProvider.getAdapter(adapter_name, port_name);
            }
            catch (Exception)
            {
                Debug.WriteLine("That is not a valid adapter/port combination.");

                System.Collections.IEnumerator en = OneWireAccessProvider.enumerateAllAdapters();

                while (en.MoveNext())
                {
                    DSPortAdapter temp = (DSPortAdapter)en.Current;

                    Debug.WriteLine("Adapter: " + temp.AdapterName);

                    System.Collections.IEnumerator f = temp.PortNames;

                    while (f.MoveNext())
                    {
                        Debug.WriteLine("   Port name : " + ((string)f.Current));
                    }
                }

                return;
            }
        }

        access.adapterDetected();
        access.targetAllFamilies();
        access.beginExclusive(true);
        access.reset();
        access.setSearchAllDevices();

        bool next = access.findFirstDevice();

        if (!next)
        {
            Debug.WriteLine("Could not find any iButtons!");

            return;
        }

        while (next)
        {
            OneWireContainer owc = access.DeviceContainer;

            Debug.WriteLine("====================================================");
            Debug.WriteLine("= Found One Wire Device: " + owc.AddressAsString + "          =");
            Debug.WriteLine("====================================================");
            Debug.WriteLine("=");

            bool isPotContainer       = false;
            PotentiometerContainer pc = null;

            try
            {
                pc             = (PotentiometerContainer)owc;
                isPotContainer = true;
            }
            catch (InvalidCastException)
            {
                pc             = null;
                isPotContainer = false;         //just to reiterate
            }

            if (isPotContainer)
            {
                Debug.WriteLine("= This device is a " + owc.Name);
                Debug.WriteLine("= Also known as a " + owc.AlternateNames);
                Debug.WriteLine("=");
                Debug.WriteLine("= It is a Potentiometer Container");

                byte[] state       = pc.readDevice();
                bool   charge      = pc.isChargePumpOn(state);
                int    wiper       = pc.getCurrentWiperNumber(state);
                int    position    = pc.WiperPosition;
                bool   linear      = pc.isLinear(state);
                int    numPots     = pc.numberOfPotentiometers(state);
                int    numSettings = pc.numberOfWiperSettings(state);
                int    resistance  = pc.potentiometerResistance(state);

                Debug.WriteLine("=");
                Debug.WriteLine("= Charge pump is " + (charge ? "ON" : "OFF"));
                Debug.WriteLine("= This device has " + numPots + " potentiometer" + ((numPots > 1) ? "s" : ""));
                Debug.WriteLine("= This device has a " + (linear ? "linear" : "logarithmic") + " potentiometer");
                Debug.WriteLine("= It uses " + numSettings + " potentiometer wiper settings");
                Debug.WriteLine("= The potentiometer resistance is " + resistance + " kOhms");
                Debug.WriteLine("= CURRENT WIPER NUMBER  : " + wiper);
                Debug.WriteLine("= CURRENT WIPER POSITION: " + position);
                Debug.WriteLine("= Trying to toggle the charge pump...");
                pc.setChargePump(!charge, state);
                pc.writeDevice(state);

                state = pc.readDevice();

                if (charge == pc.isChargePumpOn(state))
                {
                    Debug.WriteLine("= Could not toggle charge pump.  Must have external power supplied.");
                }
                else
                {
                    Debug.WriteLine("= Toggled charge pump successfully");
                }

                //int newwiper = (int)((DateTime.Now.Ticks & 0x0ff00) >> 8);
                //pc.WiperPosition = newwiper;
                //Debug.WriteLine("= Setting wiper position to " + (newwiper));
                Debug.WriteLine("= CURRENT WIPER POSITION: " + pc.WiperPosition);
            }
            else
            {
                Debug.WriteLine("= This device is not a potentiometer device.");
                Debug.WriteLine("=");
                Debug.WriteLine("=");
            }

            next = access.findNextDevice();
        }
    }