IrdaSerialPort - reading after ping bytes, tiny loggign

This commit is contained in:
Stoyan Zlatev
2024-09-24 09:47:39 +02:00
parent c84dcc36bc
commit 7ab948d38a
18 changed files with 174 additions and 242 deletions
+16 -25
View File
@@ -1,6 +1,6 @@
namespace CommonConsole
{
using Common.Logic.Extensions.SQL;
using Common.Hardware.WaterMeter.OmniPlus;
using System;
using System.Collections.Generic;
@@ -10,30 +10,7 @@
{
public static void Main()
{
SQLConnection
.CreateSQLConnection("Server=SLASQL01.emea.sensus.net; Database=Auftrag; User ID=sa; Password=sqlserver;")
.CreateCommand(@"
SELECT CONVERT(VARBINARY(8), '0x' + REPLACE(x.Address, '-', ''), 1)
, CONVERT(VARBINARY(256), '0x' + REPLACE(x.Value, '-', ''), 1)
FROM (SELECT rh.Address
, rh.Value
, ROW_NUMBER() OVER (PARTITION BY rh.Address ORDER BY rh.date DESC) AS Row
FROM GenesisMeterRegisterHistory AS rh
WHERE rh.PcbId = @pcbId) AS x
WHERE x.Row = 1
ORDER BY x.Address")
.SetParameter("pcbId", "241330796")
.ExecuteReader(reader =>
{
var address = reader.GetValue<Byte[]>(0);
if (address.Length == 2)
{
var value = reader.GetValue<Byte[]>(1);
Console.WriteLine($"App: {address[0], 5:X2} Register: {address[1], 5:X2} Data: {BitConverter.ToString(value)}");
}
});
}
public class Cordonel : Dictionary<String, App>
@@ -712,7 +689,21 @@
//TX|01-01-01-01
//TX|9B-33-07-0D-0B-08-FD-03-00-00-E1-84-06-00-02-8B-00-54-25
//TX|01-01-01-01
//TX|9B-33-06-0D-0A-08-FD-67-00-E8-03-25-00-02-93-59-12
//RX|9B-43-07-0D-0B-00-01-00-E8-03-25-00-00-00-01-29-00-33-5A
//TX|01-01-01-01
//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F
//TX|01-01-01-01
//TX|9B-33-06-0D-0A-08-FD-67-00-06-00-1E-00-01-A7-0C-65
//TX|01-01-01-01
//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F
//TX|01-01-01-01
//TX|9B-33-06-0D-0A-08-FD-67-00-05-00-25-00-01-AD-CC-4A
//TX|01-01-01-01
//TX|9B-33-04-0D-06-08-FD-67-01-01-80-40-7F
@@ -8,27 +8,22 @@
{
}
public override Byte[] Write(params Byte[] data)
public override Byte[] Write(params Byte[] bytes)
{
var _data = new Byte[0];
var plainBytes = new Byte[0];
if (data.TryParseToIrda(out var bytes))
if (bytes.TryParseToIrda(out var irdaBytes))
{
var repeatsCount = 3;
for (var i = 0; i < repeatsCount; i++)
{
this.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4);
this.Write(bytes, 0, bytes.Length);
if (this.TXLoggingEnabled)
{
this.LogTRX(bytes);
}
this.Write(new Byte[] { 0x01, 0x01, 0x01, 0x01 }, 0, 4, 25);
this.Write(irdaBytes, 0, irdaBytes.Length);
var answerd = false;
var buffer = new Byte[0];
var readsCount = 3;
var readsCount = 100;
while (readsCount-- >= 0)
{
@@ -39,7 +34,7 @@
Array.Resize(ref buffer, bufferLength + bytesLength);
Array.Copy(_bytes, 0, buffer, bufferLength, bytesLength);
if (buffer.TryParseFromIrda(out _data))
if (buffer.TryParseFromIrda(out plainBytes))
{
answerd = true;
@@ -59,15 +54,10 @@
}
else
{
this.LogMessage($"Cannot convert: {BitConverter.ToString(bytes)} - to IRDA message.");
this.LogMessage($"Cannot convert: {BitConverter.ToString(irdaBytes)} - to IRDA message.");
}
if (this.RXLoggingEnabled)
{
this.LogTRX(_data);
}
return _data;
return plainBytes;
}
}
}
@@ -13,32 +13,23 @@
private readonly SerialPortSettings settings;
public SerialPortBase(SerialPortSettings settings)
=> this.settings = settings;
{
this.settings = settings;
this.PortName = settings.PortName;
}
public event Action<String> Logging;
public event Action<String> TRXLogging;
public String PortName
=> this.settings?.PortName;
public String PortName { get; }
public Boolean IsOpen
=> serialPorts.TryGetValue(this.PortName, out var serialPort)
? serialPort?.IsOpen ?? default(Boolean)
: default(Boolean);
protected Int32 ReadBufferSize
=> serialPorts.TryGetValue(this.PortName, out var serialPort)
? serialPort?.ReadBufferSize ?? default(Int32)
: default(Int32);
protected Boolean RXLoggingEnabled => this.settings.RXLoggingEnabled;
protected Boolean TXLoggingEnabled => this.settings.TXLoggingEnabled;
public Int32 ReadSize { get; private set; }
public virtual void Close()
{
try
{
this.ReadSize = default(Int32);
if (serialPorts.TryRemove(this.PortName, out var serialPort))
{
if (serialPort != null)
@@ -47,6 +38,7 @@
serialPort.Dispose();
serialPort = null;
this.LogMessage($"Disconnected: {this}");
}
else
@@ -54,10 +46,6 @@
this.LogMessage($"{this.PortName} is null!");
}
}
else
{
this.LogMessage($"{this.PortName} dose not exists!");
}
}
catch (Exception e)
{
@@ -67,40 +55,38 @@
public virtual void EnsureConnected()
{
if (!this.IsOpen)
if (!this.TryGetSerialPort(out var serialPort))
{
this.Close();
this.Open();
}
}
public virtual void Open()
{
this.Close();
try
{
if (serialPorts.TryGetValue(this.PortName, out var serialPort))
{
serialPort.Close();
}
else
{
this.LogMessage($"{this.PortName} dose not exists!");
}
this.ReadSize = default(Int32);
serialPort = new SerialPort
var serialPort = new SerialPort
{
BaudRate = this.settings.BaudRate,
DataBits = this.settings.DataBits,
Parity = this.settings.Parity,
PortName = this.PortName,
StopBits = this.settings.StopBits,
ReadTimeout = this.settings.RWTimeout,
PortName = this.settings.PortName,
StopBits = this.settings.StopBits
};
if (serialPorts.TryAdd(this.PortName, serialPort))
if (serialPorts.TryAdd(serialPort.PortName, serialPort))
{
serialPort.Open();
if (serialPort.IsOpen)
{
this.ReadSize = serialPort.ReadBufferSize;
}
this.LogMessage($"Connected: {this}");
}
else
@@ -136,41 +122,21 @@
protected void LogMessage(String message) => this.Logging?.Invoke(message);
public void LogTRX(Byte[] data) => this.TRXLogging?.Invoke(BitConverter.ToString(data));
public void LogTX(Byte[] data) => this.TRXLogging?.Invoke($"TX|{BitConverter.ToString(data)}");
public void LogRX(Byte[] data) => this.TRXLogging?.Invoke($"RX|{BitConverter.ToString(data)}");
protected Byte[] Read(Int32 sleep = 100)
protected Byte[] Read(Int32 sleep = 1)
{
Thread.Sleep(sleep);
var bytes = new Byte[0];
if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen)
if (this.TryGetSerialPort(out var serialPort))
{
var size = this.ReadBufferSize;
var buffer = new Byte[size];
try
{
if (serialPort.IsOpen)
{
var copyLength = serialPort.Read(buffer, 0, size);
var buffer = new Byte[this.ReadSize];
var length = serialPort.Read(buffer, 0, this.ReadSize);
Array.Resize(ref bytes, copyLength);
Array.Copy(buffer, 0, bytes, 0, copyLength);
if (this.RXLoggingEnabled)
{
this.LogMessage(BitConverter.ToString(bytes));
}
}
else
{
this.LogMessage($"{this.PortName} is closed!");
}
Array.Resize(ref bytes, length);
Array.Copy(buffer, 0, bytes, 0, length);
}
catch (Exception e)
{
@@ -187,16 +153,11 @@
protected void Write(Byte[] bytes, Int32 start, Int32 length, Int32 sleep = 1)
{
if (serialPorts.TryGetValue(this.PortName, out var serialPort) && serialPort.IsOpen)
if (this.TryGetSerialPort(out var serialPort))
{
try
{
serialPort.Write(bytes, start, length);
if (this.RXLoggingEnabled)
{
this.LogMessage(BitConverter.ToString(bytes));
}
}
catch (Exception e)
{
@@ -210,5 +171,9 @@
Thread.Sleep(sleep);
}
private Boolean TryGetSerialPort(out SerialPort serialPort)
=> serialPorts.TryGetValue(this.PortName, out serialPort)
&& serialPort?.IsOpen == true;
}
}
@@ -162,23 +162,43 @@
{
return false;
}
var start = Array.IndexOf(bytes, IRDA_START);
if (start < 0)
{
return false;
}
var direction = Array.IndexOf(bytes, IRDA_REG2PC, start + 1);
if (direction < 0)
{
return false;
}
var bytesLength = bytes.Length;
var lengthIndex = direction + 1;
var directionIX = -1;
var startIX = 0;
while (bytesLength > startIX)
{
startIX = Array.IndexOf(bytes, IRDA_START, startIX);
if (startIX < 0)
{
break;
}
directionIX = Array.IndexOf(bytes, IRDA_REG2PC, ++startIX);
if (directionIX > 0)
{
break;
}
else
{
directionIX = Array.IndexOf(bytes, (Byte)0x82, startIX++);
if (directionIX >= 0)
{
directionIX = -1;
}
}
}
if (startIX < 0)
{
return false;
}
var lengthIndex = directionIX + 1;
if (lengthIndex >= bytesLength)
{
@@ -13,11 +13,5 @@
public String PortName { get; set; }
public SerialPortStopBits StopBits { get; set; }
public Int32 RWTimeout { get; set; } = 3000;
public Boolean RXLoggingEnabled { get; set; }
public Boolean TXLoggingEnabled { get; set; }
}
}
@@ -20,11 +20,6 @@
{
this.Write(bytes, 0, bytes.Length, 50);
if (this.TXLoggingEnabled)
{
this.LogTRX(bytes);
}
var answerd = false;
var buffer = new Byte[0];
var readsCount = 3;
@@ -61,11 +56,6 @@
this.LogMessage($"Cannot convert: {BitConverter.ToString(bytes)} - to UniPro message.");
}
if (this.RXLoggingEnabled)
{
this.LogTRX(_data);
}
return _data;
}
}
@@ -17,26 +17,26 @@
StopBits = SerialPortStopBits.One,
};
this.serialPortBase = new IrdaSerialPort(settings);
this.serialPortBase.Logging += this.LogText;
}
public override Byte[] Send(params Byte[] bytes)
{
this.serialPortBase.EnsureConnected();
if (bytes.TryParseToUI1236(out var _bytes))
var plainBytes = new Byte[] { };
if (bytes.TryParseToUI1236(out var ui1236Request))
{
var __bytes = this.serialPortBase.Write(_bytes);
if (__bytes.TryParseFromUI1236(out var data))
{
this.LogTX(bytes);
this.LogRX(data);
return data;
}
this.serialPortBase
.Write(ui1236Request)
.TryParseFromUI1236(out plainBytes);
}
return _bytes;
this.LogText($"TX|{BitConverter.ToString(bytes)}");
this.LogText($"RX|{BitConverter.ToString(plainBytes)}");
return plainBytes;
}
}
}
@@ -11,13 +11,17 @@
{
private SerialPortConnection connection;
public StringBuilder Telegrams { get; private set; }
public void Disconnect() => this.connection.Disconnect();
public void UseIrda(String portName)
{
this.connection?.Disconnect();
this.Telegrams = new StringBuilder();
this.connection = new IrdaConnection(portName);
this.connection.TRXLogging += this.Logging;
this.connection.Connect();
}
@@ -32,10 +36,15 @@
{
this.connection?.Disconnect();
this.Telegrams = new StringBuilder();
this.connection = new UniProConnection(portName);
this.connection.TRXLogging += this.Logging;
this.connection.Connect();
}
private void Logging(String text)
=> this.Telegrams?.AppendLine(text);
public Byte[] Send(params Byte[] bytes)
=> this.connection.Send(bytes);
@@ -81,8 +90,6 @@
return new ReadingDigits(response);
}
public String GetRecords() => this.connection.GetRecords();
public ReadingUnits ViewReadingUnits()
{
var response = this.connection.Send(VIEW_READING_UNITS);
@@ -412,8 +419,6 @@
return new FeatureResponse(response);
}
public void StartRecording() => this.connection.StartRecording();
public FactorySeal FactorySeal(Boolean? seal = null)
{
var request = default(Byte[]);
@@ -13,17 +13,9 @@
public OMNI OMNI { get; set; }
public void ConnectToIrda()
{
this.Connection.UseIrda(this.PortName);
this.Connection.StartRecording();
}
public void ConnectToIrda() => this.Connection.UseIrda(this.PortName);
public void ConnectToUniPro()
{
this.Connection.UseUniPro(this.PortName);
this.Connection.StartRecording();
}
public void ConnectToUniPro() => this.Connection.UseUniPro(this.PortName);
public void ViewSystemParameters()
{
@@ -3,38 +3,21 @@
using Common.Hardware.Interfaces.Ports;
using System;
using System.Text;
internal abstract class SerialPortConnection
{
private readonly StringBuilder records = new StringBuilder();
protected SerialPortBase serialPortBase;
public event Action<String> TRXLogging;
public void Connect() => this.serialPortBase.Open();
public void Disconnect() => this.serialPortBase.Close();
public String GetRecords()
{
var records = this.records.ToString();
this.records.Clear();
return records;
}
public abstract Byte[] Send(params Byte[] request);
public void StartRecording()
{
this.records.Clear();
protected void LogTRX(params Byte[] bytes) => this.LogText(BitConverter.ToString(bytes));
this.serialPortBase.TRXLogging += text => this.records.AppendLine(text);
}
protected void LogTX(Byte[] bytes) => this.serialPortBase.LogTX(bytes);
protected void LogRX(Byte[] bytes) => this.serialPortBase.LogRX(bytes);
protected void LogText(String text) => this.TRXLogging?.Invoke(text);
}
}
@@ -14,11 +14,10 @@
DataBits = SerialPortDataBits.X8,
Parity = SerialPortParity.None,
PortName = portName,
RXLoggingEnabled = true,
StopBits = SerialPortStopBits.One,
TXLoggingEnabled = true,
};
this.serialPortBase = new UniProSerialPort(settings);
this.serialPortBase.Logging += this.LogText;
}
public override Byte[] Send(params Byte[] bytes)
@@ -27,8 +26,8 @@
var response = this.serialPortBase.Write(bytes);
this.LogTX(bytes);
this.LogRX(response);
this.LogText($"TX|{BitConverter.ToString(bytes)}");
this.LogText($"RX|{BitConverter.ToString(response)}");
return response;
}
@@ -2,9 +2,8 @@
{
using Common.Hardware.WaterMeter.OmniPlus;
using Common.Hardware.WaterMeter.OmniPlus.Features;
using NLog;
// using OmniPlus.Features;
using NLog;
using System;
@@ -24,8 +23,6 @@
this.logger = logger;
this.connection = new OmniConnection();
this.connection.UseIrda(portName);
this.connection.StartRecording();
// this.connection.StateChenged += this.NotifyStateChanged;
}
public int Slot { get; }
@@ -76,8 +73,6 @@
{
if (this.connection != null)
{
// this.connection.StateChenged -= this.NotifyStateChanged;
this.logger.Trace(this.connection.GetRecords());
this.connection.Disconnect();
}
@@ -146,7 +141,7 @@
DataLogIntervalMinutes = configuration.DataLogIntervalMins,
ManifacturingTime = system.ManifacturingTime.SecondsSince,
NumberOfReadingDigits = configuration.ReadingDigits,
// OptionalUniDirFields = configuration.OptionalUniDirFields,
OptionalUniDirFields = 0x00C0,
ReadingPreset = configuration.Readings.Value,
RebootCount = 0,
SystemTime = Epoch20000101.UTCNow,
@@ -9,10 +9,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
public class OmniShipmentModel
@@ -61,14 +59,13 @@
this.values = new OmniValuesModel();
this.omni.UseUniPro(this.portName);
this.omni.StartRecording();
}
internal void Finish()
{
if (!String.IsNullOrWhiteSpace(this.MeterPcb))
{
var debug = this.omni.GetRecords();
var debug = this.omni.Telegrams?.ToString();
if (!String.IsNullOrWhiteSpace(debug))
{