2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using log4net;
|
|
|
|
|
|
using TBF.BenchControl.Generic;
|
|
|
|
|
|
using TBF.Boxes;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Danfoss.VLT2800
|
|
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// <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,
|
|
|
|
|
|
}
|
2015-04-17 13:05:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
public class Pump : ComponentBase, IDevice, IOperation, GenericDevices.IPumpFM, GenericDevices.IValve
|
|
|
|
|
|
{
|
2015-02-16 12:36:22 +00:00
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Pump));
|
|
|
|
|
|
public override string ToString() { return string.Format("Pump-Danfoss-VLT2800({0})", Cfg.ToString(1)); }
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
private readonly PumpCfg pumpCfg;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
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; } }
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-05-15 14:44:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Pump power in [%] in range 0 .. 100.0f
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private float power;
|
|
|
|
|
|
public float Power
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return power; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
readonly Elde.ControlBoardDev controlBoard;
|
|
|
|
|
|
readonly int bitPosition; /// 0 .. 63
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
public readonly ulong Mask; /// derived from bitPosition in the constructor
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
public bool State
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (controlBoard.Route & Mask) != 0; }
|
|
|
|
|
|
}
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// Private fields
|
|
|
|
|
|
SerialPort serialPort;
|
|
|
|
|
|
bool dataToSendReady;
|
|
|
|
|
|
byte[] dataToSend;
|
|
|
|
|
|
StringBuilder responseBuilder;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <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(PumpCfg cfg, IList<Generic.IComponent> components)
|
2015-02-16 20:01:37 +00:00
|
|
|
|
: base(cfg)
|
|
|
|
|
|
{
|
|
|
|
|
|
pumpCfg = cfg;
|
|
|
|
|
|
|
|
|
|
|
|
controlBoard = (Elde.ControlBoardDev)TbfComponents.FindComponent("CB", components); /// TODO: replace "CB"
|
|
|
|
|
|
if (controlBoard == null) throw new Exception("Cannot find " + Name + " parent");
|
|
|
|
|
|
|
2015-02-18 21:50:44 +00:00
|
|
|
|
bitPosition = pumpCfg.BitPosition;
|
2015-02-16 20:01:37 +00:00
|
|
|
|
Mask = (((ulong)1) << bitPosition);
|
2015-02-20 10:37:09 +00:00
|
|
|
|
Mask |= (((ulong)1) << (bitPosition + 1));
|
2015-02-16 20:01:37 +00:00
|
|
|
|
|
2015-02-16 12:36:22 +00:00
|
|
|
|
log.Debug(this.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize()
|
|
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
if (pumpCfg.DebugLevel == Entities.DebugMode.Simulate)
|
2015-02-16 12:36:22 +00:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
string comPortName = "COM" + pumpCfg.ComPortNr.ToString();
|
|
|
|
|
|
serialPort = new SerialPort(comPortName, pumpCfg.BaudRate, pumpCfg.Parity, pumpCfg.DataBits, pumpCfg.StopBits);
|
|
|
|
|
|
serialPort.Handshake = Handshake.None;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
serialPort.Open();
|
2015-02-16 20:01:37 +00:00
|
|
|
|
responseBuilder = new StringBuilder(40);
|
|
|
|
|
|
|
|
|
|
|
|
log.FatalFormat("Successfully initialized device {0}", ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceBefore()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pumpCfg.DebugLevel == Entities.DebugMode.Simulate) return;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
responseBuilder.Append(serialPort.ReadExisting());
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// Check if the response is complete
|
|
|
|
|
|
if (responseBuilder.Length >= 8)
|
2015-02-16 12:36:22 +00:00
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
string response = responseBuilder.ToString();
|
|
|
|
|
|
responseBuilder.Clear();
|
|
|
|
|
|
|
2015-02-18 17:05:06 +00:00
|
|
|
|
log.Info(Telegram.LogTelegram("Received ", response));
|
2015-02-16 12:36:22 +00:00
|
|
|
|
}
|
2015-02-16 20:01:37 +00:00
|
|
|
|
}
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Prepares data to be sent in RunDeviceAfter() function
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
bool SendData(byte[] data)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dataToSendReady) return false;
|
|
|
|
|
|
dataToSend = data;
|
|
|
|
|
|
dataToSendReady = true;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
|
2015-02-18 17:05:06 +00:00
|
|
|
|
log.Info(Telegram.LogTelegram("Sending ", data));
|
2015-02-16 20:01:37 +00:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceAfter()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dataToSendReady)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (pumpCfg.DebugLevel != Entities.DebugMode.Simulate)
|
2015-02-16 12:36:22 +00:00
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
serialPort.Write(dataToSend, 0, dataToSend.Length);
|
2015-02-16 12:36:22 +00:00
|
|
|
|
}
|
2015-02-16 20:01:37 +00:00
|
|
|
|
dataToSendReady = false;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
}
|
2015-02-16 20:01:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Stop this device</summary>
|
|
|
|
|
|
public void StopDevice()
|
2015-02-16 12:36:22 +00:00
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
if (serialPort != null) serialPort.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
|
|
2015-05-15 14:44:13 +00:00
|
|
|
|
this.power = powerArg;
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
//UInt16 controlWord = 0x047C;
|
|
|
|
|
|
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
|
|
|
|
|
CW.Not_CoastingStop |
|
|
|
|
|
|
CW.Not_QuickStop |
|
|
|
|
|
|
CW.Not_FreezeFreq |
|
|
|
|
|
|
CW.Start_NotRampStop |
|
|
|
|
|
|
CW.DataValid);
|
|
|
|
|
|
|
|
|
|
|
|
UInt16 serialComReference = (UInt16)(Int16)((float)0x4000 * powerArg / 100.0f + 0.5f);
|
|
|
|
|
|
|
2015-02-18 21:50:44 +00:00
|
|
|
|
byte[] msg;
|
|
|
|
|
|
if (pumpCfg.Protocol == PumpProtocol.Modbus)
|
|
|
|
|
|
{
|
|
|
|
|
|
msg = new byte[13];
|
|
|
|
|
|
msg[0] = (byte)pumpCfg.BusAddress;
|
|
|
|
|
|
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)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
|
|
|
|
|
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
|
|
|
|
|
Telegram.UpdateTelegramCRC(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
else /// if (pumpCfg.Protocol == PumpProtocol.FC)
|
|
|
|
|
|
{
|
|
|
|
|
|
msg = new byte[8];
|
|
|
|
|
|
msg[0] = 0x02; /// STX
|
|
|
|
|
|
msg[1] = 6; /// lenght (6 bytes following this one)
|
|
|
|
|
|
msg[2] = (byte)pumpCfg.BusAddress;
|
|
|
|
|
|
msg[3] = (byte)(controlWord >> 8); /// Control Word Hi
|
|
|
|
|
|
msg[4] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
|
|
|
|
|
msg[5] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
|
|
|
|
|
msg[6] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
|
|
|
|
|
Telegram.UpdateTelegramChecksum(msg);
|
|
|
|
|
|
}
|
2015-02-16 20:01:37 +00:00
|
|
|
|
SendData(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Turn the pmp off using Modbus command
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void TurnOff()
|
|
|
|
|
|
{
|
|
|
|
|
|
log.DebugFormat("{0}.TurnOff()", Name);
|
|
|
|
|
|
|
2015-05-15 14:44:13 +00:00
|
|
|
|
this.power = 0;
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
UInt16 controlWord = (UInt16)(CW.Not_DcBraking |
|
|
|
|
|
|
CW.Not_CoastingStop |
|
|
|
|
|
|
CW.Not_QuickStop |
|
|
|
|
|
|
CW.Not_FreezeFreq |
|
|
|
|
|
|
//CW.Start_NotRampStop |
|
|
|
|
|
|
CW.DataValid);
|
|
|
|
|
|
|
|
|
|
|
|
UInt16 serialComReference = 0;
|
|
|
|
|
|
|
2015-02-18 21:50:44 +00:00
|
|
|
|
byte[] msg;
|
|
|
|
|
|
if (pumpCfg.Protocol == PumpProtocol.Modbus)
|
|
|
|
|
|
{
|
|
|
|
|
|
msg = new byte[13];
|
|
|
|
|
|
msg[0] = (byte)pumpCfg.BusAddress;
|
|
|
|
|
|
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)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
|
|
|
|
|
msg[10] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
|
|
|
|
|
Telegram.UpdateTelegramCRC(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
else /// if (pumpCfg.Protocol == PumpProtocol.FC)
|
|
|
|
|
|
{
|
|
|
|
|
|
msg = new byte[8];
|
|
|
|
|
|
msg[0] = 0x02; /// STX
|
|
|
|
|
|
msg[1] = 6; /// lenght (6 bytes following this one)
|
|
|
|
|
|
msg[2] = (byte)pumpCfg.BusAddress;
|
|
|
|
|
|
msg[3] = (byte)(controlWord >> 8); /// Control Word Hi
|
|
|
|
|
|
msg[4] = (byte)(controlWord & 0xFF); /// Control Word Lo
|
|
|
|
|
|
msg[5] = (byte)(serialComReference >> 8); /// Serial Com Reference Hi
|
|
|
|
|
|
msg[6] = (byte)(serialComReference & 0xFF); /// Serial Com Reference Lo
|
|
|
|
|
|
Telegram.UpdateTelegramChecksum(msg);
|
|
|
|
|
|
}
|
2015-02-16 20:01:37 +00:00
|
|
|
|
SendData(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;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// <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>
|
2015-02-16 12:36:22 +00:00
|
|
|
|
public void Start()
|
|
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
switch (currentOp)
|
|
|
|
|
|
{
|
|
|
|
|
|
case CurrentOp.TurnOn:
|
|
|
|
|
|
TurnOn(powerForOp);
|
|
|
|
|
|
return;
|
|
|
|
|
|
case CurrentOp.TurnOff:
|
|
|
|
|
|
default:
|
|
|
|
|
|
TurnOff();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run the operation</summary>
|
2015-02-16 12:36:22 +00:00
|
|
|
|
public Event Run()
|
|
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
return Event.TurnPumpOnOffDone;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-16 20:01:37 +00:00
|
|
|
|
/// <summary>Stop the operation</summary>
|
2015-02-16 12:36:22 +00:00
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
2015-02-16 20:01:37 +00:00
|
|
|
|
currentOp = CurrentOp.None;
|
2015-02-16 12:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|