290 lines
8.5 KiB
C#
290 lines
8.5 KiB
C#
///
|
|
/// Copyright (c) 2020 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.Text;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using Dirichlet.Numerics;
|
|
using TBF.BenchControl.ControlBoard;
|
|
using TBF.BenchControl.Generic;
|
|
|
|
namespace TBF.BenchControl.Modbus.Danfoss.VLT
|
|
{
|
|
/// <summary>
|
|
/// See page 20 of MG10S202 Operational Instructions Modbus RTU
|
|
/// </summary>
|
|
public enum CW : ushort
|
|
{
|
|
PresetRefLsb = (1 << 0),
|
|
PresetRefMsb = (1 << 1),
|
|
Not_DcBraking = (1 << 2),
|
|
Not_CoastingStop = (1 << 3),
|
|
Not_QuickStop = (1 << 4),
|
|
Not_FreezeFreq = (1 << 5),
|
|
Start_NotRampStop = (1 << 6),
|
|
Reset = (1 << 7),
|
|
Jog = (1 << 8),
|
|
Ramp2_NotRamp1 = (1 << 9),
|
|
DataValid = (1 << 10),
|
|
Relay01Activ = (1 << 11),
|
|
DOut46Activ = (1 << 12),
|
|
SelectSetupLsb = (1 << 13),
|
|
SelectSetupMsb = (1 << 14),
|
|
Reversing = (1 << 15),
|
|
}
|
|
|
|
/// <summary>
|
|
/// See page 22 of MG10S202 Operational Instructions Modbus RTU
|
|
/// </summary>
|
|
public enum SW : ushort
|
|
{
|
|
ControlReady = (1 << 0),
|
|
DriveReady = (1 << 1),
|
|
CoastingStop = (1 << 2),
|
|
Trip = (1 << 3),
|
|
TripLock = (1 << 6),
|
|
Warning = (1 << 7),
|
|
SpeedEqRef = (1 << 8),
|
|
RemoteCtrl = (1 << 9),
|
|
FreqLimitOK = (1 << 10),
|
|
MotorRunning = (1 << 11),
|
|
VoltageWarn = (1 << 13),
|
|
CurrentLimit = (1 << 14),
|
|
ThermalWarn = (1 << 15),
|
|
}
|
|
|
|
public enum Function : byte
|
|
{
|
|
ReadCoilStatus = 0x01,
|
|
ForceSingleCoil = 0x05,
|
|
ForceMultipleCoils = 0x0F,
|
|
ReadHoldingRegisters = 0x03,
|
|
PresetSingleRegister = 0x06,
|
|
PresetMultipleRegisters = 0x10,
|
|
}
|
|
|
|
|
|
public class Pump : ComponentBase, IDevice, IOperation, GenericDevices.IPumpFM, GenericDevices.IValve
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Pump));
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(1)); }
|
|
|
|
private readonly PumpCfg pumpCfg;
|
|
|
|
public int Delay { get { return pumpCfg.Delay; } }
|
|
public ValveCategory Category { get { return ValveCategory.Feeding; } } /// Pump category is Feeding
|
|
public GenericDevices.IValve CoupledTo { get { return null; } }
|
|
public bool InvertCouple { get { return false; } }
|
|
public int LagOpening { get { return 0; } }
|
|
public int LagClosing { get { return 0; } }
|
|
|
|
/// <summary>
|
|
/// Pump power in [%] in range 0 .. 100.0f
|
|
/// </summary>
|
|
float power;
|
|
public float Power { get { return power; } }
|
|
|
|
|
|
readonly GenericDevices.IModbus modbus;
|
|
readonly IControlBoard controlBoard;
|
|
readonly int bitPosition; /// 0 .. 63
|
|
public readonly UInt128 Mask; /// derived from bitPosition in the constructor
|
|
|
|
|
|
public bool State
|
|
{
|
|
get { return (controlBoard.Route & Mask) != 0; }
|
|
}
|
|
|
|
|
|
public Pump() { }
|
|
|
|
/// <summary>
|
|
/// Ambient temperature / humidity / pressure meter 'Greco' connected via serial interface (RS232)
|
|
/// Connection settings: 19200 Bd 8-bits No-parity 1-stop-bit Flow control: none or hardware.
|
|
/// </summary>
|
|
public Pump(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
|
: base(cfg)
|
|
{
|
|
pumpCfg = cfg as PumpCfg;
|
|
|
|
modbus = (GenericDevices.IModbus)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
if (modbus == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
controlBoard = TbfComponents.FindComponent("CB", components) as IControlBoard; /// TODO: replace "CB"
|
|
if (controlBoard == null) throw new Exception("Cannot find a control board");
|
|
|
|
bitPosition = pumpCfg.BitPosition;
|
|
Mask = (((UInt128)1) << bitPosition);
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
///
|
|
/// IDevice interface
|
|
///
|
|
public void Initialize() { }
|
|
|
|
public void RunDeviceBefore()
|
|
{
|
|
if (pumpCfg.DebugLevel == Config.Entities.DebugMode.Normal ||
|
|
pumpCfg.DebugLevel == Config.Entities.DebugMode.DetectedOn)
|
|
{
|
|
if (modbus.ReceivedTelegrams[pumpCfg.ModbusAddress].Count > 0)
|
|
{
|
|
byte[] telegram = modbus.ReceivedTelegrams[pumpCfg.ModbusAddress].Dequeue();
|
|
string s = Telegram.LogTelegram(string.Format("Telegram received from {0}: ", Name), telegram);
|
|
log.Warn(s);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RunDeviceAfter() { }
|
|
public void StopDevice() { }
|
|
public void StopDevice2() { }
|
|
|
|
|
|
/// <summary>
|
|
/// Turn the pmp on using Modbus commad, use the variable frequency
|
|
/// </summary>
|
|
/// <param name="powerArg">Frequency in percent (-200.0 .. 200.0)</param>
|
|
public void TurnOn(float powerArg)
|
|
{
|
|
log.DebugFormat("{0}.TurnOn({1})", Name, powerArg);
|
|
|
|
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
|
CW.Not_CoastingStop |
|
|
CW.Not_QuickStop |
|
|
CW.Not_FreezeFreq |
|
|
CW.Start_NotRampStop |
|
|
CW.DataValid);
|
|
this.power = powerArg;
|
|
UInt16 reference = (UInt16)(Int16)((float)0x4000 * powerArg / 100.0f + 0.5f);
|
|
|
|
if (pumpCfg.DebugLevel == Config.Entities.DebugMode.Normal ||
|
|
pumpCfg.DebugLevel == Config.Entities.DebugMode.DetectedOn)
|
|
{
|
|
byte[] msg = new byte[13];
|
|
msg[0] = (byte)pumpCfg.ModbusAddress;
|
|
msg[1] = (byte)Function.ForceMultipleCoils;
|
|
msg[2] = 0; /// Data Hi
|
|
msg[3] = 0; /// Data Lo
|
|
msg[4] = 0; /// Nr. of coils Hi
|
|
msg[5] = 0x20; /// Nr. of coils Lo
|
|
msg[6] = 4; /// Byte count
|
|
msg[7] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
|
msg[8] = (byte)(controlWord >> 8); /// Control Word Hi
|
|
msg[9] = (byte)(reference & 0xFF); /// Serial Com Reference Lo
|
|
msg[10] = (byte)(reference >> 8); /// Serial Com Reference Hi
|
|
///
|
|
modbus.SendMessage(msg);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Turn the pmp off using Modbus command
|
|
/// </summary>
|
|
public void TurnOff()
|
|
{
|
|
log.DebugFormat("{0}.TurnOff()", Name);
|
|
|
|
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
|
CW.Not_CoastingStop |
|
|
CW.Not_QuickStop |
|
|
CW.Not_FreezeFreq |
|
|
//CW.Start_NotRampStop |
|
|
CW.DataValid);
|
|
this.power = 0;
|
|
UInt16 reference = 0;
|
|
|
|
if (pumpCfg.DebugLevel == Config.Entities.DebugMode.Normal ||
|
|
pumpCfg.DebugLevel == Config.Entities.DebugMode.DetectedOn)
|
|
{
|
|
byte[] msg = new byte[13];
|
|
msg[0] = (byte)pumpCfg.ModbusAddress;
|
|
msg[1] = (byte)Function.ForceMultipleCoils;
|
|
msg[2] = 0; /// Data Hi
|
|
msg[3] = 0; /// Data Lo
|
|
msg[4] = 0; /// Nr. of coils Hi
|
|
msg[5] = 0x20; /// Nr. of coils Lo
|
|
msg[6] = 4; /// Byte count
|
|
msg[7] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
|
msg[8] = (byte)(controlWord >> 8); /// Control Word Hi
|
|
msg[9] = (byte)(reference & 0xFF); /// Serial Com Reference Lo
|
|
msg[10] = (byte)(reference >> 8); /// Serial Com Reference Hi
|
|
Telegram.UpdateTelegramCRC(msg);
|
|
|
|
modbus.SendMessage(msg);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// The currently running operation.
|
|
/// </summary>
|
|
public enum CurrentOp
|
|
{
|
|
None,
|
|
TurnOn,
|
|
TurnOff,
|
|
}
|
|
///
|
|
CurrentOp currentOp;
|
|
|
|
float powerForOp;
|
|
|
|
/// <summary>
|
|
/// Turn the pump frequency inverter on.
|
|
/// </summary>
|
|
public IOperation TurnOnOp(float powerArg)
|
|
{
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
currentOp = CurrentOp.TurnOn;
|
|
powerForOp = powerArg;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Turn the pump frequency inverter on.
|
|
/// </summary>
|
|
public IOperation TurnOffOp()
|
|
{
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
currentOp = CurrentOp.TurnOff;
|
|
return this;
|
|
}
|
|
|
|
/// <summary>Start the operation</summary>
|
|
public void Start()
|
|
{
|
|
switch (currentOp)
|
|
{
|
|
case CurrentOp.TurnOn:
|
|
TurnOn(powerForOp);
|
|
return;
|
|
|
|
case CurrentOp.TurnOff:
|
|
default:
|
|
TurnOff();
|
|
return;
|
|
}
|
|
}
|
|
|
|
/// <summary>Run the operation</summary>
|
|
public Event Run()
|
|
{
|
|
return Event.TurnPumpOnOffDone;
|
|
}
|
|
|
|
/// <summary>Stop the operation</summary>
|
|
public void Stop()
|
|
{
|
|
currentOp = CurrentOp.None;
|
|
}
|
|
}
|
|
}
|