コード例 #1
0
        private void ExpectLevel(GpioPinValue value, int milliseconds)
        {
            if (this.pin.PinMode != GpioPinDriveMode.Input)
            {
                this.pin.PinMode = GpioPinDriveMode.Input;
            }

            if (!pin.WaitForValue(value, milliseconds))
            {
                if (!pin.WaitForValue(value, milliseconds))
                {
                    throw new SensorReadTimeoutException("等待" + value + " 读取DHT11 温度和湿度超时");
                }
            }
        }
コード例 #2
0
ファイル: AM2302.cs プロジェクト: zhen08/raspberryio
        private THData TryGetMeasure()
        {
            //Prepare buffer to store measure and checksum
            var data = new byte[5];

            for (var i = 0; i < 5; i++)
            {
                data[i] = 0;
            }

            //Wait for sensor response
            _pin.PinMode = GpioPinDriveMode.Input;
            //Read acknowledgement from sensor
            _pin.WaitForValue(GpioPinValue.High, 100);
            _pin.WaitForValue(GpioPinValue.Low, 100);

            //Read 40 bits to acquire:
            //16 bit -> Humidity
            //16 bit -> Temperature
            //8 bit -> Checksum
            var cnt = 7; //bit counter
            var idx = 0; //bit index
            var hrt = new HighResolutionTimer();

            for (var i = 0; i < 40; i++)
            {
                hrt.Reset();
                _pin.WaitForValue(GpioPinValue.High, 100);
                hrt.Start();
                _pin.WaitForValue(GpioPinValue.Low, 100);
                hrt.Stop();
                //Check if signal is 1 or 0
                if (hrt.Elapsed > _bitDataTime)
                {
                    data[idx] |= (byte)(1 << cnt);
                }
                if (cnt == 0)
                {
                    idx++;   //next bit
                    cnt = 7; //restart with next byte
                }
                else
                {
                    cnt--;
                }
            }

            var checkSum = data[0] + data[1] + data[2] + data[3];

            if ((checkSum & 0xff) != data[4])
            {
                return(null);
            }

            var sign = 1;

            //Check negative temperature
            if ((data[2] & 0x80) != 0)
            {
                data[2] = (byte)(data[2] & 0x7F);
                sign    = -1;
            }

            return(new THData
            {
                HumidityPercentage = ((data[0] << 8) + data[1]) / 10,
                TemperatureCelsius = (sign * ((data[2] << 8) + data[3])) / 10
            });
        }