MettlerToledo.Standard.BalanceOld added, no response to "T<cr><lf>" command, ver.2.5.270
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
///
|
||||
/// Copyright (c) 2016 Sensus Metering Systems
|
||||
///
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.IO.Ports;
|
||||
using log4net;
|
||||
using Config.Entities;
|
||||
using TBF.BenchControl.Generic;
|
||||
|
||||
namespace TBF.BenchControl.MettlerToledo.Standard
|
||||
{
|
||||
/// <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>
|
||||
public class BalanceOld : BalanceDev, IDevice, GenericDevices.IBalance
|
||||
{
|
||||
/// <summary>
|
||||
/// Info: StartMassMeasurement() and "Balnce response = .., Mass = ..."
|
||||
/// Warn: Start measurement when busy is true
|
||||
/// </summary>
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(BalanceOld));
|
||||
public override string ToString() { return string.Format("MettlerToledo.Standard.BalanceOld({0})", Cfg.ToString(1)); }
|
||||
|
||||
public BalanceOld()
|
||||
{
|
||||
}
|
||||
|
||||
public BalanceOld(BalanceCfg cfg)
|
||||
: base(cfg)
|
||||
{
|
||||
log.Debug(this.ToString());
|
||||
}
|
||||
|
||||
public override void Taring()
|
||||
{
|
||||
if (balanceCfg.DebugLevel == DebugMode.Simulate) return;
|
||||
|
||||
if (msrmntState == MsrmntState.Busy)
|
||||
{
|
||||
log.WarnFormat("{0} - Taring() returns without starting taring the scale because measurementState == MeasurementState.Busy", Name);
|
||||
return;
|
||||
}
|
||||
|
||||
log.InfoFormat("{0} - Taring()", Name);
|
||||
|
||||
stringBuilder.Clear();
|
||||
serialPort.Write("T\r\n");
|
||||
|
||||
mass = 0;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse characters received from the serial port
|
||||
/// </summary>
|
||||
public override void RunDeviceBefore()
|
||||
{
|
||||
if (balanceCfg.DebugLevel == DebugMode.Simulate)
|
||||
{
|
||||
mass = TestBenchSim.GetSimMass(balanceNr);
|
||||
Masses[balanceNr] = mass;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
msrmntTimeStamp = StateMachine.Time;
|
||||
return;
|
||||
}
|
||||
|
||||
log.DebugFormat("{0} - RunDevice()", Name);
|
||||
|
||||
if (base.msrmntState != MsrmntState.Busy) return; /// No measurement in progress
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
string[] fields = received.Substring(0, received.Length - 2).Split(new char[] { ' ' });
|
||||
if (fields.Length < 2)
|
||||
{
|
||||
msrmntState = MsrmntState.Failed;
|
||||
log.WarnFormat("{0} - Balance response = {1}, Unexpected error !!!", Name,
|
||||
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
||||
return;
|
||||
}
|
||||
else if (fields[0].Equals("Z")) /// Zero when stable
|
||||
{
|
||||
if (fields.Length == 2 && fields[1].Equals("A"))
|
||||
{
|
||||
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>"));
|
||||
}
|
||||
}
|
||||
else if (fields[0].Equals("S")) /// Stable (field[1] == "S") or dynamic (field[1] == "D") mass measurement
|
||||
{
|
||||
if (fields.Length == 4 && fields[1].Equals("S") &&
|
||||
float.TryParse(fields[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out mass))
|
||||
{
|
||||
if (fields[3].Equals("g")) mass /= 1000.0f;
|
||||
if (fields[3].Equals("t")) mass *= 1000.0f;
|
||||
Masses[balanceNr] = mass;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
||||
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
||||
}
|
||||
else if (fields.Length == 4 && fields[1].Equals("D") &&
|
||||
float.TryParse(fields[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out mass))
|
||||
{
|
||||
if (fields[3].Equals("g")) mass /= 1000.0f;
|
||||
if (fields[3].Equals("t")) mass *= 1000.0f;
|
||||
Masses[balanceNr] = mass;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
||||
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
||||
}
|
||||
else if (fields.Length == 2 && fields[1].Equals("-"))
|
||||
{
|
||||
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);
|
||||
}
|
||||
else if (fields.Length == 2 && fields[1].Equals("+"))
|
||||
{
|
||||
mass = Capacity;
|
||||
Masses[balanceNr] = mass;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
log.InfoFormat("{0} - Balance response = {1}, Overload", Name,
|
||||
received.Replace("\r", "<cr>").Replace("\n", "<lf>"));
|
||||
}
|
||||
else if (fields.Length >= 2 && fields[1].Equals("I"))
|
||||
{
|
||||
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>"));
|
||||
}
|
||||
}
|
||||
else if (fields[0].Equals("T")) /// Taring
|
||||
{
|
||||
if (fields.Length == 4 && fields[1].Equals("S") &&
|
||||
float.TryParse(fields[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite,
|
||||
CultureInfo.InvariantCulture, out mass))
|
||||
{
|
||||
if (fields[3].Equals("g")) mass /= 1000.0f;
|
||||
if (fields[3].Equals("t")) mass *= 1000.0f;
|
||||
msrmntState = MsrmntState.Valid;
|
||||
log.InfoFormat("{0} - Balance response = {1}, Mass = {2}", Name,
|
||||
received.Replace("\r", "<cr>").Replace("\n", "<lf>"), mass);
|
||||
}
|
||||
else if (fields.Length >= 2 && fields[1].Equals("I"))
|
||||
{
|
||||
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>"));
|
||||
}
|
||||
}
|
||||
else if (fields[0].Equals("U"))
|
||||
{
|
||||
if (fields.Length == 2 && fields[1].Equals("A"))
|
||||
{
|
||||
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>"));
|
||||
}
|
||||
}
|
||||
else if (fields[0].Equals("SI")) /// "SI I<cr><lf>" received in case of overload -> empty the tank
|
||||
{
|
||||
if (fields.Length >= 2 && fields[1].Equals("I"))
|
||||
{
|
||||
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>
|
||||
public IOperation SetUnitsOp(Units units)
|
||||
{
|
||||
return new SetUnitsOp(this, units);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user