2018-09-14 10:13:15 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2018 Sensus Slovensko a.s.
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using Config.Entities;
|
|
|
|
|
|
using log4net;
|
|
|
|
|
|
using TBF.BenchControl.Generic;
|
2018-10-24 14:28:56 +00:00
|
|
|
|
using TBF.BenchControl.GenericDevices;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
using TBF.Boxes;
|
2018-10-24 14:28:56 +00:00
|
|
|
|
using TBF.BenchControl.Hart.Common;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
namespace TBF.BenchControl.Hart.Nivotrack
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Root component for Modbus communication via serial port (RS485)
|
|
|
|
|
|
/// </summary>
|
2018-10-25 15:34:43 +00:00
|
|
|
|
public class Nivotrack : ComponentBase, IDevice, ILevelMeter, IOperation
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
|
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(Nivotrack));
|
2018-11-12 17:26:23 +00:00
|
|
|
|
public override string ToString() { return string.Format("{0}({1})", Cfg.Name, Cfg.ToString(-1)); }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
private readonly NivotrackCfg nivotrackCfg;
|
|
|
|
|
|
private readonly IHart hart;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
bool readingValid;
|
2018-11-12 17:26:23 +00:00
|
|
|
|
double rawLevel_mm;
|
|
|
|
|
|
public double Level { get { return ReadLevel(); } }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
DoubleBox levelBox;
|
2018-11-13 12:51:42 +00:00
|
|
|
|
double sdev;
|
2018-10-24 14:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
enum CurrentOp
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
None,
|
|
|
|
|
|
ReadLevel,
|
2018-11-13 12:51:42 +00:00
|
|
|
|
ReadStableLevel,
|
|
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
CurrentOp currentOp;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Nivotrack()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public Nivotrack(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
|
2018-09-14 10:13:15 +00:00
|
|
|
|
: base(cfg)
|
|
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
nivotrackCfg = cfg as NivotrackCfg;
|
|
|
|
|
|
|
|
|
|
|
|
hart = (IHart)TbfComponents.FindComponent(cfg.ParentName, components);
|
|
|
|
|
|
if (hart == null) throw new Exception("Cannot find " + Name + " parent");
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
log.Debug(this.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public void Initialize()
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if (nivotrackCfg.DebugLevel == DebugMode.Simulate) return;
|
|
|
|
|
|
|
2018-11-12 17:26:23 +00:00
|
|
|
|
rawLevel_mm = 0;
|
2018-10-24 14:28:56 +00:00
|
|
|
|
readingValid = false;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
void Detect()
|
|
|
|
|
|
{
|
|
|
|
|
|
byte bcnt = (byte)0; /// block count
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = new byte[5];
|
|
|
|
|
|
data[0] = (byte)Mark.ShortFrameMaster2Slave;
|
|
|
|
|
|
data[1] = (byte)((int)Master.Primary + (byte)nivotrackCfg.Address);
|
|
|
|
|
|
data[2] = (byte)Cmd.ReadUniqueID;
|
|
|
|
|
|
data[3] = bcnt;
|
|
|
|
|
|
data[4] = 0; /// reserved for chehcksum
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
hart.SendMessage(5, data);
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
void RequestProcessValue()
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte bcnt = (byte)0; /// block count
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = new byte[5];
|
|
|
|
|
|
data[0] = (byte)Mark.ShortFrameMaster2Slave;
|
|
|
|
|
|
data[1] = (byte)((int)Master.Primary + (byte)nivotrackCfg.Address);
|
|
|
|
|
|
data[2] = (byte)Cmd.ReadPrimaryVar;
|
|
|
|
|
|
data[3] = bcnt;
|
|
|
|
|
|
data[4] = 0; /// reserved for chehcksum
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
hart.SendMessage(5, data);
|
|
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceBefore()
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if (nivotrackCfg.DebugLevel == DebugMode.Simulate) return;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if (hart.ReceivedTelegrams[nivotrackCfg.Address].Count > 0)
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
byte[] data = hart.ReceivedTelegrams[nivotrackCfg.Address].Dequeue();
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if ((data.Length == 12) && (data[0] == (byte)Mark.ShortFrameSlave2Master)
|
|
|
|
|
|
&& (data[2] == (byte)Cmd.ReadPrimaryVar)
|
|
|
|
|
|
&& (data[3] == 7))
|
|
|
|
|
|
{
|
|
|
|
|
|
///
|
|
|
|
|
|
/// A frame with a status and a primary variable
|
|
|
|
|
|
///
|
|
|
|
|
|
ushort state = (ushort)(256 * data[4] + data[5]);
|
|
|
|
|
|
byte unitsID = data[6];
|
2018-11-12 17:26:23 +00:00
|
|
|
|
byte[] floatData = new byte[] { data[10], data[9], data[8], data[7] };
|
|
|
|
|
|
rawLevel_mm = System.BitConverter.ToSingle(floatData, 0);
|
2018-10-24 14:28:56 +00:00
|
|
|
|
readingValid = true;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-11-12 17:26:23 +00:00
|
|
|
|
string msg = string.Format("{0} : Received water level = {1} mm, State = {2}", Name, rawLevel_mm, state.ToString("X4"));
|
2018-10-24 14:28:56 +00:00
|
|
|
|
Debug.WriteLine(msg);
|
|
|
|
|
|
log.Info(msg);
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
else if ((data.Length == 7) && (data[0] == (byte)Mark.ShortFrameSlave2Master)
|
|
|
|
|
|
&& (data[2] == (byte)Cmd.ReadPrimaryVar)
|
|
|
|
|
|
&& (data[3] == 2))
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// A frame with a status only
|
|
|
|
|
|
///
|
|
|
|
|
|
ushort state = (ushort)(256 * data[4] + data[5]);
|
|
|
|
|
|
string msg = string.Format("{0} : State = {1}", Name, state.ToString("X4"));
|
|
|
|
|
|
Debug.WriteLine(msg);
|
|
|
|
|
|
log.Info(msg);
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public void RunDeviceAfter()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nivotrackCfg.DebugLevel == DebugMode.Simulate) return;
|
2018-09-14 10:13:15 +00:00
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
if ((StateMachine.Time % 2) == (nivotrackCfg.Address % 2))
|
2018-09-14 10:13:15 +00:00
|
|
|
|
{
|
2018-10-24 14:28:56 +00:00
|
|
|
|
RequestProcessValue();
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public void StopDevice() { }
|
2018-09-14 10:13:15 +00:00
|
|
|
|
public void StopDevice2() { }
|
2018-10-24 14:28:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2018-11-12 17:26:23 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the most recent raw level measurement and applies correction table
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2018-10-24 14:28:56 +00:00
|
|
|
|
public double ReadLevel()
|
|
|
|
|
|
{
|
2018-11-12 17:26:23 +00:00
|
|
|
|
return Config.Formulas.CorrectedValue(rawLevel_mm, Corrections);
|
2018-10-24 14:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <returns>Reference to the operation</returns>
|
|
|
|
|
|
public IOperation ReadLevelOp(ref DoubleBox levelBox)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
|
|
|
|
this.levelBox = levelBox;
|
|
|
|
|
|
currentOp = CurrentOp.ReadLevel;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-13 12:51:42 +00:00
|
|
|
|
/// <returns>Reference to the operation</returns>
|
|
|
|
|
|
public IOperation ReadStableLevelOp(ref DoubleBox levelBox, double sdev)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
|
|
|
|
|
|
this.levelBox = levelBox;
|
|
|
|
|
|
this.sdev = sdev;
|
|
|
|
|
|
currentOp = CurrentOp.ReadStableLevel;
|
|
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-24 14:28:56 +00:00
|
|
|
|
/// <summary>Start this operation</summary>
|
|
|
|
|
|
public void Start()
|
|
|
|
|
|
{
|
2018-11-12 17:26:23 +00:00
|
|
|
|
if (readingValid && (levelBox != null))
|
|
|
|
|
|
{
|
|
|
|
|
|
levelBox.Val = ReadLevel();
|
|
|
|
|
|
}
|
2018-10-24 14:28:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this operation</summary>
|
|
|
|
|
|
/// <returns>Event.ResultsPrinted</returns>
|
|
|
|
|
|
public Event Run()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (readingValid && (levelBox != null))
|
|
|
|
|
|
{
|
|
|
|
|
|
levelBox.Val = ReadLevel();
|
|
|
|
|
|
return Event.LevelDone;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return Event.Busy;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Stop this operation</summary>
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
currentOp = CurrentOp.None;
|
|
|
|
|
|
}
|
2018-09-14 10:13:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|