Exemplo n.º 1
0
    /// <summary>
    /// Display information about the Switch device
    /// </summary>
    /// <param name="owd"> OneWireContainer device </param>
    internal static void printSwitchInfo(SwitchContainer swd)
    {
        try
        {
            byte[] state = swd.readDevice();

            Debug.WriteLine("");
            Debug.WriteLine("-----------------------------------------------------------------------");
            Debug.WriteLine("| Number of channels: " + swd.getNumberChannels(state));
            Debug.WriteLine("| Is high-side switch: " + swd.HighSideSwitch);
            Debug.WriteLine("| Has Activity Sensing: " + swd.hasActivitySensing());
            Debug.WriteLine("| Has Level Sensing: " + swd.hasLevelSensing());
            Debug.WriteLine("| Has Smart-on: " + swd.hasSmartOn());
            Debug.WriteLine("| Only 1 channel on at a time: " + swd.onlySingleChannelOn());
            Debug.WriteLine("");

            Debug.Write("    Channel          ");
            for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
            {
                Debug.Write(ch + "      ");
            }
            Debug.WriteLine("");

            Debug.WriteLine("    -----------------------------------");

            Debug.Write("    Latch State      ");
            for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
            {
                Debug.Write(((swd.getLatchState(ch, state) == true) ? "ON     " : "OFF    "));
            }
            Debug.WriteLine("");

            if (swd.hasLevelSensing())
            {
                Debug.Write("    Sensed Level     ");
                for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
                {
                    Debug.Write(((swd.getLevel(ch, state) == true) ? "HIGH   " : "LOW    "));
                }
                Debug.WriteLine("");
            }

            if (swd.hasActivitySensing())
            {
                Debug.Write("    Sensed Activity  ");
                for (int ch = 0; ch < swd.getNumberChannels(state); ch++)
                {
                    Debug.Write(((swd.getSensedActivity(ch, state) == true) ? "SET    " : "CLEAR  "));
                }
                Debug.WriteLine("");
            }
        }
        catch (OneWireIOException e)
        {
            Debug.WriteLine(e);
        }
    }
Exemplo n.º 2
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            isSwitchContainer = false;
            SwitchContainer sc = null;

            try
            {
                sc = (SwitchContainer)owc;
                isSwitchContainer = true;
            }
            catch (InvalidCastException)
            {
                sc = null;
                isSwitchContainer = false;         //just to reiterate
            }

            if (isSwitchContainer)
            {
                Debug.WriteLine("= This device is a " + owc.Name);
                Debug.WriteLine("= Also known as a " + owc.AlternateNames);
                Debug.WriteLine("=");
                Debug.WriteLine("= It is a Switch Container");
                if (sc.hasActivitySensing())
                {
                    sc.clearActivity();
                }

                byte[] state    = sc.readDevice();
                int    channels = sc.getNumberChannels(state);
                bool   activity = sc.hasActivitySensing();
                bool   level    = sc.hasLevelSensing();
                bool   smart    = sc.hasSmartOn();

                Debug.WriteLine("= This device has " + channels + " channel" + (channels > 1 ? "s" : ""));
                Debug.WriteLine("= It " + (activity ? "has" : "does not have") + " activity sensing abilities");
                Debug.WriteLine("= It " + (level ? "has" : "does not have") + " level sensing abilities");
                Debug.WriteLine("= It " + (smart ? "is" : "is not") + " smart-on capable");

                for (int ch = 0; ch < channels; ch++)
                {
                    Debug.WriteLine("======================");
                    Debug.WriteLine("=   Channel " + ch + "        =");
                    Debug.WriteLine("=--------------------=");

                    bool latchstate = sc.getLatchState(ch, state);

                    Debug.WriteLine("= State " + (latchstate ? "ON " : "OFF") + "          =");

                    if (level)
                    {
                        bool sensedLevel = sc.getLevel(ch, state);

                        Debug.WriteLine("= Level " + (sensedLevel ? "HIGH" : "LOW ") + "         =");
                    }

                    if (activity)
                    {
                        bool sensedActivity = sc.getSensedActivity(ch, state);

                        Debug.WriteLine("= Activity " + (sensedActivity ? "YES" : "NO ") + "       =");
                    }

                    Debug.WriteLine("= Toggling switch... =");

                    try
                    {
                        Thread.Sleep(500);
                    }
                    catch (Exception)
                    {
                        /*drain it*/
                    }

                    sc.setLatchState(ch, !latchstate, smart, state);
                    sc.writeDevice(state);

                    state = sc.readDevice();

                    if (latchstate == sc.getLatchState(ch, state))
                    {
                        Debug.WriteLine("= Toggle Failed      =");
                    }
                    else
                    {
                        try
                        {
                            Thread.Sleep(500);
                        }
                        catch (Exception)
                        {
                            /*drain it*/
                        }

                        Debug.WriteLine("= Toggling back...   =");
                        sc.setLatchState(ch, latchstate, smart, state);
                        sc.writeDevice(state);
                    }
                }
            }
            else
            {
                Debug.WriteLine("= This device is not a Switch device.");
                Debug.WriteLine("=");
                Debug.WriteLine("=");
            }

            next = access.findNextDevice();
        }
    }