Ejemplo n.º 1
0
        /// <summary>
        /// Read sensor data
        /// </summary>
        /// <returns>true on success, else false</returns>
        public override bool Read()
        {
            if (Address != null && Address.Length == 8 && Address[0] == FAMILY_CODE)
            {
                //now write command and ROM at once
                byte[] cmdAndData = new byte[9] {
                    0x55,                                                                                          //match ROM command
                    Address[0], Address[1], Address[2], Address[3], Address[4], Address[5], Address[6], Address[7] //do not convert to a for..loop
                };

                _oneWire.TouchReset();
                foreach (var b in cmdAndData)
                {
                    _oneWire.WriteByte(b);
                }

                //now read the scratchpad
                var verify = _oneWire.WriteByte(READ_SCRATCHPAD);

                //Now read the temperature
                var tempLo = _oneWire.ReadByte();
                var tempHi = _oneWire.ReadByte();

                if (_oneWire.TouchReset())
                {
                    float currentTemperature = ((tempHi << 8) | tempLo) / 16;
                    TemperatureInCelcius = (float)(Math.Floor(currentTemperature * Math.Pow(10, _scale)) / Math.Pow(10, _scale));
                }
                else
                {
                    TemperatureInCelcius = ERROR_TEMPERATURE;
                }
            }
            return(TemperatureInCelcius != ERROR_TEMPERATURE);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read sensor data
        /// </summary>
        /// <returns>true on success, else false</returns>
        public bool Read()
        {
            SelectDevice();

            //now read the scratchpad
            var verify = _oneWire.WriteByte(READ_SCRATCHPAD);

            //Now read the temperature
            var tempLo = _oneWire.ReadByte();
            var tempHi = _oneWire.ReadByte();

            if (_oneWire.TouchReset())
            {
                var temp = ((tempHi << 8) | tempLo);

                // Bits manipulation to represent negative values correctly.
                if ((tempHi >> 7) == 1)
                {
                    temp = (temp | unchecked ((int)0xffff0000));
                }

                TemperatureInCelcius = ((float)temp) / 16;
                return(true);
            }
            else
            {
                TemperatureInCelcius = ERROR_TEMPERATURE;
                return(false);
            }
        }