e-register programming done
This commit is contained in:
@@ -1,161 +1,123 @@
|
||||
namespace Common.Hardware.SIRT
|
||||
{
|
||||
using Common.Hardware.SIRT.Parameters;
|
||||
|
||||
using System;
|
||||
|
||||
using static SIRTConstants;
|
||||
|
||||
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;
|
||||
this.bytes[0] = PC_SIRT;
|
||||
}
|
||||
|
||||
public SIRTMessage(byte[] bytes) : this()
|
||||
public SIRTMessage(byte[] bytes)
|
||||
=> this.bytes = bytes;
|
||||
|
||||
public byte Start
|
||||
public byte Direction
|
||||
{
|
||||
get => this.bytes[0];
|
||||
set => this.bytes[0] = value;
|
||||
get => this.bytes[DIR_IX];
|
||||
set => this.bytes[DIR_IX] = value;
|
||||
}
|
||||
|
||||
public byte Cmd
|
||||
public byte Command
|
||||
{
|
||||
get => this.bytes[1];
|
||||
set => this.bytes[1] = value;
|
||||
get => this.bytes[CMD_IX];
|
||||
set => this.bytes[CMD_IX] = value;
|
||||
}
|
||||
|
||||
public byte P1
|
||||
{
|
||||
get => this.bytes[2];
|
||||
set => this.bytes[2] = value;
|
||||
get => this.bytes[P1_IX];
|
||||
set => this.bytes[P1_IX] = value;
|
||||
}
|
||||
|
||||
public byte P2
|
||||
{
|
||||
get => this.bytes[3];
|
||||
set => this.bytes[3] = value;
|
||||
get => this.bytes[P2_IX];
|
||||
set => this.bytes[P2_IX] = value;
|
||||
}
|
||||
|
||||
public uint Address
|
||||
{
|
||||
get => this.bytes.ToUInt32BE(4);
|
||||
set => value.GetBytesBE().CopyTo(this.bytes, 4);
|
||||
get => this.bytes.GetAddress(ADDR_IX);
|
||||
set => this.bytes.SetAddress(ADDR_IX, value);
|
||||
}
|
||||
|
||||
public byte Length
|
||||
{
|
||||
get => this.bytes[8];
|
||||
set => this.bytes[8] = value;
|
||||
get => this.bytes[LEN_IX];
|
||||
set => this.bytes[LEN_IX] = value;
|
||||
}
|
||||
|
||||
public byte[] Data
|
||||
public byte[] Payload
|
||||
{
|
||||
get
|
||||
{
|
||||
var data = new byte[this.Length];
|
||||
var payload = new byte[this.Length];
|
||||
|
||||
Array.Copy(this.bytes, 9, data, 0, this.Length);
|
||||
Array.Copy(bytes, DATA_IX, payload, 0, this.Length);
|
||||
|
||||
return data;
|
||||
return payload;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
value = new byte[0];
|
||||
}
|
||||
|
||||
this.bytes[8] = (byte)value.Length;
|
||||
var payload = value ?? Array.Empty<byte>();
|
||||
this.Length = (byte)payload.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);
|
||||
Array.Clear(this.bytes, DATA_IX, this.bytes.Length - DATA_IX);
|
||||
Array.Copy(payload, 0, this.bytes, DATA_IX, this.Length);
|
||||
}
|
||||
}
|
||||
|
||||
public ushort CRC
|
||||
public ushort Crc
|
||||
{
|
||||
get => this.bytes.ToUInt16BE(this.bytes.Length - 3);
|
||||
set => value.GetBytesBE().CopyTo(this.bytes, this.bytes.Length - 3);
|
||||
get => this.bytes.GetCrc(this.bytes.Length - 3);
|
||||
set => this.bytes.SetCrc(this.bytes.Length - 3, value);
|
||||
}
|
||||
|
||||
public byte Stop
|
||||
public byte End
|
||||
{
|
||||
get => this.bytes[this.bytes.Length - 1];
|
||||
set => this.bytes[this.bytes.Length - 1] = value;
|
||||
}
|
||||
|
||||
public bool IsValid { get; private set; } = true;
|
||||
|
||||
public string SirtId { get; set; }
|
||||
|
||||
public int Frequency { get; set; }
|
||||
|
||||
public DateTimeOffset Timestamp { get; set; } = DateTimeOffset.UtcNow;
|
||||
|
||||
public override string ToString()
|
||||
=> $"{this.Timestamp}|{this.SirtId}|{this.Frequency}|{BitConverter.ToString(this.GetBytes())}";
|
||||
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
this.CRC = this.bytes.CRCCCITT(1, this.bytes.Length - 3);
|
||||
this.Stop = STOP;
|
||||
var crc = this.bytes.CRCCCITT(1, this.bytes.Length - 3);
|
||||
|
||||
if (this.Direction == PC_SIRT)
|
||||
{
|
||||
this.Crc = crc;
|
||||
this.End = MSG_END;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.IsValid = this.Crc == crc;
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
public static implicit operator byte[] (SIRTMessage message)
|
||||
=> message?.GetBytes() ?? Array.Empty<byte>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user