eRegister receive messages over WritePamTask.ReceiveMessage

This commit is contained in:
Stoyan Zlatev 2025-11-21 09:45:14 +01:00
parent 0af6b64f30
commit a4e4ee6f90
8 changed files with 153 additions and 74 deletions

View File

@ -64,6 +64,7 @@
<Compile Include="Parameters\RSSI.cs" />
<Compile Include="Parameters\SirtOperating.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SensusRF\SirtCom.cs" />
<Compile Include="SIRTBroadcaster.cs" />
<Compile Include="SIRTConstants.cs" />
<Compile Include="SIRTExtensions.cs" />

View File

@ -0,0 +1,89 @@
namespace Common.Hardware.SIRT.SensusRF
{
using System;
using System.Collections.Generic;
using System.IO.Ports;
internal class SirtCom
{
private readonly SerialPort serialPort;
public SirtCom(string portName)
=> this.serialPort = new SerialPort
{
BaudRate = 115200,
DataBits = 8,
Parity = Parity.None,
PortName = portName,
StopBits = StopBits.One
};
public bool Close()
{
try
{
this.serialPort.Close();
return this.serialPort.IsOpen;
}
catch
{
return false;
}
}
public bool Open()
{
this.Close();
try
{
this.serialPort.Open();
return this.serialPort.IsOpen;
}
catch
{
return false;
}
}
public IEnumerable<byte[]> ReadBytes()
{
var buffer = default(byte[]);
var length = default(int);
while (true)
{
buffer = new byte[this.serialPort.ReadBufferSize];
try
{
length = this.serialPort.Read(buffer, 0, buffer.Length);
Array.Resize(ref buffer, length);
}
catch
{
buffer = Array.Empty<byte>();
}
yield return buffer;
}
}
public bool WriteBytes(params byte[] bytes)
{
try
{
this.serialPort.Write(bytes, 0, bytes.Length);
return true;
}
catch
{
return false;
}
}
}
}

View File

@ -96,7 +96,7 @@
{
this.AnyTelegramReceived = true;
if (response.Command == SIRTConstants.ACKN_ANSW) // command sent
if (response.Command == SIRTConstants.ACKN_ANSW)
{
this.DecriptResponse(response);
this.AKNReceived?.Invoke(response);
@ -105,47 +105,45 @@
{
var payload = response.Payload;
if (this.UseEncryption(response))
{
this.DecriptResponse(response);
}
if (payload != null && payload.Length > 0)
{
var type = payload[0];
if (type == SIRTConstants.BUP) // bubble up message
if (type == SIRTConstants.BUP)
{
this.DecriptResponse(response, payload);
this.BUPReceived?.Invoke(response);
}
else if (type == SIRTConstants.LAT) // command received
else if (type == SIRTConstants.LAT)
{
this.latReceived = true;
this.DecriptResponse(response, payload);
this.LATReceived?.Invoke(response);
}
else if (type == SIRTConstants.DEBUG) // received only on specific commands requires lat
else if (type == SIRTConstants.DEBUG)
{
this.debugReceived = true;
this.DecriptResponse(response, payload);
this.DEBUGReceived?.Invoke(response);
}
else if (type == SIRTConstants.SEMI) // always the last answer of the command requires lat, debug only in some cases
else if (type == SIRTConstants.SEMI)
{
this.DecriptResponse(response, payload);
this.SEMIReceived?.Invoke(response);
}
else
{
this.DecriptResponse(response, payload);
this.UnknownReceived?.Invoke(response);
}
}
else
{
this.DecriptResponse(response, payload);
this.UnknownReceived?.Invoke(response);
}
}
else
{
this.DecriptResponse(response);
this.UnknownReceived?.Invoke(response);
}
@ -221,6 +219,12 @@
}
protected virtual bool UseEncryption(SIRTMessage response)
=> response != null
&& response.Payload != null
&& response.Payload.Length > 2
&& response.Payload.GetCrc() != 0;
private byte[] EncryptRequest()
{
var requestPayload = this.request.Payload;
@ -240,19 +244,11 @@
return payload;
}
private void DecriptResponse(SIRTMessage response, params byte[] payload)
private void DecriptResponse(SIRTMessage response)
{
if (payload == null || payload.Length == 0)
{
return;
}
this.IsEncrypted = true;
this.IsEncrypted = payload.GetCrc(payload.Length - 2) != default(ushort);
if (payload.GetCrc(payload.Length - 2) != default(ushort))
{
response.Payload = payload.DecryptResponse(this.EncryptionKey);
}
response.Payload = response.Payload.DecryptResponse(this.EncryptionKey);
}
private void SetP811(Action<P811> p811Setter)

View File

@ -81,11 +81,6 @@
this.executionType = nameof(LoadInspectionFinalizationTasks);
var features = this.parameters.LoadInspectionFinalizationFeatures();
//if (this.parameters.Completed)
//{
// return new EregisterTask[] { };
//}
if (features.RequestAddress > 0 && features.ResponseAddress > 0)
{
this.LoadProgrammingParameters(features);
@ -101,24 +96,22 @@
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
IsEncrypted = this.parameters.IsEncrypted,
});
//this.runningTasks.Enqueue(new EregisterTask(this.parameters.Frequency)
//{
// Name = "0, 2, 16",
// SlotNr = features.SlotNr,
// PoNr = features.PoNr,
// SerialNr = features.SerialNr,
// RequestAddress = features.RequestAddress,
// ResponseAddress = features.ResponseAddress,
// EncryptionKey = features.EncryptionKey,
// IsEncrypted = this.parameters.IsEncrypted,
// Data = new ChangeWakeUp((WakeUpMode)this.parameters.Wake_Up_Interval)
// .Append(new ChangeNumberOfLatWindows(this.parameters.LAT_Interval))
// .Append(new ChangeTransmissionInterval(this.parameters.Transmission_Intervall))
// .Append(new ProvidePin(AuthLevel.II)),
//});
this.runningTasks.Enqueue(new EregisterTask(this.parameters.Frequency)
{
Name = "0, 2, 16",
SlotNr = features.SlotNr,
PoNr = features.PoNr,
SerialNr = features.SerialNr,
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
Data = new ChangeWakeUp((WakeUpMode)this.parameters.Wake_Up_Interval)
.Append(new ChangeNumberOfLatWindows(this.parameters.LAT_Interval))
.Append(new ChangeTransmissionInterval(this.parameters.Transmission_Intervall))
.Append(new ProvidePin(AuthLevel.II)),
});
}
}
@ -267,21 +260,23 @@
var frequency = this.parameters.Frequency;
//this.runningTasks.Enqueue(new EregisterTask(frequency)
//{
// Name = "WUP",
// SlotNr = features.SlotNr,
// PoNr = features.PoNr,
// SerialNr = features.SerialNr,
// RequestAddress = features.RequestAddress,
// ResponseAddress = features.ResponseAddress,
// EncryptionKey = features.EncryptionKey,
// IsEncrypted = this.parameters.IsEncrypted,
// Data = new ChangeWakeUp(WakeUpMode.StayAwake)
// .Append(new ChangeNumberOfLatWindows(this.parameters.LatWindowsInitialization))
// .Append(new ChangeTransmissionInterval(this.parameters.TransmissionIntervalInitialization))
// .Append(new ProvidePin(AuthLevel.II)),
//});
if (this.executionType == nameof(this.LoadInspectionInitializationTasks))
{
this.runningTasks.Enqueue(new EregisterTask(frequency)
{
Name = "WUP",
SlotNr = features.SlotNr,
PoNr = features.PoNr,
SerialNr = features.SerialNr,
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
Data = new ChangeWakeUp(WakeUpMode.StayAwake)
.Append(new ChangeNumberOfLatWindows(this.parameters.LatWindowsInitialization))
.Append(new ChangeTransmissionInterval(this.parameters.TransmissionIntervalInitialization))
.Append(new ProvidePin(AuthLevel.II)),
});
}
this.runningTasks.Enqueue(new EregisterCheckInitializationTask(this.parameters, frequency)
{
@ -291,7 +286,6 @@
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
IsEncrypted = this.parameters.IsEncrypted,
});
foreach (var pam in features.BuildPams())
@ -304,7 +298,6 @@
RequestAddress = features.RequestAddress,
ResponseAddress = features.ResponseAddress,
EncryptionKey = features.EncryptionKey,
IsEncrypted = this.parameters.IsEncrypted,
Data = pam.Value,
Name = pam.Key
});

View File

@ -58,11 +58,6 @@
eregister.StartProgrammingAsync(cancellationToken);
}
});
//foreach (var eregister in this.eregisters)
//{
// eregister.StartProgrammingAsync(cancellationToken);
//}
}
public void StopProgramming()

View File

@ -123,7 +123,6 @@
this.P11 = new P11(message.P1);
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
this.BUPMessage = new BUP(message.Payload, this.P11);
this.IsEncrypted = this.BUPMessage.Crc != 0;
this.AnyTelegramReceived = true;
this.Label = nameof(BUP);
this.SetRunning();
@ -135,7 +134,6 @@
this.P11 = new P11(message.P1);
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
this.LATMessage = new LAT(message.Payload, this.P11);
this.IsEncrypted = this.LATMessage.Crc != 0;
this.AnyTelegramReceived = true;
this.Label = nameof(LAT);
this.SetRunning();
@ -148,7 +146,6 @@
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
this.DEBUGMessage = new DEBUG(message.Payload, this.P11);
this.FactoryState = this.DEBUGMessage.FactoryState;
this.IsEncrypted = this.DEBUGMessage.Crc != 0;
this.AnyTelegramReceived = true;
this.Label = nameof(DEBUG);
@ -167,7 +164,6 @@
this.P11 = new P11(message.P1);
this.RSSI = new RSSI(message.P2, (ushort)this.Frequency);
this.SEMIMessage = new SEMI(message.Payload, this.Frequency, this.P11);
this.IsEncrypted = this.SEMIMessage.Crc != 0;
this.AnyTelegramReceived = true;
if (this.RequireLAT && !this.latReceived)
@ -206,5 +202,14 @@
this.Label = "UNKN";
this.SetPending();
}
protected override Boolean UseEncryption(SIRTMessage response)
{
var payloadLength = response.Payload.Length;
return (payloadLength == 13 && base.UseEncryption(response))
|| payloadLength == 54
|| payloadLength == 93;
}
}
}

View File

@ -382,9 +382,9 @@
Frequency = this.Frequency,
EncryptionKey = this.EncryptionKey.GetEncryptionKey(),
/////////////////////////////////////////////////////
// ChangeWakeUp = WakeUpMode.StayAwake,
// ChangeTransmissionInterval = this.TransmissionIntervalInitialization,
// ChangeNumberOfLatWindows = this.LatWindowsInitialization,
ChangeWakeUp = WakeUpMode.StayAwake,
ChangeTransmissionInterval = this.TransmissionIntervalInitialization,
ChangeNumberOfLatWindows = this.LatWindowsInitialization,
ChangeMeterReading = 111_111_111,
WirelessMBusMode = this.MBusStatus,
SetMetrologyParameters = new SetMetrologyParameters

View File

@ -22,7 +22,7 @@
}
this.sharedId = AuftragDbContext.P2000Shared.FindSharedId(psnr, "EregisterFinalize");
var parametersList = AuftragDbContext.LoadEregisterProgrammingParametersListForSharedId(this.sharedId, true);
var parametersList = AuftragDbContext.LoadEregisterProgrammingParametersListForSharedId(this.sharedId);
this.LoadEncryptionKeys(parametersList);