287 lines
10 KiB
C#
287 lines
10 KiB
C#
///
|
|
/// Copyright (c) 2015 Sensus Metering Systems
|
|
/// Author: Milan Hanajik
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.IO.Ports;
|
|
using log4net;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Boxes;
|
|
|
|
namespace TBF.BenchControl.MettlerToledo.Multi
|
|
{
|
|
/// <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 BalanceDev : ComponentBase, 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(BalanceDev));
|
|
public override string ToString() { return string.Format("MettlerToledo.Multi.Balance({0})", Cfg.ToString(1)); }
|
|
|
|
protected readonly BalanceCfg balanceCfg;
|
|
|
|
/// <summary>
|
|
/// Enumeration of balances via static fields and methods
|
|
/// </summary>
|
|
static int nextBalanceIdx = 0;
|
|
public static new void ResetStaticProperties()
|
|
{
|
|
nextBalanceIdx = 0;
|
|
}
|
|
public static int BalancesCount { get { return nextBalanceIdx; } }
|
|
public static BalanceDev[] Balances;
|
|
public static float[] Masses;
|
|
///
|
|
protected int balanceNr; /// 0-based balance number
|
|
|
|
protected SerialPort serialPort;
|
|
protected StringBuilder stringBuilder;
|
|
|
|
public int BalanceNr { get { return balanceNr; } }
|
|
public float Capacity { get { return balanceCfg.Capacity; } }
|
|
public float Resolution { get { return balanceCfg.Resolution; } }
|
|
public int EmptyTimeSec { get { return balanceCfg.EmptyTimeSec; } }
|
|
|
|
/// <summary>The state of the mass measurement</summary>
|
|
public MsrmntState MsrmntState { get { return msrmntState; } }
|
|
protected MsrmntState msrmntState;
|
|
|
|
/// <summary>The mass after StartMassMeasurement() when MsrmntState == MsrmntState.Valid</summary>
|
|
public float Mass { get { return Masses[balanceNr]; } }
|
|
|
|
/// <summary>The time stamp after StartMassMeasurement() when MsrmntState == MsrmntState.Valid</summary>
|
|
public int MsrmntTimeStamp { get { return msrmntTimeStamp; } }
|
|
protected int msrmntTimeStamp;
|
|
|
|
public string Format { get { return balanceCfg.Format; } }
|
|
|
|
public BalanceDev()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="cfg">Balance properties</param>
|
|
public BalanceDev(BalanceCfg cfg)
|
|
: base(cfg)
|
|
{
|
|
balanceCfg = cfg;
|
|
|
|
balanceNr = nextBalanceIdx++;
|
|
Masses = new float[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;
|
|
}
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
msrmntState = MsrmntState.Busy; /// Data not valid yet
|
|
|
|
if (balanceCfg.DebugLevel == Entities.DebugMode.Simulate) return;
|
|
|
|
if (balanceCfg.IdNr == 1)
|
|
{
|
|
stringBuilder = new StringBuilder(40);
|
|
string comPortName = "COM" + balanceCfg.ComPortNr.ToString();
|
|
serialPort = new SerialPort(comPortName, balanceCfg.BaudRate, balanceCfg.Parity, balanceCfg.DataBits, balanceCfg.StopBits);
|
|
serialPort.Handshake = balanceCfg.Handshake;
|
|
serialPort.Open();
|
|
}
|
|
|
|
log.FatalFormat("Successfully initialized device {0}", ToString());
|
|
}
|
|
|
|
public bool IsEmpty()
|
|
{
|
|
return (Mass <= balanceCfg.Empty);
|
|
}
|
|
|
|
public bool IsEmpty(float mass)
|
|
{
|
|
return (mass <= balanceCfg.Empty);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse characters received from the serial port
|
|
/// </summary>
|
|
public virtual void RunDeviceBefore()
|
|
{
|
|
if (balanceCfg.DebugLevel == Entities.DebugMode.Simulate)
|
|
{
|
|
Masses[balanceNr] = TestBenchSim.GetSimMass(balanceNr);
|
|
msrmntState = MsrmntState.Valid;
|
|
msrmntTimeStamp = StateMachine.Time;
|
|
return;
|
|
}
|
|
|
|
if (balanceCfg.IdNr != 1) return;
|
|
|
|
log.Debug("RunDevice()");
|
|
|
|
string justReceived = serialPort.ReadExisting();
|
|
if (justReceived != null && justReceived.Length > 0)
|
|
{
|
|
string curBuf = justReceived.ToString();
|
|
log.Debug("RunDevice() received " + curBuf);
|
|
StringBuilder hexa = new StringBuilder();
|
|
for (int i = 0; curBuf.Length > i; i++)
|
|
{
|
|
hexa.Append(((int)curBuf[i]).ToString("X2"));
|
|
hexa.Append(" ");
|
|
}
|
|
log.Debug(hexa.ToString());
|
|
stringBuilder.Append(justReceived);
|
|
}
|
|
|
|
string currentBuffer = stringBuilder.ToString();
|
|
stringBuilder.Clear();
|
|
int eolPos = currentBuffer.IndexOf('\r');
|
|
if (eolPos >= 0 && currentBuffer.Length > eolPos + 33)
|
|
{
|
|
msrmntTimeStamp = StateMachine.Time;
|
|
|
|
/// Get part of theh string before EOL
|
|
int balanceIdNr = currentBuffer[eolPos + 1];
|
|
string received = currentBuffer.Substring(eolPos + 2, 15);
|
|
|
|
while (true)
|
|
{
|
|
string tmp = received.Replace(" ", " ");
|
|
if (tmp.Length == received.Length) break;
|
|
received = tmp;
|
|
}
|
|
|
|
string[] fields = received.Split(new char[] { ' ' });
|
|
|
|
if (fields.Length == 3 && balanceIdNr >= 1 && balanceIdNr <= nextBalanceIdx)
|
|
{
|
|
int code;
|
|
float tmpMass;
|
|
if (int.TryParse(fields[0], out code) &&
|
|
(code >= 0 || code == -8) &&
|
|
float.TryParse(fields[1],
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
CultureInfo.InvariantCulture,
|
|
out tmpMass))
|
|
{
|
|
Masses[balanceIdNr - 1] = tmpMass * Balances[balanceIdNr - 1].Resolution;
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Valid;
|
|
log.InfoFormat("Balance({2}) Valid response=\"{0}<cr>\", Mass = {1}kg", received, Masses[balanceIdNr - 1], balanceIdNr);
|
|
}
|
|
else
|
|
{
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Busy;
|
|
log.InfoFormat("Balance({2}) Busy response=\"{0}<cr>\", Mass = {1}kg", received, Masses[balanceIdNr - 1], balanceIdNr);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Failed;
|
|
log.WarnFormat("Balance({2}) Failed response=\"{0}<cr>\", Unexpected error !!!", received, balanceIdNr);
|
|
}
|
|
|
|
/// Get part of theh string before EOL
|
|
balanceIdNr = currentBuffer[eolPos + 18];
|
|
received = currentBuffer.Substring(eolPos + 19, 15);
|
|
|
|
while (true)
|
|
{
|
|
string tmp = received.Replace(" ", " ");
|
|
if (tmp.Length == received.Length) break;
|
|
received = tmp;
|
|
}
|
|
|
|
fields = received.Split(new char[] { ' ' });
|
|
|
|
if (fields.Length == 3 && balanceIdNr >= 1 && balanceIdNr <= nextBalanceIdx)
|
|
{
|
|
int code;
|
|
float tmpMass;
|
|
if (int.TryParse(fields[0], out code) &&
|
|
(code >= 0 || code == -8) &&
|
|
float.TryParse(fields[1],
|
|
NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign,
|
|
CultureInfo.InvariantCulture,
|
|
out tmpMass))
|
|
{
|
|
Masses[balanceIdNr - 1] = tmpMass * Balances[balanceIdNr - 1].Resolution;
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Valid;
|
|
log.InfoFormat("Balance({2}) Valid response=\"{0}<cr>\", Mass = {1}kg", received, Masses[balanceIdNr - 1], balanceIdNr);
|
|
}
|
|
else
|
|
{
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Busy;
|
|
log.InfoFormat("Balance({2}) Busy response=\"{0}<cr>\", Mass = {1}kg", received, Masses[balanceIdNr - 1], balanceIdNr);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Balances[balanceIdNr - 1].msrmntState = MsrmntState.Failed;
|
|
log.WarnFormat("Balance({2}) Failed response=\"{0}<cr>\", Unexpected error !!!", received, balanceIdNr);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Run this device</summary>
|
|
public void RunDeviceAfter()
|
|
{
|
|
}
|
|
|
|
/// <summary>Stop this device</summary>
|
|
public void StopDevice()
|
|
{
|
|
if (balanceCfg.IdNr == 1 && serialPort != null) serialPort.Close();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Events: BalanceDone, Error
|
|
/// </summary>
|
|
/// <param name="result">Reference to a variable for 'tara' in kg</param>
|
|
/// <returns>TaringOp instance reference casted to IOperaton</returns>
|
|
public IOperation TaringOp(ref FloatBox tara)
|
|
{
|
|
return new TaringOp(this, ref tara);
|
|
}
|
|
|
|
/// <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>
|
|
public IOperation ReadMassOp(ref FloatBox mass)
|
|
{
|
|
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>
|
|
/// <returns>ReadMassAverageOp instance reference casted to IOperaton</returns>
|
|
public IOperation ReadStableMassOp(ref FloatBox mass, int readingsCount)
|
|
{
|
|
return new ReadStableMassOp(this, ref mass, readingsCount);
|
|
}
|
|
}
|
|
}
|