Beispiel #1
0
        private void PeriodicTimerCallback(ThreadPoolTimer timer)
        {
            if (_cancelRequested == false)
            {
                // get exclusive use of port
                _adapter.beginExclusive(true);

                // open a path to the temp device
                _path.open();

                // check if present
                if (_owc.Present)
                {
                    // read the temp device
                    byte[] state = _tc.readDevice();

                    _tc.doTemperatureConvert(state);

                    state = _tc.readDevice();

                    Debug.WriteLine("Temperature of " + _address + " is " + _tc.getTemperature(state) + " C");
                }
                else
                {
                    Debug.WriteLine("Device " + _address + " not present so stopping thread");
                }

                // close the path to the device
                _path.close();

                // release exclusive use of port
                _adapter.endExclusive();
            }
            else
            {
                _periodicTimer.Cancel();

                var settings = ApplicationData.Current.LocalSettings;
                var key      = _taskInstance.Task.Name;

                //
                // Write to LocalSettings to indicate that this background task ran.
                //
                settings.Values[key] = "Canceled with reason: " + _cancelReason.ToString();
                Debug.WriteLine("Background " + _taskInstance.Task.Name + settings.Values[key]);

                //
                // Indicate that the background task has completed.
                //
                _deferral.Complete();
            }
        }
Beispiel #2
0
        /// <summary>
        /// The readSensor method returns a temperature in degrees Celsius
        ///
        /// @param--none.
        /// </summary>
        /// <returns> String temperature in degrees Celsius </returns>
        public virtual string readSensor()
        {
            string returnString = "";
            double theTemperature;
            TemperatureContainer tc = DeviceContainer as TemperatureContainer;

            // read the device first before getting the temperature
            byte[] state = tc.readDevice();

            // perform a temperature conversion
            tc.doTemperatureConvert(state);

            // read the result of the conversion
            state = tc.readDevice();

            // extract the result out of state
            theTemperature = tc.getTemperature(state);
            //theTemperature = (double)(Math.round(theTemperature * 100))/100; // avoid Math for TINI?
            theTemperature = roundDouble(theTemperature * 100) / 100;
            // make string out of results
            returnString = theTemperature + " �C";

            return(returnString);
        }
Beispiel #3
0
        /// <summary>
        /// Method main
        ///
        /// </summary>
        /// <param name="args">
        /// </param>
        /// <exception cref="OneWireException"> </exception>
        /// <exception cref="OneWireIOException">
        ///  </exception>

        public static void Main1([ReadOnlyArray()] 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)
            {
                //parse device instance

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

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

                    return;
                }

                adapter_name = args[0];
                port_name    = args[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 : " + ((DeviceInformation)f.Current).Id);
                        }
                    }

                    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 isTempContainer    = false;
                TemperatureContainer tc = null;

                try
                {
                    tc = (TemperatureContainer)owc;
                    isTempContainer = true;
                }
                catch (System.InvalidCastException)
                {
                    tc = null;
                    isTempContainer = false; //just to reiterate
                }

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

                    double max       = tc.MaxTemperature;
                    double min       = tc.MinTemperature;
                    bool   hasAlarms = tc.hasTemperatureAlarms();

                    Debug.WriteLine("= This device " + (hasAlarms ? "has" : "does not have") + " alarms");
                    Debug.WriteLine("= Maximum temperature: " + max);
                    Debug.WriteLine("= Minimum temperature: " + min);

                    double high  = 0.0;
                    double low   = 0.0;
                    byte[] state = tc.readDevice();

                    if (hasAlarms)
                    {
                        high = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state);
                        low  = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state);

                        Debug.WriteLine("= High temperature alarm set to : " + high);
                        Debug.WriteLine("= Low temperature alarm set to  : " + low);
                    }

                    double resol      = 0.0;
                    bool   selectable = tc.hasSelectableTemperatureResolution();

                    if (hasAlarms)
                    {
                        resol = tc.TemperatureAlarmResolution;

                        Debug.WriteLine("= Temperature alarm resolution  : " + resol);
                    }

                    double   tempres    = tc.getTemperatureResolution(state);
                    double[] resolution = null;

                    Debug.WriteLine("= Temperature resolution        : " + tempres);
                    Debug.WriteLine("= Resolution is selectable      : " + selectable);

                    if (selectable)
                    {
                        try
                        {
                            resolution = tc.TemperatureResolutions;

                            for (int i = 0; i < resolution.Length; i++)
                            {
                                Debug.WriteLine("= Available resolution " + i + "        : " + resolution[i]);
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine("= Could not get available resolutions : " + e.ToString());
                        }
                    }

                    if (hasAlarms)
                    {
                        Debug.WriteLine("= Setting high temperature alarm to 28.0 C...");
                        tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, 28.0, state);
                        Debug.WriteLine("= Setting low temperature alarm to 23.0 C...");
                        tc.setTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, 23.0, state);
                    }

                    if (selectable)
                    {
                        try
                        {
                            Debug.WriteLine("= Setting temperature resolution to " + resolution[0] + "...");
                            tc.setTemperatureResolution(resolution[0], state);
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine("= Could not set resolution: " + e.ToString());
                        }
                    }

                    try
                    {
                        tc.writeDevice(state);
                        Debug.WriteLine("= Device state written.");
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("= Could not write device state, all changes lost.");
                        Debug.WriteLine("= Exception occurred: " + e.ToString());
                    }

                    Debug.WriteLine("= Doing temperature conversion...");

                    try
                    {
                        tc.doTemperatureConvert(state);
                    }
                    catch (Exception)
                    {
                        Debug.WriteLine("= Could not complete temperature conversion...");
                    }

                    state = tc.readDevice();

                    if (hasAlarms)
                    {
                        high = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_HIGH, state);
                        low  = tc.getTemperatureAlarm(TemperatureContainer_Fields.ALARM_LOW, state);

                        Debug.WriteLine("= High temperature alarm set to : " + high);
                        Debug.WriteLine("= Low temperature alarm set to  : " + low);
                    }

                    double temp = tc.getTemperature(state);

                    Debug.WriteLine("= Reported temperature: " + temp);
                }
                else
                {
                    Debug.WriteLine("= This device is not a temperature device.");
                    Debug.WriteLine("=");
                    Debug.WriteLine("=");
                }

                next = access.findNextDevice();
            }
        }