tbf/TestBenchFramework/BenchControl/Modbus/Easytherm/Easytherm.cs
2017-03-06 00:33:44 +01:00

126 lines
3.5 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 ushort temperatureSetpoint;
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");
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 outputs of QuidoRS board
/// </summary>
/// <param name="temperature">output value</param>
public void SetTemperature(float temperature)
{
if (temperature < 0 || temperature > 100) return; /// temperature out of range
ushort temperatureSetpoint = (ushort)(100 * temperature);
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);
}
/// <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>
/// <param name="temperatureSetpoint">Reference to a variable for the pressure in Bar</param>
/// <returns>SetOutputsOp instance reference casted to IOperaton</returns>
public IOperation SetTemperatureOp(ushort temperatureSetpoint)
{
return new SetTemperatureOp(this, temperatureSetpoint);
}
/// <summary>
/// Events: pressureDone, Error
/// </summary>
/// <param name="outValue">Reference to a variable for the pressure in Bar</param>
/// <param name="setOutputsDone">Event returned when SetOutput is done</param>
/// <returns>SetOutputsOp instance reference casted to IOperaton</returns>
public IOperation ReadTemperatureOp()
{
return new ReadTemperatureOp(this);
}
}
}