Fix things after testing with virtual serial port

This commit is contained in:
2026-07-07 15:28:50 +02:00
parent 08d12d5307
commit fdb7976dda
4 changed files with 45 additions and 14 deletions
+8 -8
View File
@@ -31,15 +31,15 @@ namespace PamhagenSysCtrl.Helpers {
protected async Task SendCommand(string cmd, string? data = null, bool checksum = true) {
// max 255 bytes
byte[] d = new byte[255];
int n = Encoding.ASCII.GetBytes($"(A{1:02}{cmd}", d);
int n = Encoding.ASCII.GetBytes($"(A{1:00}{cmd}", d);
if (data != null) {
if (data.Length > 244) throw new ArgumentException("T1 playload too long");
n += Encoding.ASCII.GetBytes(data, 0, data.Length, d, n);
}
if (checksum) {
d[n++] = (byte)'&';
byte chk = d.Take(n).Aggregate((s, c) => s = (byte)((s + c) & 0xFF));
n += Encoding.ASCII.GetBytes($"{chk:02X}", 0, 2, d, n);
byte chk = d.Take(n).Aggregate((s, c) => (byte)((s + c) & 0xFF));
n += Encoding.ASCII.GetBytes($"{chk:X2}", 0, 2, d, n);
}
d[n++] = (byte)')';
d[n++] = (byte)'\r';
@@ -55,13 +55,13 @@ namespace PamhagenSysCtrl.Helpers {
}
var chk1 = (byte)Convert.ToInt32(line[^4..^2], 16);
var chk2 = Encoding.ASCII.GetBytes(line).SkipLast(5).Aggregate((s, c) => (byte)((s + c) & 0xFF));
var chk2 = Encoding.ASCII.GetBytes(line).SkipLast(4).Aggregate((s, c) => (byte)((s + c) & 0xFF));
if (chk1 != chk2) {
throw new IOException("Ungültige Prüfsumme von SPS (T1)");
throw new IOException($"Ungültige Prüfsumme von SPS (T1)");
}
var (cmd, data) = (line[3..5], line[5..^5]);
if (cmd == "ST" && data[4] == '6') {
var (cmd, data) = (line[4..6], line[6..^5]);
if (cmd == "ST" && data[3] == '6') {
// TODO check for other error bits?
await SendCommand("ER");
var (_, res) = await ReceiveResponse("ER");
@@ -124,7 +124,7 @@ namespace PamhagenSysCtrl.Helpers {
}
public async Task<Status> DataWrite(params (string Register, ushort[] Values)[] registers) {
await SendCommand("DW", string.Join(',', registers.Select(r => $"{r.Register},{r.Values.Length}," + string.Join(',', r.Values.Select(v => $"{v:04X}")))));
await SendCommand("DW", string.Join(',', registers.Select(r => $"{r.Register},{r.Values.Length}," + string.Join(',', r.Values.Select(v => $"{v:X4}")))));
var (_, res) = await ReceiveResponse("ST");
return new Status((Mode)(res[3] - '0'), ((res[0] - '0') << 4) | (res[1] - '0'));
}