tbf/TestBenchFramework/BenchControl/Keithley/TempMeter/TempMeter.cs
2016-08-26 11:36:42 +02:00

120 lines
3.9 KiB
C#

///
/// Copyright (c) 2016 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.Boxes;
using TBF.BenchControl.Keithley.Multimeter_2010_RS232;
namespace TBF.BenchControl.Keithley.TempMeter
{
public class TempMeter : ComponentBase, GenericDevices.ITempMeter
{
private static readonly ILog log = LogManager.GetLogger(typeof(TempMeter));
public override string ToString() { return string.Format("Keithley.TempMeter({0})", Cfg.ToString(1)); }
readonly TempMeterCfg tempMtrCfg; /// configuration
public readonly Multimeter Multimeter; /// parent component
public int Channel { get { return tempMtrCfg.Channel; } } /// 1..5
/// Dictionaries to maintain separate operations for each FloatBox argument
Dictionary<FloatBox, IOperation> operations;
Dictionary<FloatBox, IOperation> operations2;
public TempMeter()
{
}
public TempMeter(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
tempMtrCfg = cfg as TempMeterCfg;
Multimeter = (Multimeter)TbfComponents.FindComponent(cfg.ParentName, components);
if (Multimeter == null) throw new Exception("Cannot find " + Name + " parent");
operations = new Dictionary<FloatBox, IOperation>();
operations2 = new Dictionary<FloatBox, IOperation>();
log.Debug(this.ToString());
}
public float ReadTemperature()
{
double resistance = Multimeter.ReadResistance(tempMtrCfg.Channel);
double temperature = 0;
if (resistance > 0 && resistance < 2000)
{
if (tempMtrCfg.UseITS90)
{
temperature = Formulas.PlatinumResistanceTM_ITS90_R2T(resistance,
tempMtrCfg.R001C,
tempMtrCfg.a7,
tempMtrCfg.b7,
tempMtrCfg.c7);
}
else
{
temperature = Formulas.PlatinumResistanceTM_ITS27_R2T(resistance,
tempMtrCfg.R0,
tempMtrCfg.a,
tempMtrCfg.b);
}
log.DebugFormat("Ch = {0}, R = {1} Ohm, T = {2} °C", Channel, resistance.ToString("F3"), temperature.ToString("F3"));
}
return (float)temperature;
}
/// <summary>
/// Events: TempDone, Error
/// </summary>
/// <param name="temp">Reference to a variable for the temperature in Celsius</param>
/// <returns>ReadTempOp instance reference casted to IOperaton</returns>
public IOperation ReadTempOp(ref FloatBox temp)
{
IOperation operation;
if (operations.TryGetValue(temp, out operation))
{
return operation; /// Operation already available
}
else
{
/// Create a new operation and store it in the dictionary
operation = new ReadTempOp(this, ref temp);
operations.Add(temp, operation);
return operation;
}
}
/// <summary>
/// Events: tempDone, Error
/// </summary>
/// <param name="temp">Reference to a variable for the temperature in Celsius</param>
/// <param name="tempDone">Event returned when measurement done</param>
/// <returns>ReadTempOp instance reference casted to IOperaton</returns>
public IOperation ReadTempOp(ref FloatBox temp, Event tempDone)
{
IOperation operation;
if (operations2.TryGetValue(temp, out operation))
{
return operation; /// Operation already available
}
else
{
/// Create a new operation and store it in the dictionary
operation = new ReadTempOp(this, ref temp, tempDone);
operations2.Add(temp, operation);
return operation;
}
}
}
}