예제 #1
0
        private async Task Update()
        {
            byte[]   readBuffer;
            BitArray bufferBits;

            //Send the command to the sensor to tell it to do the thing.
            i2cPeripheral.WriteBytes(mprlsMeasurementCommand);

            //Datasheet says wait 5 ms.
            await Task.Delay(5);

            while (true)
            {
                readBuffer = i2cPeripheral.ReadBytes(1);
                bufferBits = new BitArray(readBuffer);

                //From section 6.5 of the datasheet.
                this.IsDevicePowered          = bufferBits[6];
                this.IsDeviceBusy             = bufferBits[5];
                this.HasMemoryIntegrityFailed = bufferBits[2];
                this.InternalMathSaturated    = bufferBits[0];

                if (this.InternalMathSaturated)
                {
                    throw new InvalidOperationException("Sensor pressure has exceeded max value!");
                }

                if (this.HasMemoryIntegrityFailed)
                {
                    throw new InvalidOperationException("Sensor internal memory integrity check failed!");
                }

                if (!(this.IsDeviceBusy))
                {
                    break;
                }
            }

            readBuffer = i2cPeripheral.ReadBytes(4);

            RawPSIMeasurement = (readBuffer[1] << 16) | (readBuffer[2] << 8) | readBuffer[3];
            //Console.WriteLine(RawPSIMeasurement);

            //From Section 8.0 of the datasheet.
            CalculatedPSIMeasurement = (RawPSIMeasurement - 1677722) * (psiMax - psiMin);
            //Console.WriteLine(CalculatedPSIMeasurement);

            CalculatedPSIMeasurement /= 15099494 - 1677722;
            //Console.WriteLine(CalculatedPSIMeasurement);

            CalculatedPSIMeasurement += psiMin;
            //Console.WriteLine(CalculatedPSIMeasurement);

            //https://www.justintools.com/unit-conversion/pressure.php?k1=psi&k2=millibars
            CalculatedhPAMeasurement = CalculatedPSIMeasurement * 68.947572932;
            //Console.WriteLine(CalculatedhPAMeasurement);

            Conditions.Pressure = CalculatedPSIMeasurement;
        }
예제 #2
0
        public byte[] Read(int expectedByteCount, out UInt16 ResultCode)
        {
            //Read the IOT-SAS reply

            var data       = new byte[expectedByteCount];
            var resultCode = new byte[2];


            var count = 0;

#if DEBUGSERIAL
            Console.WriteLine($"RX: Start RX {expectedByteCount}");
#endif

            try
            {
                while (count < expectedByteCount)
                {
                    var datain = sas.ReadBytes((ushort)(expectedByteCount - count));
                    Array.Copy(datain, 0, data, count, datain.Length);
#if DEBUGSERIAL
                    Console.WriteLine($"RX: {BitConverter.ToString(datain)}");
#endif
                    count += datain.Length;
                }

                //count = 0;
                //expectedByteCount = 2;
                //while (count < 2)
                //{
                resultCode = sas.ReadBytes((ushort)(1));
                // }
                Array.Reverse(resultCode);
                ResultCode = BitConverter.ToUInt16(resultCode, 0);
            }
            catch (TimeoutException)
            {
                if (count == 2)
                {
                    Array.Reverse(data, 0, 2);
                    ResultCode = BitConverter.ToUInt16(data, 0);
                    throw new SASException(ResultCode, "Serial port - Timeout, no data");
                }
                else
                {
                    throw new SASException(eResultCode.PORT_NO_DATA, "Serial port - Timeout, no data");
                }
            }

            resultCode = sas.ReadBytes(2);
            Array.Reverse(resultCode);
            ResultCode = BitConverter.ToUInt16(resultCode, 0);
#if DEBUGSERIAL
            Console.WriteLine($"RXE: {BitConverter.ToString(resultCode,0,2)}  ResultCode: {resultCode.ToHexString()}");
#endif
            return(data);
        }
예제 #3
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevant properties.
        /// </summary>
        public async Task Update()
        {
            hih6130.WriteByte(0);
            //
            //  Sensor takes 35ms to make a valid reading.
            //
            await Task.Delay(40);

            var data = hih6130.ReadBytes(4);

            //
            //  Data format:
            //
            //  Byte 0: S1  S0  H13 H12 H11 H10 H9 H8
            //  Byte 1: H7  H6  H5  H4  H3  H2  H1 H0
            //  Byte 2: T13 T12 T11 T10 T9  T8  T7 T6
            //  Byte 4: T5  T4  T3  T2  T1  T0  XX XX
            //
            if ((data[0] & 0xc0) != 0)
            {
                throw new Exception("Status indicates readings are invalid.");
            }
            var reading = ((data[0] << 8) | data[1]) & 0x3fff;

            Conditions.Humidity    = ((float)reading / 16383) * 100;
            reading                = ((data[2] << 8) | data[3]) >> 2;
            Conditions.Temperature = (((float)reading / 16383) * 165) - 40;
        }
예제 #4
0
        protected async Task <AtmosphericConditions> ReadSensor()
        {
            AtmosphericConditions conditions = new AtmosphericConditions();

            return(await Task.Run(() => {
                si7021.WriteByte(HUMDITY_MEASURE_NOHOLD);
                //
                //  Maximum conversion time is 12ms (page 5 of the datasheet).
                //
                Thread.Sleep(25);
                var data = si7021.ReadBytes(3);
                var humidityReading = (ushort)((data[0] << 8) + data[1]);
                conditions.Humidity = ((125 * (float)humidityReading) / 65536) - 6;
                if (conditions.Humidity < 0)
                {
                    conditions.Humidity = 0;
                }
                else
                {
                    if (conditions.Humidity > 100)
                    {
                        conditions.Humidity = 100;
                    }
                }
                data = si7021.ReadRegisters(TEMPERATURE_MEASURE_PREVIOUS, 2);
                var temperatureReading = (short)((data[0] << 8) + data[1]);
                conditions.Temperature = (float)(((175.72 * temperatureReading) / 65536) - 46.85);

                return conditions;
            }));
        }
예제 #5
0
        /// <summary>
        /// Get ASG01DB VOC Gas Concentration
        /// </summary>
        /// <returns>Concentration (ppm)</returns>
        public double GetConcentration()
        {
            var data = new byte[] { ASG_DATA_MSB, ASG_DATA_LSB };

            sensor.WriteBytes(data);
            var readBuffer = sensor.ReadBytes(3);

            // CRC check
            //   if (!CheckCrc8(readBuffer, 2, readBuffer[1]))
            //   {
            //       return -1;
            //   }

            ushort res = BinaryPrimitives.ReadUInt16BigEndian(readBuffer.AsSpan(0, 2));

            return(res / 10.0);
        }
예제 #6
0
        /// <summary>
        ///     Force the sensor to make a reading and update the relevanyt properties.
        /// </summary>
        /// <param name="startAddress">Start address for the read operation.</param>
        /// <param name="amount">Amount of data to read from the EEPROM.</param>
        public byte[] Read(ushort startAddress, ushort amount)
        {
            CheckAddress(startAddress, amount);
            var address = new byte[2];

            address[0] = (byte)((startAddress >> 8) & 0xff);
            address[1] = (byte)(startAddress & 0xff);
            _eeprom.WriteBytes(address);
            return(_eeprom.ReadBytes(amount));
        }
예제 #7
0
        /// <summary>
        /// Read data via I2C (DHT12, etc.)
        /// </summary>
        internal virtual void ReadDataI2c()
        {
            _sensor.WriteByte(0x00);
            _readBuffer = _sensor.ReadBytes(5);

            _lastMeasurement = Environment.TickCount;

            if ((_readBuffer[4] == ((_readBuffer[0] + _readBuffer[1] + _readBuffer[2] + _readBuffer[3]) & 0xFF)))
            {
                WasLastReadSuccessful = (_readBuffer[0] != 0) || (_readBuffer[2] != 0);
            }
            else
            {
                WasLastReadSuccessful = false;
            }
        }
예제 #8
0
        private long ReadUncompensatedTemperature()
        {
            // write register address
            bmp085.WriteBytes(new byte[] { 0xF4, 0x2E });

            // Required as per datasheet.
            Thread.Sleep(5);

            // write register address
            bmp085.WriteBytes(new byte[] { 0xF6 });

            // get MSB and LSB result
            byte[] data = new byte[2];
            data = bmp085.ReadBytes(2);

            return((data[0] << 8) | data[1]);
        }
예제 #9
0
 void ReadStatus()
 {
     reception_data = i2cPeripheral.ReadBytes(5);
     Thread.Sleep(100);
 }