2016-04-20 13:58:48 +00:00
|
|
|
|
///
|
2021-02-15 10:02:28 +00:00
|
|
|
|
/// Copyright (c) 2016-2021 Sensus Slovensko a.s.
|
2016-04-20 13:58:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
|
using log4net;
|
2021-09-23 09:46:40 +00:00
|
|
|
|
using Common;
|
2016-04-20 13:58:48 +00:00
|
|
|
|
using Config.Entities;
|
2021-10-26 09:23:57 +00:00
|
|
|
|
using TBF.Rig.Generic;
|
2016-04-20 13:58:48 +00:00
|
|
|
|
|
2021-10-26 09:23:57 +00:00
|
|
|
|
namespace TBF.Rig.MettlerToledo.Standard
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Mettler Toledo ICS4_5 Weighting terminal connected via serial interface (RS232)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <note>
|
|
|
|
|
|
/// Implemented and tested on 15.10.2013 by Milan Hanajik
|
|
|
|
|
|
/// </note>
|
2017-03-28 21:47:32 +00:00
|
|
|
|
public class BalanceOld : BalanceDev, IDevice, GenericDevices.IScale
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Info: StartMassMeasurement() and "Balnce response = .., Mass = ..."
|
|
|
|
|
|
/// Warn: Start measurement when busy is true
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(BalanceOld));
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
2016-04-20 13:58:48 +00:00
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public BalanceOld() { }
|
2016-04-20 13:58:48 +00:00
|
|
|
|
|
2016-08-05 12:32:36 +00:00
|
|
|
|
public BalanceOld(Generic.IComponentCfg cfg)
|
2016-04-20 13:58:48 +00:00
|
|
|
|
: base(cfg)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public override void SendZeroWhenStableCmd()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.SendTaraCmd();
|
2016-04-20 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
|
2016-04-20 13:58:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse characters received from the serial port
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override void RunDeviceBefore()
|
|
|
|
|
|
{
|
2017-03-27 21:05:02 +00:00
|
|
|
|
base.RunBefore();
|
|
|
|
|
|
|
2016-04-20 13:58:48 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate)
|
|
|
|
|
|
{
|
|
|
|
|
|
mass = TestBenchSim.GetSimMass(balanceNr);
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
msrmntTimeStamp = StateMachine.Time;
|
|
|
|
|
|
return;
|
2017-02-16 08:55:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - RunDeviceBefore() - serial port closed", Name);
|
2017-02-16 08:55:49 +00:00
|
|
|
|
return;
|
2016-04-20 13:58:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.DebugFormat("{0} - RunDeviceBefore()", Name);
|
2016-04-20 13:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
if (base.msrmntState != MsrmntState.Busy) return; /// No measurement in progress
|
2020-10-29 09:29:32 +00:00
|
|
|
|
///
|
|
|
|
|
|
if (Activity == Activity.ImmediateMeasurement)
|
|
|
|
|
|
{
|
|
|
|
|
|
Activity = Activity.Idle;
|
|
|
|
|
|
}
|
2016-04-20 13:58:48 +00:00
|
|
|
|
|
|
|
|
|
|
string justReceived = serialPort.ReadExisting();
|
|
|
|
|
|
if (justReceived != null && justReceived.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.DebugFormat("{0} - RunDevice() received {1}", Name, justReceived);
|
|
|
|
|
|
stringBuilder.Append(justReceived);
|
|
|
|
|
|
|
|
|
|
|
|
if (stringBuilder.ToString().EndsWith("\r\n"))
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntTimeStamp = StateMachine.Time;
|
|
|
|
|
|
|
|
|
|
|
|
string received = stringBuilder.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
string tmp = received.Replace(" ", " ");
|
|
|
|
|
|
if (tmp.Length == received.Length) break;
|
|
|
|
|
|
received = tmp;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-15 10:02:28 +00:00
|
|
|
|
string[] field = received.Substring(0, received.Length - 2).Split(new char[] { ' ' });
|
|
|
|
|
|
if (field.Length < 2)
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = {1}, Unexpected error !!!", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "Z") /// Zero when stable
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 2 && field[1] == "A")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}\", error !!!", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "S") /// Stable (field[1] == "S") or dynamic (field[1] == "D") mass measurement
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 4 && field[1] == "S" &&
|
|
|
|
|
|
double.TryParse(field[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
2016-04-20 13:58:48 +00:00
|
|
|
|
CultureInfo.InvariantCulture, out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[3]), mass);
|
2016-04-20 13:58:48 +00:00
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 4 && field[1] == "D" &&
|
|
|
|
|
|
double.TryParse(field[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
2016-04-20 13:58:48 +00:00
|
|
|
|
CultureInfo.InvariantCulture, out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[3]), mass);
|
2016-04-20 13:58:48 +00:00
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 2 && field[1] == "-")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 2 && field[1] == "+")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = Capacity;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Overload", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length >= 2 && field[1] == "I")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = balanceCfg.Capacity;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Overload;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Overload", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}\", Overload or and unexpected error !!!", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "T") /// Taring
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 4 && field[1] == "S" &&
|
|
|
|
|
|
double.TryParse(field[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
2016-04-20 13:58:48 +00:00
|
|
|
|
CultureInfo.InvariantCulture, out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[3]), mass);
|
2016-04-20 13:58:48 +00:00
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length >= 2 && field[1] == "I")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = mass = balanceCfg.Capacity;
|
|
|
|
|
|
msrmntState = MsrmntState.Overload;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = {1}, Overload", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}\", Overload or and unexpected error !!!", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "U")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 2 && field[1] == "A")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}\"", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}\", Unexpected error !!!", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "SI") /// "SI I<cr><lf>" received in case of overload -> empty the tank
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length >= 2 && field[1] == "I")
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = balanceCfg.Capacity;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
//msrmntState = MsrmntState.Overload;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}\", Overload.", Name,
|
|
|
|
|
|
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="units"></param>
|
|
|
|
|
|
/// <returns>SetUnitsOp instance reference casted to IOperaton</returns>
|
2022-01-23 18:56:15 +00:00
|
|
|
|
public IOperation SetUnitsOp(Common.Unit units)
|
2016-04-20 13:58:48 +00:00
|
|
|
|
{
|
|
|
|
|
|
return new SetUnitsOp(this, units);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|