155 lines
4.3 KiB
C#
155 lines
4.3 KiB
C#
///
|
|
/// Copyright (c) 2017 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using log4net;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.Modbus.Easytherm
|
|
{
|
|
public class Easytherm : ComponentBase, IDevice
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Easytherm));
|
|
public override string ToString() { return string.Format("Easytherm({0})", Cfg.ToString(1)); }
|
|
|
|
readonly EasythermCfg easythermCfg;
|
|
readonly GenericDevices.IModbus modbus;
|
|
|
|
private float temperatureSetPoint;
|
|
private bool temperatureSetPointValid;
|
|
|
|
public float requiredTemp { get { return easythermCfg.ProcParams.RequiredTemp; } }
|
|
|
|
|
|
public Easytherm()
|
|
{
|
|
}
|
|
|
|
public Easytherm(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
easythermCfg = cfg as EasythermCfg;
|
|
|
|
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
temperatureSetPointValid = false;
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
/// <summary>Initialize this device</summary>
|
|
public void Initialize()
|
|
{
|
|
}
|
|
|
|
/// <summary>Run this device</summary>
|
|
public void RunDeviceBefore()
|
|
{
|
|
}
|
|
|
|
/// <summary>Run this device</summary>
|
|
public void RunDeviceAfter()
|
|
{
|
|
}
|
|
|
|
/// <summary>Stop this device</summary>
|
|
public void StopDevice()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Easytherm controller to required temperature procedure parameter
|
|
/// </summary>
|
|
/// <returns>true = OK, false = any error</returns>
|
|
public bool SetRequiredTemperature()
|
|
{
|
|
return SetTemperature(requiredTemp);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set Easytherm controller to specified temperature
|
|
/// </summary>
|
|
/// <param name="temperature">Temperature in °C</param>
|
|
/// <returns>true = OK, false = any error</returns>
|
|
public bool SetTemperature(float temperature)
|
|
{
|
|
if (temperature < 0 || temperature > 100.0f)
|
|
{
|
|
return false; /// required temperature out of range
|
|
}
|
|
|
|
if (temperatureSetPointValid && (temperature == temperatureSetPoint))
|
|
{
|
|
return true; /// already set to the required temperature
|
|
}
|
|
|
|
temperatureSetPoint = temperature;
|
|
temperatureSetPointValid = true;
|
|
|
|
//ushort temperatureSetpoint = (ushort)temperature; // in °C
|
|
//ushort destAddress = (ushort)1002;
|
|
ushort temperatureSetpoint = (ushort)((10000.0f / 400.0f) * temperature); // in 0.01% of range (400 °C)
|
|
ushort destAddress = (ushort)2;
|
|
|
|
byte[] msg = new byte[8];
|
|
|
|
msg[0] = easythermCfg.ModbusAddress;
|
|
msg[1] = (byte)Function.PresetSingleRegister;
|
|
msg[2] = (byte)(destAddress >> 8); /// Relative address Hi
|
|
msg[3] = (byte)(destAddress & 0xFF); /// Relative address Lo
|
|
msg[4] = (byte)(temperatureSetpoint >> 8); /// Output data Hi
|
|
msg[5] = (byte)(temperatureSetpoint & 0xFF); /// Output data Lo
|
|
|
|
modbus.SendMessage(msg);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Set outputs of QuidoRS board
|
|
/// </summary>
|
|
/// <param name="temperature">output value</param>
|
|
public void GetSomething(ushort address, int len)
|
|
{
|
|
ReadSomething(address, len);
|
|
}
|
|
|
|
private void ReadSomething(ushort address, int len)
|
|
{
|
|
byte[] msg = new byte[8];
|
|
|
|
msg[0] = easythermCfg.ModbusAddress;
|
|
msg[1] = (byte)Function.ReadInputRegister;
|
|
msg[2] = (byte)(address >> 8); /// Relative address Hi
|
|
msg[3] = (byte)(address & 0xFF); /// Relative address Lo
|
|
msg[4] = (byte)((len >> 8) & 0xFF); /// Data length Hi
|
|
msg[5] = (byte)(len & 0xFF); /// Data length Lo
|
|
|
|
modbus.SendMessage(msg);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Events: SetOpututsDone, Error
|
|
/// </summary>
|
|
/// <returns>SetOutputsOp instance reference casted to IOperaton</returns>
|
|
public IOperation SetRequiredTemperatureOp()
|
|
{
|
|
return new SetRequiredTemperatureOp(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: SetOpututsDone, Error
|
|
/// </summary>
|
|
/// <param name="temperature">Reference to a variable for the pressure in Bar</param>
|
|
/// <returns>SetOutputsOp instance reference casted to IOperaton</returns>
|
|
public IOperation SetTemperatureOp(float temperature)
|
|
{
|
|
return new SetTemperatureOp(this, temperature);
|
|
}
|
|
}
|
|
}
|