164 lines
4.6 KiB
C#
164 lines
4.6 KiB
C#
// ConductivityMeterDiagnostics.cs
|
|
using System;
|
|
using System.Text;
|
|
|
|
namespace TBF.Rig.Modbus.ConductivityMeter
|
|
{
|
|
public class ConductivityMeterDiagnostics
|
|
{
|
|
public DateTime LastRequestTime { get; private set; }
|
|
public DateTime LastResponseTime { get; private set; }
|
|
|
|
public string LastRequest { get; private set; }
|
|
public string LastResponse { get; private set; }
|
|
public string LastError { get; private set; }
|
|
|
|
public int RequestCount { get; private set; }
|
|
public int ResponseCount { get; private set; }
|
|
public int ErrorCount { get; private set; }
|
|
|
|
public ConductivityMeterDiagnostics()
|
|
{
|
|
LastRequest = string.Empty;
|
|
LastResponse = string.Empty;
|
|
LastError = string.Empty;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
LastRequestTime = DateTime.MinValue;
|
|
LastResponseTime = DateTime.MinValue;
|
|
|
|
LastRequest = string.Empty;
|
|
LastResponse = string.Empty;
|
|
LastError = string.Empty;
|
|
|
|
RequestCount = 0;
|
|
ResponseCount = 0;
|
|
ErrorCount = 0;
|
|
}
|
|
|
|
public void SetRequest(
|
|
byte address,
|
|
byte function,
|
|
ushort firstRegister,
|
|
ushort registerCount)
|
|
{
|
|
RequestCount++;
|
|
LastRequestTime = DateTime.Now;
|
|
|
|
LastRequest = string.Format(
|
|
"TX {0:HH:mm:ss.fff}{1}" +
|
|
"Address: {2}{1}" +
|
|
"Function: 0x{3:X2}{1}" +
|
|
"First register: {4}{1}" +
|
|
"Register count: {5}",
|
|
LastRequestTime,
|
|
Environment.NewLine,
|
|
address,
|
|
function,
|
|
firstRegister,
|
|
registerCount);
|
|
}
|
|
|
|
public void SetResponse(
|
|
byte[] telegram,
|
|
ushort[] rawRegisters,
|
|
double milliAmps,
|
|
double conductivity,
|
|
string unit,
|
|
float floatValue)
|
|
{
|
|
ResponseCount++;
|
|
LastResponseTime = DateTime.Now;
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendFormat("RX {0:HH:mm:ss.fff}", LastResponseTime);
|
|
sb.AppendLine();
|
|
|
|
sb.Append("Telegram: ");
|
|
sb.AppendLine(ToHex(telegram));
|
|
|
|
if (rawRegisters != null)
|
|
{
|
|
for (int i = 0; i < rawRegisters.Length; i++)
|
|
{
|
|
sb.AppendFormat("Raw[{0}]: {1}", i, rawRegisters[i]);
|
|
sb.AppendLine();
|
|
}
|
|
}
|
|
|
|
sb.AppendFormat("Float value: {0:0.###}", floatValue);
|
|
sb.AppendLine();
|
|
|
|
sb.AppendFormat("Current: {0:0.000} mA", milliAmps);
|
|
sb.AppendLine();
|
|
|
|
sb.AppendFormat("Conductivity: {0:0.###} {1}", conductivity, unit);
|
|
|
|
LastResponse = sb.ToString();
|
|
LastError = string.Empty;
|
|
}
|
|
|
|
public void SetError(string message)
|
|
{
|
|
ErrorCount++;
|
|
LastError = string.Format(
|
|
"{0:HH:mm:ss.fff} {1}",
|
|
DateTime.Now,
|
|
message);
|
|
}
|
|
|
|
public string GetText()
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine("Conductivity Meter Diagnostics");
|
|
sb.AppendLine("--------------------------------");
|
|
sb.AppendFormat("Requests: {0}", RequestCount);
|
|
sb.AppendLine();
|
|
sb.AppendFormat("Responses: {0}", ResponseCount);
|
|
sb.AppendLine();
|
|
sb.AppendFormat("Errors: {0}", ErrorCount);
|
|
sb.AppendLine();
|
|
sb.AppendLine();
|
|
|
|
if (!string.IsNullOrEmpty(LastRequest))
|
|
{
|
|
sb.AppendLine(LastRequest);
|
|
sb.AppendLine();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(LastResponse))
|
|
{
|
|
sb.AppendLine(LastResponse);
|
|
sb.AppendLine();
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(LastError))
|
|
{
|
|
sb.AppendLine("Last error:");
|
|
sb.AppendLine(LastError);
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
static string ToHex(byte[] data)
|
|
{
|
|
if (data == null || data.Length == 0)
|
|
return string.Empty;
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
|
{
|
|
if (i > 0) sb.Append(" ");
|
|
sb.Append(data[i].ToString("X2"));
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
} |