2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
2023-04-18 12:01:23 +00:00
|
|
|
|
/// Copyright (c) 2013-2023 Sensus Slovensko a.s.
|
2015-04-15 18:32:56 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
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;
|
2021-10-26 09:23:57 +00:00
|
|
|
|
using TBF.Rig.Generic;
|
|
|
|
|
|
using TBF.Rig.GenericDevices;
|
2014-07-31 15:04:09 +00:00
|
|
|
|
using TBF.Boxes;
|
2020-03-05 12:59:43 +00:00
|
|
|
|
using TBF.Resources;
|
2023-04-18 12:01:23 +00:00
|
|
|
|
using TBF.UI.Calendar;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-10-26 09:23:57 +00:00
|
|
|
|
namespace TBF.Rig.MettlerToledo.Standard
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public enum Activity
|
|
|
|
|
|
{
|
|
|
|
|
|
Idle,
|
|
|
|
|
|
ImmediateMeasurement,
|
|
|
|
|
|
RunningOperation,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +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>
|
2020-03-05 12:59:43 +00:00
|
|
|
|
public class BalanceDev : TankDraining, IDevice, GenericDevices.IScale, GenericDevices.IHasCalendarEvents
|
2014-07-28 07:54:35 +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(BalanceDev));
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2014-12-31 12:46:33 +00:00
|
|
|
|
protected readonly BalanceCfg balanceCfg;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// Enumeration of balances via static fields and methods
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
static int nextBalanceIdx = 0;
|
|
|
|
|
|
public static int BalancesCount { get { return nextBalanceIdx; } }
|
|
|
|
|
|
public static BalanceDev[] Balances;
|
2016-10-07 12:06:54 +00:00
|
|
|
|
public static double[] Masses;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
///
|
|
|
|
|
|
protected int balanceNr; /// 0-based balance number
|
|
|
|
|
|
|
|
|
|
|
|
protected SerialPort serialPort;
|
|
|
|
|
|
protected StringBuilder stringBuilder;
|
|
|
|
|
|
|
2017-03-28 21:47:32 +00:00
|
|
|
|
public int ScaleNr { get { return balanceNr; } }
|
2016-10-07 12:06:54 +00:00
|
|
|
|
public double Capacity { get { return balanceCfg.Capacity; } }
|
2017-03-01 13:40:10 +00:00
|
|
|
|
public int EmptyTimeSec { get { return balanceCfg.DrainTimeSec; } }
|
2018-10-25 15:34:43 +00:00
|
|
|
|
public IValve DrainValve2 { get { return null; } }
|
2016-04-25 10:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
/// Buoyancy
|
|
|
|
|
|
public float BuoyancyTemp { get { return balanceCfg.BuoyancyTemp; } }
|
|
|
|
|
|
public float BuoyancyPress { get { return balanceCfg.BuoyancyPress; } }
|
|
|
|
|
|
public float BuoyancyHumi { get { return balanceCfg.BuoyancyHumi; } }
|
2016-10-07 12:06:54 +00:00
|
|
|
|
public double WeightStandardDensity { get { return balanceCfg.WeightStandardDensity; } }
|
2016-04-25 13:25:23 +00:00
|
|
|
|
|
2016-04-25 10:28:21 +00:00
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>The state of the mass measurement</summary>
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public Activity Activity;
|
|
|
|
|
|
public MsrmntState MsrmntState { get { return msrmntState; } set { msrmntState = value; } }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
protected MsrmntState msrmntState;
|
|
|
|
|
|
|
2018-05-14 08:41:03 +00:00
|
|
|
|
/// <summary>Time since the last serial command in seconds</summary>
|
|
|
|
|
|
public int MsrmntTime;
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>The mass after StartMassMeasurement() when MsrmntState == MsrmntState.Valid</summary>
|
2016-10-07 12:06:54 +00:00
|
|
|
|
public double Mass { get { return mass; } }
|
|
|
|
|
|
protected double mass;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>The time stamp after StartMassMeasurement() when MsrmntState == MsrmntState.Valid</summary>
|
2014-12-31 12:46:33 +00:00
|
|
|
|
public int MsrmntTimeStamp { get { return msrmntTimeStamp; } }
|
2014-07-28 07:54:35 +00:00
|
|
|
|
protected int msrmntTimeStamp;
|
|
|
|
|
|
|
2015-03-25 09:01:37 +00:00
|
|
|
|
public string Format { get { return balanceCfg.Format; } }
|
|
|
|
|
|
|
2020-08-07 07:30:38 +00:00
|
|
|
|
|
|
|
|
|
|
public BalanceDev() { }
|
2015-06-29 09:14:33 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor
|
|
|
|
|
|
/// </summary>
|
2014-12-31 12:46:33 +00:00
|
|
|
|
/// <param name="cfg">Balance properties</param>
|
|
|
|
|
|
/// <param components="cfg">A list of components loaded so far</param>
|
2016-08-05 12:32:36 +00:00
|
|
|
|
public BalanceDev(Generic.IComponentCfg cfg)
|
2014-12-31 12:46:33 +00:00
|
|
|
|
: base(cfg)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2016-08-05 12:32:36 +00:00
|
|
|
|
balanceCfg = cfg as BalanceCfg;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-07 07:57:42 +00:00
|
|
|
|
public override void Initialize()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-04-07 07:57:42 +00:00
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
|
|
balanceNr = nextBalanceIdx++;
|
|
|
|
|
|
Masses = new double[nextBalanceIdx]; /// (re)initialize the static array of masses of all balances
|
|
|
|
|
|
///
|
|
|
|
|
|
if (Balances == null || Balances.Length < nextBalanceIdx)
|
|
|
|
|
|
{
|
|
|
|
|
|
BalanceDev[] balancesSoFar = Balances;
|
|
|
|
|
|
Balances = new BalanceDev[nextBalanceIdx];
|
|
|
|
|
|
if (balancesSoFar != null) for (int i = 0; i < balancesSoFar.Length; i++) Balances[i] = balancesSoFar[i];
|
|
|
|
|
|
Balances[nextBalanceIdx - 1] = this;
|
|
|
|
|
|
}
|
2017-03-27 21:05:02 +00:00
|
|
|
|
|
2023-04-18 12:01:23 +00:00
|
|
|
|
if (balanceCfg.CalibValidDate > DateTime.MinValue && balanceCfg.CalibValidDate.Date < DateTime.Now.Date)
|
2020-03-05 12:59:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
throw new Exception(string.Format("{0}: {1}", Name, Strings.Calibration_certificate_validity_expired));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
Activity = Activity.Idle;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
msrmntState = MsrmntState.Failed; /// Data not valid yet
|
2018-05-14 08:41:03 +00:00
|
|
|
|
MsrmntTime = 0;
|
2021-04-07 07:57:42 +00:00
|
|
|
|
stringBuilder = new StringBuilder(40);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-04-07 07:57:42 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Normal)
|
|
|
|
|
|
{
|
|
|
|
|
|
string portName = "COM" + balanceCfg.ComPortNr.ToString();
|
|
|
|
|
|
serialPort = new SerialPort(portName, balanceCfg.BaudRate, balanceCfg.Parity, balanceCfg.DataBits, balanceCfg.StopBits);
|
|
|
|
|
|
serialPort.Handshake = balanceCfg.Handshake;
|
|
|
|
|
|
serialPort.Open();
|
|
|
|
|
|
log.FatalFormat("{0} initialized: {1}", Name, this);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
serialPort = null;
|
|
|
|
|
|
log.FatalFormat("{0} simulated: {1}", Name, this);
|
|
|
|
|
|
}
|
2020-03-05 12:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-18 12:01:23 +00:00
|
|
|
|
public List<Config.CalendarEvent.ICalendarEvent> GetCalendarEvents()
|
2020-03-05 12:59:43 +00:00
|
|
|
|
{
|
2023-04-18 12:01:23 +00:00
|
|
|
|
var calEvents = new List<Config.CalendarEvent.ICalendarEvent>();
|
2020-03-05 12:59:43 +00:00
|
|
|
|
|
2023-04-18 12:01:23 +00:00
|
|
|
|
DateTime calibDue = balanceCfg.CalibValidDate;
|
|
|
|
|
|
if (calibDue > TBF.UI.Constants.MinDate)
|
2020-03-05 12:59:43 +00:00
|
|
|
|
{
|
2023-04-18 12:01:23 +00:00
|
|
|
|
/// Five weekly notifications
|
|
|
|
|
|
foreach (var days in new int[] { -35, -28, -21, -14 })
|
2020-04-21 08:59:31 +00:00
|
|
|
|
{
|
2023-04-18 12:01:23 +00:00
|
|
|
|
if (calibDue.Date.AddDays(days + 6) >= DateTime.Now.Date)
|
|
|
|
|
|
{
|
|
|
|
|
|
calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Notification,
|
|
|
|
|
|
calibDue.Date.AddDays(days),
|
|
|
|
|
|
Name,
|
|
|
|
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
|
|
|
|
calibDue.Date.ToString(TBF.UI.Constants.DateFormat))));
|
|
|
|
|
|
}
|
2020-04-21 08:59:31 +00:00
|
|
|
|
}
|
2023-04-18 12:01:23 +00:00
|
|
|
|
|
|
|
|
|
|
/// Five daily warnings
|
|
|
|
|
|
foreach (var days in new int[] { -7, -6, -5, -4, -3, -2, -1 })
|
2020-04-21 08:59:31 +00:00
|
|
|
|
{
|
2023-04-18 12:01:23 +00:00
|
|
|
|
if (calibDue.Date.AddDays(days) >= DateTime.Now.Date)
|
|
|
|
|
|
{
|
|
|
|
|
|
/// Weekly reminders (last 5 weeks)
|
|
|
|
|
|
calEvents.Add(new CalibrationReminderEvent(Config.CalendarEvent.AutoAction.Warning,
|
|
|
|
|
|
calibDue.Date.AddDays(days),
|
|
|
|
|
|
Name,
|
|
|
|
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
|
|
|
|
calibDue.Date.ToString(TBF.UI.Constants.DateFormat))));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Calibration due date
|
|
|
|
|
|
if (calibDue.Date >= DateTime.Now.Date)
|
|
|
|
|
|
{
|
|
|
|
|
|
calEvents.Add(new CalibrationDueDateEvent(calibDue.Date,
|
|
|
|
|
|
Name,
|
|
|
|
|
|
string.Format(Strings.Calibration_due_date_is_0,
|
|
|
|
|
|
calibDue.Date.ToString(TBF.UI.Constants.DateFormat))));
|
2020-04-21 08:59:31 +00:00
|
|
|
|
}
|
2020-03-05 12:59:43 +00:00
|
|
|
|
}
|
2023-04-18 12:01:23 +00:00
|
|
|
|
|
|
|
|
|
|
return calEvents;
|
2014-12-10 21:39:53 +00:00
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2020-03-05 12:59:43 +00:00
|
|
|
|
|
2017-03-28 19:40:27 +00:00
|
|
|
|
public override bool IsEmpty()
|
2015-05-22 10:31:10 +00:00
|
|
|
|
{
|
2018-05-29 11:37:04 +00:00
|
|
|
|
return DrainValve.State ? (mass <= balanceCfg.Empty) : (mass <= balanceCfg.EmptyUp);
|
2015-05-22 10:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-14 10:21:27 +00:00
|
|
|
|
public override bool ContainsMoreThen(float thld)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (mass >= thld);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public virtual void SendZeroWhenStableCmd()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - SendZeroWhenStableCmd() - serial port closed", Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (msrmntState == MsrmntState.Busy)
|
|
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.WarnFormat("{0} - SendZeroWhenStableCmd() returns without starting zeroing the scale because measurementState == MeasurementState.Busy", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SendZeroWhenStableCmd() - \"Z\\r\\n\"", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
msrmntState = MsrmntState.Busy;
|
2018-05-14 08:41:03 +00:00
|
|
|
|
MsrmntTime = 0;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
serialPort.Write("Z\r\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public void SendTaraCmd()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - SendTaraCmd() - serial port closed", Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (msrmntState == MsrmntState.Busy)
|
|
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.WarnFormat("{0} - SendTaraCmd() returns without starting taring the scale because measurementState == MeasurementState.Busy", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SendTaraCmd() - - \"T\\r\\n\"", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
msrmntState = MsrmntState.Busy;
|
2018-05-14 08:41:03 +00:00
|
|
|
|
MsrmntTime = 0;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
serialPort.Write("T\r\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get a stable mass measurement
|
|
|
|
|
|
/// </summary>
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public void SendStableMeasurementCmd()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - SendStableMeasurementCmd() - serial port closed", Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (msrmntState == MsrmntState.Busy)
|
|
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.WarnFormat("{0} - SendStableMeasurementCmd() returns without starting a measurement because measurementState == MeasurementState.Busy", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SendStableMeasurementCmd() - \"S\\r\\n\"", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
msrmntState = MsrmntState.Busy;
|
2018-05-14 08:41:03 +00:00
|
|
|
|
MsrmntTime = 0;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
serialPort.Write("S\r\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get an immediate mass measurement
|
|
|
|
|
|
/// </summary>
|
2020-10-29 09:29:32 +00:00
|
|
|
|
public void SendImmediateMeasurementCmd()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - SendImmediateMeasurementCmd() - serial port closed", Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
if (msrmntState == MsrmntState.Busy)
|
|
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.WarnFormat("{0} - SendImmediateMeasurementCmd() returns without starting a measurement because measurementState == MeasurementState.Busy", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SendImmediateMeasurementCmd() - \"SI\\r\\n\"", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
msrmntState = MsrmntState.Busy;
|
2018-05-14 08:41:03 +00:00
|
|
|
|
MsrmntTime = 0;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
serialPort.Write("SI\r\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the serial number
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>After a successful SetUnits(kg) mass is still sent in g. MH 15.10.2013</remarks>
|
2022-01-23 18:56:15 +00:00
|
|
|
|
public void SendSetUnitsCmd(Common.Unit unit)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.ErrorFormat("{0} - SendSetUnitsCmd() - serial port closed", Name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
if (msrmntState == MsrmntState.Busy)
|
|
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.WarnFormat("{0} - SendSetUnitsCmd() returns without setting the units because measurementState == MeasurementState.Busy", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
msrmntState = MsrmntState.Busy;
|
|
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
switch (unit)
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
case Common.Unit.g:
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SetUnits({1}) - \"U g\\r\\n\"", Name, unit);
|
|
|
|
|
|
serialPort.Write("U g\r\n");
|
|
|
|
|
|
break;
|
2022-01-23 18:56:15 +00:00
|
|
|
|
case Common.Unit.t:
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SetUnits({1}) - \"U t\\r\\n\"", Name, unit);
|
|
|
|
|
|
serialPort.Write("U t\r\n");
|
|
|
|
|
|
break;
|
2022-01-23 18:56:15 +00:00
|
|
|
|
case Common.Unit.lb:
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SetUnits({1}) - \"U lb\\r\\n\"", Name, unit);
|
|
|
|
|
|
serialPort.Write("U lb\r\n");
|
|
|
|
|
|
break;
|
2022-01-23 18:56:15 +00:00
|
|
|
|
case Common.Unit.oz:
|
2020-10-29 09:29:32 +00:00
|
|
|
|
log.InfoFormat("{0} - SetUnits({1}) - \"U oz\\r\\n\"", Name, unit);
|
|
|
|
|
|
serialPort.Write("U oz\r\n");
|
|
|
|
|
|
break;
|
2022-01-23 18:56:15 +00:00
|
|
|
|
case Common.Unit.kg:
|
2021-02-15 10:02:28 +00:00
|
|
|
|
default:
|
|
|
|
|
|
log.InfoFormat("{0} - SetUnits({1}) - \"U kg\\r\\n\"", Name, unit);
|
|
|
|
|
|
serialPort.Write("U kg\r\n");
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse characters received from the serial port
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual void RunDeviceBefore()
|
|
|
|
|
|
{
|
2017-03-27 21:05:02 +00:00
|
|
|
|
base.RunBefore();
|
|
|
|
|
|
|
2015-12-04 16:17:20 +00:00
|
|
|
|
if (balanceCfg.DebugLevel == DebugMode.Simulate)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2020-08-07 07:30:38 +00:00
|
|
|
|
Masses[balanceNr] = 123.456;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2014-08-01 06:36:17 +00:00
|
|
|
|
msrmntTimeStamp = StateMachine.Time;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-28 13:03:29 +00:00
|
|
|
|
if (serialPort == null || !serialPort.IsOpen)
|
2017-02-16 08:55:49 +00:00
|
|
|
|
{
|
2021-04-28 13:03:29 +00:00
|
|
|
|
log.ErrorFormat("{0} - RunDeviceBefore() - serial port closed", Name);
|
2017-02-16 08:55:49 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-29 11:29:37 +00:00
|
|
|
|
log.DebugFormat("{0} - RunDeviceBefore()", Name);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
if (msrmntState != MsrmntState.Busy) return; /// No measurement in progress
|
2020-10-29 09:29:32 +00:00
|
|
|
|
///
|
|
|
|
|
|
if (Activity == Activity.ImmediateMeasurement)
|
|
|
|
|
|
{
|
|
|
|
|
|
Activity = Activity.Idle;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
string justReceived = serialPort.ReadExisting();
|
|
|
|
|
|
if (justReceived != null && justReceived.Length > 0)
|
|
|
|
|
|
{
|
2016-12-29 11:29:37 +00:00
|
|
|
|
log.DebugFormat("{0} - RunDeviceBefore() received {1}", Name, justReceived);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
stringBuilder.Append(justReceived);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string currentBuffer = stringBuilder.ToString();
|
|
|
|
|
|
int eolPos = currentBuffer.IndexOf("\r\n");
|
|
|
|
|
|
if (eolPos >= 0)
|
|
|
|
|
|
{
|
2014-08-01 06:36:17 +00:00
|
|
|
|
msrmntTimeStamp = StateMachine.Time;
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2016-01-26 12:43:47 +00:00
|
|
|
|
/// Get part of the string before EOL
|
2014-07-28 07:54:35 +00:00
|
|
|
|
string received = currentBuffer.Substring(0, eolPos);
|
|
|
|
|
|
|
|
|
|
|
|
/// Put the rest back to the string builder
|
|
|
|
|
|
stringBuilder.Clear();
|
|
|
|
|
|
if (currentBuffer.Length > eolPos + 2)
|
|
|
|
|
|
{
|
|
|
|
|
|
stringBuilder.Append(currentBuffer.Substring(eolPos + 2));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.Split(new char[] { ' ' });
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 1 && field[0] == "ZA") /// Zero when stable
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
//msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\"", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length < 2)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", Unexpected error !!!", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field[0] == "Z") /// Zero when stable
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 2 && field[1] == "A")
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
//msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\"", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", error !!!", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "S") /// Stable mass: field[1] = <mass> field[2] = "kg"
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length == 3 &&
|
|
|
|
|
|
double.TryParse(field[1],
|
2014-07-28 07:54:35 +00:00
|
|
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[2]), mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Mass = {2}", Name, received, mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 4 && (field[1] == "S" || field[1] == "D") &&
|
|
|
|
|
|
double.TryParse(field[2],
|
2016-04-20 13:07:25 +00:00
|
|
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[3]), mass);
|
2016-04-20 13:07:25 +00:00
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Mass = {2}", Name, received, mass);
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 2 && field[1] == "-")
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Mass = {2}", Name, received, mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} Balance response = \"{1}<cr><lf>\", Overload or and unexpected error !!!", Name, received);
|
2021-02-15 10:02:28 +00:00
|
|
|
|
bool b = double.TryParse(field[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "SD") /// Dynamic mass: field[1] = <mass> field[2] = "kg"
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length >= 3 &&
|
|
|
|
|
|
double.TryParse(field[1],
|
2014-07-28 07:54:35 +00:00
|
|
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[2]), mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Mass = {2}", Name, received, mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field.Length == 2 && field[1] == "-")
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
mass = 0;
|
|
|
|
|
|
Masses[balanceNr] = mass;
|
|
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Mass = {2}", Name, received, mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", Overload or and unexpected error !!!", Name, received);
|
2021-02-15 10:02:28 +00:00
|
|
|
|
bool b = double.TryParse(field[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "TB") /// Taring
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length >= 3 &&
|
|
|
|
|
|
double.TryParse(field[1],
|
2014-07-28 07:54:35 +00:00
|
|
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[2]), mass);
|
2021-02-15 10:02:28 +00:00
|
|
|
|
msrmntState = MsrmntState.Valid;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Tara = {2}", Name, received, mass);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", Overload or and unexpected error !!!", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
else if (field[0] == "T") /// Taring
|
2016-04-20 13:07:25 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
if (field.Length >= 4 && field[1] == "S" &&
|
|
|
|
|
|
double.TryParse(field[2],
|
2016-04-20 13:07:25 +00:00
|
|
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
|
out mass))
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
mass = Common.Units.ConvertFrom(ParseUnit(field[3]), mass);
|
2016-04-20 13:07:25 +00:00
|
|
|
|
msrmntState = MsrmntState.Valid;
|
|
|
|
|
|
log.InfoFormat("{0} - Balance response = \"{1}<cr><lf>\", Tara = {2}", Name, received, mass);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
|
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", Overload or and unexpected error !!!", Name, received);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
msrmntState = MsrmntState.Failed;
|
2015-05-21 12:24:07 +00:00
|
|
|
|
log.WarnFormat("{0} - Balance response = \"{1}<cr><lf>\", Unexpected error !!!", Name, received);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Run this device</summary>
|
|
|
|
|
|
public void RunDeviceAfter()
|
|
|
|
|
|
{
|
2020-10-29 09:29:32 +00:00
|
|
|
|
if (Activity == Activity.Idle)
|
|
|
|
|
|
{
|
|
|
|
|
|
SendImmediateMeasurementCmd();
|
|
|
|
|
|
Activity = Activity.ImmediateMeasurement;
|
|
|
|
|
|
}
|
2017-03-27 21:05:02 +00:00
|
|
|
|
base.RunAfter();
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Stop this device</summary>
|
|
|
|
|
|
public void StopDevice()
|
|
|
|
|
|
{
|
2017-04-03 13:15:31 +00:00
|
|
|
|
if ((balanceCfg.DebugLevel != DebugMode.Simulate) && (serialPort != null))
|
2017-03-09 12:29:10 +00:00
|
|
|
|
{
|
|
|
|
|
|
serialPort.Close();
|
|
|
|
|
|
serialPort = null;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-05 18:26:07 +00:00
|
|
|
|
public void StopDevice2() { }
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
2020-08-07 07:30:38 +00:00
|
|
|
|
/// <returns>ZeroOp reference</returns>
|
|
|
|
|
|
public IOperation ZeroOp()
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
2021-02-15 10:02:28 +00:00
|
|
|
|
switch (balanceCfg.ActionAfter1stDraining)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ActionA1D.Zero:
|
|
|
|
|
|
return new ZeroOp(this);
|
|
|
|
|
|
case ActionA1D.Tara:
|
|
|
|
|
|
DoubleBox dummy = new DoubleBox();
|
|
|
|
|
|
return new TaringOp(this, ref dummy);
|
|
|
|
|
|
default:
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-07 07:30:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="result">Reference to a variable for 'tara' in kg</param>
|
|
|
|
|
|
/// <returns>TaringOp reference</returns>
|
|
|
|
|
|
public IOperation TaringOp(ref DoubleBox tara)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new TaringOp(this, ref tara);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-07-28 07:54:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="result">Reference to a variable for the measured mass in kg</param>
|
|
|
|
|
|
/// <returns>ReadMassOp instance reference casted to IOperaton</returns>
|
2016-10-07 12:06:54 +00:00
|
|
|
|
public IOperation ReadMassOp(ref DoubleBox mass)
|
2014-07-28 07:54:35 +00:00
|
|
|
|
{
|
|
|
|
|
|
return new ReadMassOp(this, ref mass);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="result">Reference to a variable for the measured mass in kg</param>
|
|
|
|
|
|
/// <param name="readingsCount">Required mass readings count (>= 3)</param>
|
2016-04-14 15:03:33 +00:00
|
|
|
|
/// <param name="maxSpread">Maximum spread of measurements in kg (otherwise the measurement continues)</param>
|
|
|
|
|
|
/// <param name="method">Method: false = slow (precise), true = fast (immediate)</param>
|
|
|
|
|
|
/// <returns>ReadMassAverageOp instance reference casted to IOperaton</returns>
|
2021-09-23 09:46:40 +00:00
|
|
|
|
public IOperation ReadStableMassOp(ref DoubleBox mass, int readingsCount, double maxSpread, MassMethod method)
|
2016-04-14 15:03:33 +00:00
|
|
|
|
{
|
|
|
|
|
|
return new ReadStableMassOp(this, ref mass, readingsCount, maxSpread, method);
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
2017-06-26 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Events: BalanceDone, Error
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serialNumber">Reference to the serialNumber</param>
|
|
|
|
|
|
/// <returns>GetBalanceSNOp instance reference casted to IOperaton</returns>
|
|
|
|
|
|
public virtual IOperation GetSerNumOp(ref string serialNumber)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2021-02-15 10:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse a string representing mass units.
|
|
|
|
|
|
/// Defaults to 'kg'in case unitStr not recognized.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="unitStr">String: "kg", "g", "t", "lb" or "oz" expected</param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-01-23 18:56:15 +00:00
|
|
|
|
protected Common.Unit ParseUnit(string unitStr)
|
2021-02-15 10:02:28 +00:00
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
if (unitStr == "g") return Common.Unit.g;
|
|
|
|
|
|
if (unitStr == "t") return Common.Unit.t;
|
|
|
|
|
|
if (unitStr == "lb") return Common.Unit.lb;
|
|
|
|
|
|
if (unitStr == "oz") return Common.Unit.oz;
|
|
|
|
|
|
return Common.Unit.kg;
|
2021-02-15 10:02:28 +00:00
|
|
|
|
}
|
2014-07-28 07:54:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|