tbf/TBF/Rig/MettlerToledo/Standard/ZeroOp.cs
Marek Frniak 0075362f92 Fix - Handle scale underload condition
- Added Underload measurement state
- Detect underload responses from Mettler Toledo scales
- Automatically zero the scale after underload detection
- Restart mass measurement after successful zeroing
- Prevent zeroing response from being used as a valid measurement
- Preserve measurement stability by clearing buffered readings after underload
2026-07-13 14:01:57 +02:00

102 lines
2.7 KiB
C#

///
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
///
using System;
using log4net;
namespace TBF.Rig.MettlerToledo.Standard
{
public class ZeroOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(ZeroOp));
public override string ToString() { return string.Format("ZeroOp(.,.)"); }
bool done;
/// Set by the constructor
BalanceDev scale;
bool activityStarted;
/// <summary>
/// Events: BalanceDone, Error
/// </summary>
/// <param name="scale">Balance device instance</param>
/// <param name="result">Reference to variable for 'tara' in kg</param>
public ZeroOp(BalanceDev scale)
{
if (scale == null) throw new ArgumentNullException("balanceDev");
this.scale = scale;
log.Debug(this.ToString());
}
/// <summary>Start this operation</summary>
public void Start()
{
log.DebugFormat("{0} - ZeroOp - Start()", scale.Name);
done = false;
if (scale.Activity == Activity.Idle)
{
scale.SendZeroWhenStableCmd();
activityStarted = true;
scale.Activity = Activity.RunningOperation;
}
else
{
activityStarted = false;
}
}
/// <summary>Run this operation</summary>
/// <returns>
/// Event.None
/// Event.BalanceDone
/// Event.Error . . . . . Current.Tick == null
/// </returns>
public Event Run()
{
if (!activityStarted && scale.Activity == Activity.Idle)
{
scale.SendZeroWhenStableCmd();
activityStarted = true;
scale.Activity = Activity.RunningOperation;
return Event.Busy;
}
if (done) return Event.ScaleDone; /// Zeroing has been done already
if (scale.MsrmntState == MsrmntState.Busy) return Event.Busy;
if (scale.MsrmntState == MsrmntState.Underload)
{
done = true;
return Event.ScaleUnderload;
}
if (scale.MsrmntState == MsrmntState.Overload)
{
done = true;
return Event.ScaleOverload;
}
if (scale.MsrmntState == MsrmntState.Valid)
{
done = true;
return Event.ScaleDone; /// Zeroing completed
}
scale.SendZeroWhenStableCmd(); /// Failed -> repeat zeroing
return Event.Busy;
}
/// <summary>Stop this operation</summary>
public void Stop()
{
if (scale.MsrmntState == MsrmntState.Busy) scale.MsrmntState = MsrmntState.Failed;
scale.Activity = Activity.Idle;
log.DebugFormat("{0} - ZeroOp - Stop()", scale.Name);
}
}
}