Example #1
0
    /// <summary>
    /// Search for all devices on the provided adapter and return
    /// a vector
    /// </summary>
    /// <param name="adapter"> valid 1-Wire adapter
    /// </param>
    /// <returns> Vector or OneWireContainers </returns>
    public static ArrayList findAllDevices(DSPortAdapter adapter)
    {
        ArrayList        owd_vect = new ArrayList(3);
        OneWireContainer owd;

        try
        {
            // clear any previous search restrictions
            adapter.setSearchAllDevices();
            adapter.targetAllFamilies();
            adapter.Speed = DSPortAdapter.SPEED_REGULAR;

            // enumerate through all the 1-Wire devices and collect them in a vector
            for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();)
            {
                owd = (OneWireContainer)owd_enum.Current;
                owd_vect.Add(owd);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        return(owd_vect);
    }
Example #2
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    ///  </param>
    public static void Main1(string[] args)
    {
        OneWireContainer owd;

        try
        {
            // get the default adapter
            DSPortAdapter adapter = OneWireAccessProvider.DefaultAdapter;

            Debug.WriteLine("");
            Debug.WriteLine("Adapter: " + adapter.AdapterName + " Port: " + adapter.PortName);
            Debug.WriteLine("");

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // clear any previous search restrictions
            adapter.setSearchAllDevices();
            adapter.targetAllFamilies();
            adapter.Speed = DSPortAdapter.SPEED_REGULAR;

            // enumerate through all the 1-Wire devices found
            for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();)
            {
                owd = (OneWireContainer)owd_enum.Current;

                Debug.WriteLine(owd.AddressAsString);
            }

            // end exclusive use of adapter
            adapter.endExclusive();

            // free port used by adapter
            adapter.freePort();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }

        return;
    }
        private static void PollSensors()
        {
            com.dalsemi.onewire.container.OneWireContainer owd = default(com.dalsemi.onewire.container.OneWireContainer);
            object state = null;

            com.dalsemi.onewire.container.TemperatureContainer tc       = default(com.dalsemi.onewire.container.TemperatureContainer);
            com.dalsemi.onewire.adapter.DSPortAdapter          oAdapter = null;
            bool bFound1w = false;

            for (int i = 0; i < 16; i++)
            {
                try
                {
                    //Logger.Log(Logger.LOGLEVEL_INFO, "Looking for 1 wire on USB" + i.ToString());

                    oAdapter = null;
                    try { oAdapter = com.dalsemi.onewire.OneWireAccessProvider.getAdapter("{DS9490}", "USB" + i.ToString()); }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                        //Logger.Log(Logger.LOGLEVEL_INFO, "USB" + i.ToString() + " " + msg);
                    }

                    if (oAdapter != null)
                    {
                        bFound1w = true;
                        Logger.Log(Logger.LOGLEVEL_INFO, "Found 1 wire on USB" + i.ToString());
                        //java.util.Enumeration owd_enum = default(java.util.Enumeration);

                        // get exclusive use of 1-Wire network
                        oAdapter.beginExclusive(true);

                        // clear any previous search restrictions
                        oAdapter.setSearchAllDevices();
                        oAdapter.targetAllFamilies();
                        oAdapter.setSpeed(com.dalsemi.onewire.adapter.DSPortAdapter.SPEED_REGULAR);
                        //                        oAdapter.setSpeed(com.dalsemi.onewire.adapter.DSPortAdapter.SPEED_HYPERDRIVE);

                        // Get all device containers
                        //oAdapter.getAllDeviceContainers();

                        var owd_enum = getEnumerator(oAdapter);
                        //Logger.Log(Logger.LOGLEVEL_INFO, "Enumerating devices connected to adapter " + oAdapter.getAdapterName());

                        // enumerate through all the 1-Wire devices found (with Java-style enumeration)
                        while (owd_enum.hasMoreElements())
                        {
                            try
                            {
                                // retrieve OneWireContainer
                                owd = (com.dalsemi.onewire.container.OneWireContainer)owd_enum.nextElement();
                                // check to see if 1-Wire device supports temperatures, if so get address and temp.
                                if (owd is com.dalsemi.onewire.container.TemperatureContainer)
                                {
                                    // cast the OneWireContainer to TemperatureContainer
                                    tc = (com.dalsemi.onewire.container.TemperatureContainer)owd;
                                    // read the device
                                    state = tc.readDevice();
                                    // extract the temperature from previous read
                                    tc.doTemperatureConvert((sbyte[])state);
                                    SetValue(owd.getAddressAsString(), (int)tc.getTemperature((sbyte[])state));
                                }
                                else
                                {
                                    Logger.Log(Logger.LOGLEVEL_WARNING, "Non-thermometer device found");
                                }
                            }
                            catch (Exception ex)
                            {
                                string sError = "USB" + i.ToString() + " ";
                                try
                                {
                                    sError += owd.getAddressAsString() + " ";
                                }
                                catch (Exception ex2)
                                {
                                    sError += "NO ID: " + ex2.Message + " ";
                                }
                                sError += ex.ToString();

                                Logger.Log(Logger.LOGLEVEL_ERROR, sError);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(Logger.LOGLEVEL_ERROR, "USB" + i.ToString() + " " + ex.ToString());
                }
                finally
                {
                    try
                    {
                        if (oAdapter != null)
                        {
                            // end exclusive use of 1-Wire net adapter
                            oAdapter.freePort();
                            oAdapter.endExclusive();
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            if (!bFound1w)
            {
                Logger.Log(Logger.LOGLEVEL_INFO, "No 1 wire USB found");
            }
        }