Files
laatzen/Common/Hardware/Interfaces/Ports/Common.Hardware.Interfaces.Ports.SIRT/JustSirtMessage.cs
T
2024-10-14 15:02:21 +02:00

167 lines
4.4 KiB
C#

namespace Common.Hardware.Interfaces.Ports.SIRT
{
using System;
public class JustSirtMessage
{
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 readonly DateTime timestamp;
private Byte[] bytes;
public JustSirtMessage(DateTime? timestamp = null)
{
this.timestamp = timestamp ?? DateTime.Now;
this.bytes = new Byte[MIN_LEN];
this.bytes[0] = PC2SIRT;
}
public JustSirtMessage(Byte[] bytes, DateTime? timestamp = null)
{
this.timestamp = timestamp ?? DateTime.Now;
this.bytes = bytes;
}
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 UInt32 Address
{
get
{
var address = 0U;
address ^= (UInt32)this.bytes[4] << 24;
address ^= (UInt32)this.bytes[5] << 16;
address ^= (UInt32)this.bytes[6] << 8;
address ^= (UInt32)this.bytes[7];
return address;
}
set
{
this.bytes[4] = (Byte)(value >> 24);
this.bytes[5] = (Byte)(value >> 16);
this.bytes[6] = (Byte)(value >> 8);
this.bytes[7] = (Byte)value;
}
}
public Byte Length => this.bytes[8];
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 UInt16 CRC
{
get
{
var length = this.bytes.Length;
var crc = default(UInt16);
crc ^= (UInt16)(this.bytes[length - 3] << 8);
crc ^= (UInt16)(this.bytes[length - 2] << 0);
return crc;
}
}
public Byte[] ToByteArray()
{
var length = this.bytes.Length;
var crc = this.CRCCCITT(1, length - 3);
this.bytes[length - 3] = (Byte)(crc >> 8);
this.bytes[length - 2] = (Byte)(crc >> 0);
this.bytes[length - 1] = STOP;
return this.bytes;
}
public override String ToString()
=> BitConverter.ToString(this.ToByteArray());
public static implicit operator Byte[](JustSirtMessage message)
=> message.ToByteArray() ?? new Byte[0];
private UInt16 CRCCCITT(Int32 start, Int32 length)
{
var crc = (UInt16)0xFFFF;
for (var b = start; b < length; b++)
{
var _byte = this.bytes[b];
var token = (UInt16)((_byte << 8) & 0xFF00);
for (var i = 0; i < 8; i++)
{
if (0x8000 == ((crc ^ token) & 0x8000))
{
crc <<= 1;
crc ^= 0x1021;
}
else
{
crc <<= 1;
}
token <<= 1;
}
}
return crc;
}
}
}