Files
laatzen/Common/Hardware/Common.Hardware.SIRT/SIRTMessage.cs
T
2025-02-20 15:27:22 +01:00

162 lines
4.2 KiB
C#

namespace Common.Hardware.SIRT
{
using Common.Hardware.SIRT.Parameters;
using System;
public class SIRTMessage
{
public const byte MIN_LEN = 12;
public const byte PC2SIRT = 0x55;
public const byte SIRT2PC = 0xFF;
public const byte W_PAM = 0x81;
public const byte R_PAM = 0x82;
public const byte W_REG = 0x84;
public const byte R_REG = 0x87;
public const byte FROMAIR = 0x11;
public const byte ACKNANSW = 0x12;
public const byte STOP = 0x16;
public const byte BUP = 0x00;
public const byte LAT = 0x01;
public const byte DEBUG = 0x0B;
public const byte SEMI = 0x08;
private byte[] bytes;
public SIRTMessage()
{
this.bytes = new byte[MIN_LEN];
this.Start = PC2SIRT;
this.Stop = STOP;
}
public SIRTMessage(byte[] bytes) : this()
=> this.bytes = bytes;
public byte Start
{
get => this.bytes[0];
set => this.bytes[0] = value;
}
public byte Cmd
{
get => this.bytes[1];
set => this.bytes[1] = value;
}
public byte P1
{
get => this.bytes[2];
set => this.bytes[2] = value;
}
public byte P2
{
get => this.bytes[3];
set => this.bytes[3] = value;
}
public uint Address
{
get => this.bytes.ToUInt32BE(4);
set => value.GetBytesBE().CopyTo(this.bytes, 4);
}
public byte Length
{
get => this.bytes[8];
set => this.bytes[8] = value;
}
public byte[] Data
{
get
{
var data = new byte[this.Length];
Array.Copy(this.bytes, 9, data, 0, this.Length);
return data;
}
set
{
if (value is null)
{
value = new byte[0];
}
this.bytes[8] = (byte)value.Length;
Array.Resize(ref this.bytes, MIN_LEN + this.Length);
Array.Clear(this.bytes, 9, this.bytes.Length - 9);
Array.Copy(value, 0, this.bytes, 9, this.Length);
}
}
public ushort CRC
{
get => this.bytes.ToUInt16BE(this.bytes.Length - 3);
set => value.GetBytesBE().CopyTo(this.bytes, this.bytes.Length - 3);
}
public byte Stop
{
get => this.bytes[this.bytes.Length - 1];
set => this.bytes[this.bytes.Length - 1] = value;
}
public byte[] GetBytes()
{
this.CRC = this.bytes.CRCCCITT(1, this.bytes.Length - 3);
this.Stop = STOP;
return this.bytes;
}
public override string ToString()
=> BitConverter.ToString(this.bytes);
public static implicit operator byte[] (SIRTMessage message)
=> message.bytes;
public static implicit operator SIRTMessage(byte[] bytes)
=> new SIRTMessage(bytes);
internal static SIRTMessage Activate = new SIRTMessage
{
Cmd = W_REG,
Address = 0x20000000,
P2 = 0x05,
Data = new byte[]
{
new SirtOperating { /* default */ },
new BluetoothOperating { BtOff = true },
new RFModuleOperating { RfOff = true },
new RFModuleOperating { OpMode = OperatingModes.ReceiveF1TransmitSensusRFF3 },
new PamTimeout { Minutes = 1 }
}
};
internal static SIRTMessage Identify = new SIRTMessage
{
Cmd = SIRTMessage.R_REG,
Address = 0x11001820,
P2 = 0x08
};
internal static readonly SIRTMessage ReadPamPool = new SIRTMessage
{
Cmd = R_PAM,
Address = 0xFFFFFFFF
};
internal static SIRTMessage DeleteFromPamPool(uint address) => new SIRTMessage
{
Cmd = W_PAM,
P1 = new P811 { DelPamAdr = true },
Address = address
};
}
}