/// <summary> /// Gets the current CO2 concentration from the sensor. /// </summary> /// <returns>CO2 volume concentration</returns> /// <exception cref="IOException">Communication with sensor failed</exception> /// <exception cref="TimeoutException">A timeout occurred while communicating with the sensor</exception> public VolumeConcentration GetCo2Reading() { // send read command request var request = CreateRequest(Command.ReadCo2Concentration); request[(int)MessageFormat.Checksum] = Checksum(request); _serialPortStream.Write(request, 0, request.Length); // read complete response (9 bytes expected) byte[] response = new byte[MessageBytes]; long endTicks = DateTime.Now.AddMilliseconds(250).Ticks; int bytesRead = 0; while (DateTime.Now.Ticks < endTicks && bytesRead < MessageBytes) { bytesRead += _serialPortStream.Read(response, bytesRead, response.Length - bytesRead); Thread.Sleep(1); } if (bytesRead < MessageBytes) { throw new TimeoutException($"Communication with sensor failed."); } // check response and return calculated concentration if valid if (response[(int)MessageFormat.Checksum] == Checksum(response)) { return(VolumeConcentration.FromPartsPerMillion((int)response[(int)MessageFormat.DataHighResponse] * 256 + (int)response[(int)MessageFormat.DataLowResponse])); } else { throw new IOException("Invalid response message received from sensor"); } }
public void As() { var decimalfraction = VolumeConcentration.FromDecimalFractions(1); AssertEx.EqualTolerance(CentilitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.CentilitersPerLiter), CentilitersPerLiterTolerance); AssertEx.EqualTolerance(CentilitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.CentilitersPerMililiter), CentilitersPerMililiterTolerance); AssertEx.EqualTolerance(DecilitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.DecilitersPerLiter), DecilitersPerLiterTolerance); AssertEx.EqualTolerance(DecilitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.DecilitersPerMililiter), DecilitersPerMililiterTolerance); AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.DecimalFraction), DecimalFractionsTolerance); AssertEx.EqualTolerance(LitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.LitersPerLiter), LitersPerLiterTolerance); AssertEx.EqualTolerance(LitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.LitersPerMililiter), LitersPerMililiterTolerance); AssertEx.EqualTolerance(MicrolitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.MicrolitersPerLiter), MicrolitersPerLiterTolerance); AssertEx.EqualTolerance(MicrolitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.MicrolitersPerMililiter), MicrolitersPerMililiterTolerance); AssertEx.EqualTolerance(MillilitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.MillilitersPerLiter), MillilitersPerLiterTolerance); AssertEx.EqualTolerance(MillilitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.MillilitersPerMililiter), MillilitersPerMililiterTolerance); AssertEx.EqualTolerance(NanolitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.NanolitersPerLiter), NanolitersPerLiterTolerance); AssertEx.EqualTolerance(NanolitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.NanolitersPerMililiter), NanolitersPerMililiterTolerance); AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PartPerBillion), PartsPerBillionTolerance); AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PartPerMillion), PartsPerMillionTolerance); AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PartPerThousand), PartsPerThousandTolerance); AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PartPerTrillion), PartsPerTrillionTolerance); AssertEx.EqualTolerance(PercentInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.Percent), PercentTolerance); AssertEx.EqualTolerance(PicolitersPerLiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PicolitersPerLiter), PicolitersPerLiterTolerance); AssertEx.EqualTolerance(PicolitersPerMililiterInOneDecimalFraction, decimalfraction.As(VolumeConcentrationUnit.PicolitersPerMililiter), PicolitersPerMililiterTolerance); }
/// <summary> /// Read the equivalent CO2 in ppm and equivalent Total Volatile Compound in ppb /// </summary> /// <param name="equivalentCO2">The equivalent CO2 (eCO2) output range for CCS811 is from /// 400ppm up to 29206ppm.</param> /// <param name="equivalentTotalVolatileOrganicCompound">The equivalent Total Volatile Organic Compound (eTVOC) /// output range for CCS811 is from 0ppb up to 32768ppb</param> /// <param name="rawCurrentSelected">Raw data containing the value of the /// current through the sensor(0μA to 63μA)</param> /// <param name="rawAdcReading">Raw data containing the /// readings of the voltage across the sensor with the selected /// current(1023 = 1.65V) where 1023 is the maximum value</param> /// <returns>True if success</returns> public bool TryReadGasData(out VolumeConcentration equivalentCO2, out VolumeConcentration equivalentTotalVolatileOrganicCompound, out ElectricCurrent rawCurrentSelected, out int rawAdcReading) { int equivalentCO2InPpm = -1; int equivalentTotalVolatileOrganicCompoundInPpb = -1; int rawCurrent = -1; rawAdcReading = -1; Span <byte> toRead = stackalloc byte[8]; ReadRegister(Register.ALG_RESULT_DATA, toRead); if (toRead[5] != (byte)Error.NoError) { equivalentCO2 = VolumeConcentration.Zero; equivalentTotalVolatileOrganicCompound = VolumeConcentration.Zero; rawCurrentSelected = ElectricCurrent.Zero; return(false); } equivalentCO2InPpm = BinaryPrimitives.ReadInt16BigEndian(toRead.Slice(0, 2)); equivalentTotalVolatileOrganicCompoundInPpb = BinaryPrimitives.ReadInt16BigEndian(toRead.Slice(2, 2)); rawCurrent = toRead[6] >> 2; rawAdcReading = ((toRead[6] & 0b0000_0011) << 2) + toRead[7]; equivalentCO2 = VolumeConcentration.FromPartsPerMillion(equivalentCO2InPpm); equivalentTotalVolatileOrganicCompound = VolumeConcentration.FromPartsPerBillion(equivalentTotalVolatileOrganicCompoundInPpb); rawCurrentSelected = ElectricCurrent.FromMicroamperes(rawCurrent); return((equivalentCO2InPpm >= 400) && (equivalentCO2InPpm <= 29206) && (equivalentTotalVolatileOrganicCompoundInPpb >= 0) && (equivalentTotalVolatileOrganicCompoundInPpb <= 32768)); }
public void DecimalFractionToVolumeConcentrationUnits() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); AssertEx.EqualTolerance(CentilitersPerLiterInOneDecimalFraction, decimalfraction.CentilitersPerLiter, CentilitersPerLiterTolerance); AssertEx.EqualTolerance(CentilitersPerMililiterInOneDecimalFraction, decimalfraction.CentilitersPerMililiter, CentilitersPerMililiterTolerance); AssertEx.EqualTolerance(DecilitersPerLiterInOneDecimalFraction, decimalfraction.DecilitersPerLiter, DecilitersPerLiterTolerance); AssertEx.EqualTolerance(DecilitersPerMililiterInOneDecimalFraction, decimalfraction.DecilitersPerMililiter, DecilitersPerMililiterTolerance); AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, decimalfraction.DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(LitersPerLiterInOneDecimalFraction, decimalfraction.LitersPerLiter, LitersPerLiterTolerance); AssertEx.EqualTolerance(LitersPerMililiterInOneDecimalFraction, decimalfraction.LitersPerMililiter, LitersPerMililiterTolerance); AssertEx.EqualTolerance(MicrolitersPerLiterInOneDecimalFraction, decimalfraction.MicrolitersPerLiter, MicrolitersPerLiterTolerance); AssertEx.EqualTolerance(MicrolitersPerMililiterInOneDecimalFraction, decimalfraction.MicrolitersPerMililiter, MicrolitersPerMililiterTolerance); AssertEx.EqualTolerance(MillilitersPerLiterInOneDecimalFraction, decimalfraction.MillilitersPerLiter, MillilitersPerLiterTolerance); AssertEx.EqualTolerance(MillilitersPerMililiterInOneDecimalFraction, decimalfraction.MillilitersPerMililiter, MillilitersPerMililiterTolerance); AssertEx.EqualTolerance(NanolitersPerLiterInOneDecimalFraction, decimalfraction.NanolitersPerLiter, NanolitersPerLiterTolerance); AssertEx.EqualTolerance(NanolitersPerMililiterInOneDecimalFraction, decimalfraction.NanolitersPerMililiter, NanolitersPerMililiterTolerance); AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, decimalfraction.PartsPerBillion, PartsPerBillionTolerance); AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, decimalfraction.PartsPerMillion, PartsPerMillionTolerance); AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, decimalfraction.PartsPerThousand, PartsPerThousandTolerance); AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, decimalfraction.PartsPerTrillion, PartsPerTrillionTolerance); AssertEx.EqualTolerance(PercentInOneDecimalFraction, decimalfraction.Percent, PercentTolerance); AssertEx.EqualTolerance(PicolitersPerLiterInOneDecimalFraction, decimalfraction.PicolitersPerLiter, PicolitersPerLiterTolerance); AssertEx.EqualTolerance(PicolitersPerMililiterInOneDecimalFraction, decimalfraction.PicolitersPerMililiter, PicolitersPerMililiterTolerance); }
public void DefaultCtor_ReturnsQuantityWithZeroValueAndBaseUnit() { var quantity = new VolumeConcentration(); Assert.Equal(0, quantity.Value); Assert.Equal(VolumeConcentrationUnit.DecimalFraction, quantity.Unit); }
public void EqualsRelativeToleranceIsImplemented() { var v = VolumeConcentration.FromDecimalFractions(1); Assert.True(v.Equals(VolumeConcentration.FromDecimalFractions(1), DecimalFractionsTolerance, ComparisonType.Relative)); Assert.False(v.Equals(VolumeConcentration.Zero, DecimalFractionsTolerance, ComparisonType.Relative)); }
public void ConversionRoundTrip() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); AssertEx.EqualTolerance(1, VolumeConcentration.FromCentilitersPerLiter(decimalfraction.CentilitersPerLiter).DecimalFractions, CentilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromCentilitersPerMililiter(decimalfraction.CentilitersPerMililiter).DecimalFractions, CentilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromDecilitersPerLiter(decimalfraction.DecilitersPerLiter).DecimalFractions, DecilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromDecilitersPerMililiter(decimalfraction.DecilitersPerMililiter).DecimalFractions, DecilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromDecimalFractions(decimalfraction.DecimalFractions).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromLitersPerLiter(decimalfraction.LitersPerLiter).DecimalFractions, LitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromLitersPerMililiter(decimalfraction.LitersPerMililiter).DecimalFractions, LitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromMicrolitersPerLiter(decimalfraction.MicrolitersPerLiter).DecimalFractions, MicrolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromMicrolitersPerMililiter(decimalfraction.MicrolitersPerMililiter).DecimalFractions, MicrolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromMillilitersPerLiter(decimalfraction.MillilitersPerLiter).DecimalFractions, MillilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromMillilitersPerMililiter(decimalfraction.MillilitersPerMililiter).DecimalFractions, MillilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromNanolitersPerLiter(decimalfraction.NanolitersPerLiter).DecimalFractions, NanolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromNanolitersPerMililiter(decimalfraction.NanolitersPerMililiter).DecimalFractions, NanolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPartsPerBillion(decimalfraction.PartsPerBillion).DecimalFractions, PartsPerBillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPartsPerMillion(decimalfraction.PartsPerMillion).DecimalFractions, PartsPerMillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPartsPerThousand(decimalfraction.PartsPerThousand).DecimalFractions, PartsPerThousandTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPartsPerTrillion(decimalfraction.PartsPerTrillion).DecimalFractions, PartsPerTrillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPercent(decimalfraction.Percent).DecimalFractions, PercentTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPicolitersPerLiter(decimalfraction.PicolitersPerLiter).DecimalFractions, PicolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.FromPicolitersPerMililiter(decimalfraction.PicolitersPerMililiter).DecimalFractions, PicolitersPerMililiterTolerance); }
public void FromValueAndUnit() { AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.CentilitersPerLiter).CentilitersPerLiter, CentilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.CentilitersPerMililiter).CentilitersPerMililiter, CentilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.DecalitersPerLiter).DecalitersPerLiter, DecalitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.DecalitersPerMililiter).DecalitersPerMililiter, DecalitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.DecilitersPerLiter).DecilitersPerLiter, DecilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.DecilitersPerMililiter).DecilitersPerMililiter, DecilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.DecimalFraction).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.FemtolitersPerLiter).FemtolitersPerLiter, FemtolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.FemtolitersPerMililiter).FemtolitersPerMililiter, FemtolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.HectolitersPerLiter).HectolitersPerLiter, HectolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.KilolitersPerLiter).KilolitersPerLiter, KilolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.KilolitersPerMililiter).KilolitersPerMililiter, KilolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.LitersPerLiter).LitersPerLiter, LitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.LitersPerMililiter).LitersPerMililiter, LitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.MicrolitersPerLiter).MicrolitersPerLiter, MicrolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.MicrolitersPerMililiter).MicrolitersPerMililiter, MicrolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.MillilitersPerLiter).MillilitersPerLiter, MillilitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.MillilitersPerMililiter).MillilitersPerMililiter, MillilitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.NanolitersPerLiter).NanolitersPerLiter, NanolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.NanolitersPerMililiter).NanolitersPerMililiter, NanolitersPerMililiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PartPerBillion).PartsPerBillion, PartsPerBillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PartPerMillion).PartsPerMillion, PartsPerMillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PartPerThousand).PartsPerThousand, PartsPerThousandTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PartPerTrillion).PartsPerTrillion, PartsPerTrillionTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.Percent).Percent, PercentTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PicolitersPerLiter).PicolitersPerLiter, PicolitersPerLiterTolerance); AssertEx.EqualTolerance(1, VolumeConcentration.From(1, VolumeConcentrationUnit.PicolitersPerMililiter).PicolitersPerMililiter, PicolitersPerMililiterTolerance); }
public void CompareToIsImplemented() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); Assert.Equal(0, decimalfraction.CompareTo(decimalfraction)); Assert.True(decimalfraction.CompareTo(VolumeConcentration.Zero) > 0); Assert.True(VolumeConcentration.Zero.CompareTo(decimalfraction) < 0); }
private bool IsPpmValidThreshold(VolumeConcentration ppm) { if ((ppm < VolumeConcentration.Zero) || (ppm > VolumeConcentration.FromPartsPerMillion(ushort.MaxValue))) { return(false); } return(true); }
public void EqualsIsImplemented() { var a = VolumeConcentration.FromDecimalFractions(1); var b = VolumeConcentration.FromDecimalFractions(2); Assert.True(a.Equals(a)); Assert.False(a.Equals(b)); Assert.False(a.Equals(null)); }
0.01142805, MolarityUnit.MolePerLiter)] // 10 % dilution of HCL public void MolarityFromDilutedSolution( double startingMolarityValue, MolarityUnit startingMolarityUnit, double newConcentration, VolumeConcentrationUnit newConcentrationUnit, double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerence = 1e-5) { var startingMolarity = new Molarity(startingMolarityValue, startingMolarityUnit); var newVolumeConc = new VolumeConcentration(newConcentration, newConcentrationUnit); Molarity dilutedMolarity = startingMolarity * newVolumeConc; AssertEx.EqualTolerance(expectedMolarityValue, dilutedMolarity.As(expectedMolarityUnit), tolerence); }
public void ArithmeticOperators() { VolumeConcentration v = VolumeConcentration.FromDecimalFractions(1); AssertEx.EqualTolerance(-1, -v.DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(2, (VolumeConcentration.FromDecimalFractions(3) - v).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(2, (v + v).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(10, (v * 10).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(10, (10 * v).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(2, (VolumeConcentration.FromDecimalFractions(10) / 5).DecimalFractions, DecimalFractionsTolerance); AssertEx.EqualTolerance(2, VolumeConcentration.FromDecimalFractions(10) / VolumeConcentration.FromDecimalFractions(5), DecimalFractionsTolerance); }
0.5, VolumeConcentrationUnit.DecimalFraction)] // synthetic data public void VolumeConcentrationFromMassConcentrationAndDensity( double componentDensityValue, DensityUnit componentDensityUnit, double massConcValue, MassConcentrationUnit masConcUnit, double expectedVolumeConcValue, VolumeConcentrationUnit expectedVolumeConcUnit, double tolerence = 1e-5) { var density = new Density(componentDensityValue, componentDensityUnit); var massConcentration = new MassConcentration(massConcValue, masConcUnit); VolumeConcentration volumeConcentration = massConcentration.ToVolumeConcentration(density); // massConcentration / density; AssertEx.EqualTolerance(expectedVolumeConcValue, volumeConcentration.As(expectedVolumeConcUnit), tolerence); }
23.03422, MassConcentrationUnit.GramPerLiter)] // 29.19419518377693 = VolumeConcentration_0_5M_Ethanol public void MassConcentrationFromVolumeConcentrationAndComponentDensity( double volumeConcValue, VolumeConcentrationUnit volumeConcUnit, double componentDensityValue, DensityUnit componentDensityUnit, double expectedMassConcValue, MassConcentrationUnit expectedMassConcUnit, double tolerence = 1e-5) { var volumeConcentration = new VolumeConcentration(volumeConcValue, volumeConcUnit); var componentDensity = new Density(componentDensityValue, componentDensityUnit); MassConcentration massConcentration = volumeConcentration.ToMassConcentration(componentDensity); // volumeConcentration * density AssertEx.EqualTolerance(expectedMassConcValue, massConcentration.As(expectedMassConcUnit), tolerence); }
/// <summary> /// Main entry point /// </summary> public static void Main(string[] args) { // create serial port using the setting acc. to datasheet, pg. 7, sec. general settings var serialPort = new SerialPort("/dev/serial0", 9600, Parity.None, 8, StopBits.One) { Encoding = Encoding.ASCII, ReadTimeout = 1000, WriteTimeout = 1000 }; serialPort.Open(); Mhz19b sensor = new Mhz19b(serialPort.BaseStream, true); // Alternatively you can let the binding create the serial port stream: // Mhz19b sensor = new Mhz19b("/dev/serial0"); // Switch ABM on (default). // sensor.SetAutomaticBaselineCorrection(AbmState.On); // Set sensor detection range to 2000ppm (default). // sensor.SetSensorDetectionRange(DetectionRange.Range2000); // Perform calibration // Step #1: perform zero point calibration // Step #2: perform span point calibration at 2000ppm // CAUTION: enable the following lines only if you know exactly what you do. // Consider also that zero point and span point calibration are performed // at different concentrations. The sensor requires up to 20 min to be // saturated at the target level. // sensor.PerformZeroPointCalibration(); // ---- Now change to target concentration for span point. // sensor.PerformSpanPointCalibration(VolumeConcentration.FromPartsPerMillion(2000)); // Continously read current concentration while (true) { try { VolumeConcentration reading = sensor.GetCo2Reading(); Console.WriteLine($"{reading.PartsPerMillion:F0} ppm"); } catch (IOException e) { Console.WriteLine("Concentration couldn't be read"); Console.WriteLine(e.Message); Console.WriteLine(e.InnerException.Message); } Thread.Sleep(1000); } }
0.5, MolarityUnit.MolesPerLiter)] // 29.19419518377693 = VolumeConcentration_0_5M_Ethanol public void MolarityFromVolumeConcentrationAndComponentDensityAndMolarMass( double volumeConcValue, VolumeConcentrationUnit volumeConcUnit, double componentDensityValue, DensityUnit componetDensityUnit, double componentMolarMassValue, MolarMassUnit componentMolarMassUnit, double expectedMolarityValue, MolarityUnit expectedMolarityUnit, double tolerence = 1e-5) { var volumeConcentration = new VolumeConcentration(volumeConcValue, volumeConcUnit); var componentDensity = new Density(componentDensityValue, componetDensityUnit); var componentMolarMass = new MolarMass(componentMolarMassValue, componentMolarMassUnit); Molarity molarity = volumeConcentration.ToMolarity(componentDensity, componentMolarMass); // volumeConcentration * density / molarMass AssertEx.EqualTolerance(expectedMolarityValue, molarity.As(expectedMolarityUnit), tolerence); }
29.19419518377693, VolumeConcentrationUnit.MillilitersPerLiter)] // 0.5M ethanol public void VolumeConcentrationFromComponentDensityAndMolarity( double molarityValue, MolarityUnit molarityUnit, double componentDensityValue, DensityUnit componentDensityUnit, double componentMolarMassValue, MolarMassUnit compontMolarMassUnit, double expectedVolumeConcValue, VolumeConcentrationUnit expectedVolumeConcUnit, double tolerence = 1e-5) { var molarity = new Molarity(molarityValue, molarityUnit); var componentDensity = new Density(componentDensityValue, componentDensityUnit); var componentMolarMass = new MolarMass(componentMolarMassValue, compontMolarMassUnit); VolumeConcentration volumeConcentration = molarity.ToVolumeConcentration(componentDensity, componentMolarMass); AssertEx.EqualTolerance(expectedVolumeConcValue, volumeConcentration.As(expectedVolumeConcUnit), tolerence); }
/// <summary> /// Initiate a span point calibration. /// </summary> /// <param name="span">span value, between 1000[ppm] and 5000[ppm]. The typical value is 2000[ppm].</param> /// <exception cref="System.ArgumentException">Thrown when span value is out of range</exception> /// <exception cref="System.IO.IOException">Communication with sensor failed</exception> public void PerformSpanPointCalibration(VolumeConcentration span) { if ((span.PartsPerMillion < 1000) || (span.PartsPerMillion > 5000)) { throw new ArgumentException("Span value out of range (1000-5000[ppm])", nameof(span)); } var request = CreateRequest(Command.CalibrateSpanPoint); // set span in request, c. f. datasheet rev. 1.0, pg. 8 for details request[(int)MessageFormat.DataHighRequest] = (byte)(span.PartsPerMillion / 256); request[(int)MessageFormat.DataLowRequest] = (byte)(span.PartsPerMillion % 256); SendRequest(request); }
public void ComparisonOperators() { VolumeConcentration oneDecimalFraction = VolumeConcentration.FromDecimalFractions(1); VolumeConcentration twoDecimalFractions = VolumeConcentration.FromDecimalFractions(2); Assert.True(oneDecimalFraction < twoDecimalFractions); Assert.True(oneDecimalFraction <= twoDecimalFractions); Assert.True(twoDecimalFractions > oneDecimalFraction); Assert.True(twoDecimalFractions >= oneDecimalFraction); Assert.False(oneDecimalFraction > twoDecimalFractions); Assert.False(oneDecimalFraction >= twoDecimalFractions); Assert.False(twoDecimalFractions < oneDecimalFraction); Assert.False(twoDecimalFractions <= oneDecimalFraction); }
public void EqualityOperators() { var a = VolumeConcentration.FromDecimalFractions(1); var b = VolumeConcentration.FromDecimalFractions(2); // ReSharper disable EqualExpressionComparison Assert.True(a == a); Assert.False(a != a); Assert.True(a != b); Assert.False(a == b); Assert.False(a == null); Assert.False(null == a); // ReSharper restore EqualExpressionComparison }
public void VolumeConcentration_QuantityInfo_ReturnsQuantityInfoDescribingQuantity() { var quantity = new VolumeConcentration(1, VolumeConcentrationUnit.DecimalFraction); QuantityInfo <VolumeConcentrationUnit> quantityInfo = quantity.QuantityInfo; Assert.Equal(VolumeConcentration.Zero, quantityInfo.Zero); Assert.Equal("VolumeConcentration", quantityInfo.Name); Assert.Equal(QuantityType.VolumeConcentration, quantityInfo.QuantityType); var units = EnumUtils.GetEnumValues <VolumeConcentrationUnit>().Except(new[] { VolumeConcentrationUnit.Undefined }).ToArray(); var unitNames = units.Select(x => x.ToString()); // Obsolete members #pragma warning disable 618 Assert.Equal(units, quantityInfo.Units); Assert.Equal(unitNames, quantityInfo.UnitNames); #pragma warning restore 618 }
/// <summary> /// Set the threshold for the equivalent CO2. The pinInterrupt should be existing so /// interruptions are activated. If not, then the function will return false /// </summary> /// <param name="lowEquivalentCO2">The low value for the threshold</param> /// <param name="highEquivalentCO2">The high value for the threshold</param> /// <returns>True if success</returns> /// <remarks>Difference between the low and high value should be more than 50. This is called /// the hysteresis value.</remarks> public bool SetThreshold(VolumeConcentration lowEquivalentCO2, VolumeConcentration highEquivalentCO2) { if (_pinInterruption < 0) { return(false); } if (!IsPpmValidThreshold(lowEquivalentCO2)) { throw new ArgumentException($"{lowEquivalentCO2} can only be between 0 and {ushort.MaxValue}"); } if (!IsPpmValidThreshold(highEquivalentCO2)) { throw new ArgumentException($"{highEquivalentCO2} can only be between 0 and {ushort.MaxValue}"); } if (lowEquivalentCO2 > highEquivalentCO2) { var temp = highEquivalentCO2; highEquivalentCO2 = lowEquivalentCO2; lowEquivalentCO2 = temp; } if (highEquivalentCO2 - lowEquivalentCO2 < VolumeConcentration.FromPartsPerMillion(50)) { throw new ArgumentException($"{highEquivalentCO2}-{lowEquivalentCO2} should be more than 50"); } Span <byte> toSend = stackalloc byte[4]; BinaryPrimitives.WriteUInt16BigEndian(toSend.Slice(0, 2), (ushort)lowEquivalentCO2.PartsPerMillion); BinaryPrimitives.WriteUInt16BigEndian(toSend.Slice(2, 2), (ushort)highEquivalentCO2.PartsPerMillion); WriteRegister(Register.THRESHOLDS, toSend); // Activate the interrupt threshold as well byte mode = ReadRegister(Register.MEAS_MODE); mode |= 0b0000_0100; WriteRegister(Register.MEAS_MODE, mode); return(!Status.HasFlag(Status.ERROR)); }
private static void TestThresholdAndInterrupt(Ccs811Sensor ccs811) { if (!ccs811.InterruptEnable) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: interrupt needs to be activated to run this test"); Console.ResetColor(); return; } ccs811.MeasurementReady += Ccs811MeasurementReady; // Setting up a range where we will see something in a normal environment VolumeConcentration low = VolumeConcentration.FromPartsPerMillion(400); VolumeConcentration high = VolumeConcentration.FromPartsPerMillion(600); Console.WriteLine($"Setting up {low.PartsPerMillion}-{high.PartsPerMillion} range, in clear environment, that should raise interrupts. Wait 3 minutes and change mode. Blow on the sensor and wait a bit."); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Warning: only the first measurement to cross the threshold is raised."); Console.ResetColor(); ccs811.SetThreshold(low, high); DateTime dt = DateTime.Now.AddMinutes(3); while (dt > DateTime.Now) { Thread.Sleep(10); } low = VolumeConcentration.FromPartsPerMillion(15000); high = VolumeConcentration.FromPartsPerMillion(20000); Console.WriteLine($"Changing threshold for {low.PartsPerMillion}-{high.PartsPerMillion}, a non reachable range in clear environment. No measurement should appear in next 3 minutes"); dt = DateTime.Now.AddMinutes(3); ccs811.SetThreshold(low, high); while (dt > DateTime.Now) { Thread.Sleep(10); } }
public void FromDecimalFractions_WithNanValue_ThrowsArgumentException() { Assert.Throws <ArgumentException>(() => VolumeConcentration.FromDecimalFractions(double.NaN)); }
public void EqualsReturnsFalseOnNull() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); Assert.False(decimalfraction.Equals(null)); }
public void EqualsReturnsFalseOnTypeMismatch() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); Assert.False(decimalfraction.Equals(new object())); }
public void CompareToThrowsOnNull() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); Assert.Throws <ArgumentNullException>(() => decimalfraction.CompareTo(null)); }
public void CompareToThrowsOnTypeMismatch() { VolumeConcentration decimalfraction = VolumeConcentration.FromDecimalFractions(1); Assert.Throws <ArgumentException>(() => decimalfraction.CompareTo(new object())); }
public void ToUnit() { var decimalfraction = VolumeConcentration.FromDecimalFractions(1); var centilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.CentilitersPerLiter); AssertEx.EqualTolerance(CentilitersPerLiterInOneDecimalFraction, (double)centilitersperliterQuantity.Value, CentilitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.CentilitersPerLiter, centilitersperliterQuantity.Unit); var centiliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.CentilitersPerMililiter); AssertEx.EqualTolerance(CentilitersPerMililiterInOneDecimalFraction, (double)centiliterspermililiterQuantity.Value, CentilitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.CentilitersPerMililiter, centiliterspermililiterQuantity.Unit); var decilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecilitersPerLiter); AssertEx.EqualTolerance(DecilitersPerLiterInOneDecimalFraction, (double)decilitersperliterQuantity.Value, DecilitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.DecilitersPerLiter, decilitersperliterQuantity.Unit); var deciliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecilitersPerMililiter); AssertEx.EqualTolerance(DecilitersPerMililiterInOneDecimalFraction, (double)deciliterspermililiterQuantity.Value, DecilitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.DecilitersPerMililiter, deciliterspermililiterQuantity.Unit); var decimalfractionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.DecimalFraction); AssertEx.EqualTolerance(DecimalFractionsInOneDecimalFraction, (double)decimalfractionQuantity.Value, DecimalFractionsTolerance); Assert.Equal(VolumeConcentrationUnit.DecimalFraction, decimalfractionQuantity.Unit); var litersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.LitersPerLiter); AssertEx.EqualTolerance(LitersPerLiterInOneDecimalFraction, (double)litersperliterQuantity.Value, LitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.LitersPerLiter, litersperliterQuantity.Unit); var literspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.LitersPerMililiter); AssertEx.EqualTolerance(LitersPerMililiterInOneDecimalFraction, (double)literspermililiterQuantity.Value, LitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.LitersPerMililiter, literspermililiterQuantity.Unit); var microlitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MicrolitersPerLiter); AssertEx.EqualTolerance(MicrolitersPerLiterInOneDecimalFraction, (double)microlitersperliterQuantity.Value, MicrolitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.MicrolitersPerLiter, microlitersperliterQuantity.Unit); var microliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MicrolitersPerMililiter); AssertEx.EqualTolerance(MicrolitersPerMililiterInOneDecimalFraction, (double)microliterspermililiterQuantity.Value, MicrolitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.MicrolitersPerMililiter, microliterspermililiterQuantity.Unit); var millilitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MillilitersPerLiter); AssertEx.EqualTolerance(MillilitersPerLiterInOneDecimalFraction, (double)millilitersperliterQuantity.Value, MillilitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.MillilitersPerLiter, millilitersperliterQuantity.Unit); var milliliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.MillilitersPerMililiter); AssertEx.EqualTolerance(MillilitersPerMililiterInOneDecimalFraction, (double)milliliterspermililiterQuantity.Value, MillilitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.MillilitersPerMililiter, milliliterspermililiterQuantity.Unit); var nanolitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.NanolitersPerLiter); AssertEx.EqualTolerance(NanolitersPerLiterInOneDecimalFraction, (double)nanolitersperliterQuantity.Value, NanolitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.NanolitersPerLiter, nanolitersperliterQuantity.Unit); var nanoliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.NanolitersPerMililiter); AssertEx.EqualTolerance(NanolitersPerMililiterInOneDecimalFraction, (double)nanoliterspermililiterQuantity.Value, NanolitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.NanolitersPerMililiter, nanoliterspermililiterQuantity.Unit); var partperbillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerBillion); AssertEx.EqualTolerance(PartsPerBillionInOneDecimalFraction, (double)partperbillionQuantity.Value, PartsPerBillionTolerance); Assert.Equal(VolumeConcentrationUnit.PartPerBillion, partperbillionQuantity.Unit); var partpermillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerMillion); AssertEx.EqualTolerance(PartsPerMillionInOneDecimalFraction, (double)partpermillionQuantity.Value, PartsPerMillionTolerance); Assert.Equal(VolumeConcentrationUnit.PartPerMillion, partpermillionQuantity.Unit); var partperthousandQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerThousand); AssertEx.EqualTolerance(PartsPerThousandInOneDecimalFraction, (double)partperthousandQuantity.Value, PartsPerThousandTolerance); Assert.Equal(VolumeConcentrationUnit.PartPerThousand, partperthousandQuantity.Unit); var partpertrillionQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PartPerTrillion); AssertEx.EqualTolerance(PartsPerTrillionInOneDecimalFraction, (double)partpertrillionQuantity.Value, PartsPerTrillionTolerance); Assert.Equal(VolumeConcentrationUnit.PartPerTrillion, partpertrillionQuantity.Unit); var percentQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.Percent); AssertEx.EqualTolerance(PercentInOneDecimalFraction, (double)percentQuantity.Value, PercentTolerance); Assert.Equal(VolumeConcentrationUnit.Percent, percentQuantity.Unit); var picolitersperliterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PicolitersPerLiter); AssertEx.EqualTolerance(PicolitersPerLiterInOneDecimalFraction, (double)picolitersperliterQuantity.Value, PicolitersPerLiterTolerance); Assert.Equal(VolumeConcentrationUnit.PicolitersPerLiter, picolitersperliterQuantity.Unit); var picoliterspermililiterQuantity = decimalfraction.ToUnit(VolumeConcentrationUnit.PicolitersPerMililiter); AssertEx.EqualTolerance(PicolitersPerMililiterInOneDecimalFraction, (double)picoliterspermililiterQuantity.Value, PicolitersPerMililiterTolerance); Assert.Equal(VolumeConcentrationUnit.PicolitersPerMililiter, picoliterspermililiterQuantity.Unit); }