Fix Waagen
This commit is contained in:
@ -4,7 +4,6 @@ using System.IO.Ports;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace Elwig.Helpers.Weighing {
|
||||
public class SystecScale : IScale {
|
||||
@ -76,12 +75,12 @@ namespace Elwig.Helpers.Weighing {
|
||||
throw new ArgumentException("Invalid value for argument empty");
|
||||
}
|
||||
|
||||
public async Task SendCommand(string command) {
|
||||
protected async Task SendCommand(string command) {
|
||||
byte[] bytes = Encoding.ASCII.GetBytes($"<{command}>");
|
||||
await Stream.WriteAsync(bytes);
|
||||
}
|
||||
|
||||
public async Task<string> ReceiveResponse() {
|
||||
protected async Task<string> ReceiveResponse() {
|
||||
string? line = null;
|
||||
using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) {
|
||||
line = await reader.ReadLineAsync();
|
||||
@ -93,7 +92,7 @@ namespace Elwig.Helpers.Weighing {
|
||||
var error = line[1..3];
|
||||
if (error[0] == '0') {
|
||||
if (error[1] != '0') {
|
||||
throw new IOException($"Invalid response from scale (error code {error}");
|
||||
throw new IOException($"Invalid response from scale (error code {error})");
|
||||
}
|
||||
} else if (error[0] == '1') {
|
||||
string msg = $"Unbekannter Fehler (Fehler code {error})";
|
||||
@ -118,32 +117,41 @@ namespace Elwig.Helpers.Weighing {
|
||||
throw new IOException($"Invalid response from scale (error code {error})");
|
||||
}
|
||||
|
||||
return line[3..(line.Length - 1)];
|
||||
return line[1..(line.Length - 1)];
|
||||
}
|
||||
|
||||
protected async Task<WeighingResult> Weigh(bool incIdentNr) {
|
||||
await SendCommand(incIdentNr ? $"RM{InternalScaleNr}" : $"RN{InternalScaleNr}");
|
||||
string line = await ReceiveResponse();
|
||||
await SendCommand(incIdentNr ? $"RN{InternalScaleNr}" : $"RM{InternalScaleNr}");
|
||||
string record = await ReceiveResponse();
|
||||
if (record.Length != 62)
|
||||
throw new IOException("Invalid response from scale: Received record has invalid size");
|
||||
var line = record[2..];
|
||||
|
||||
var status = line[ 0.. 2];
|
||||
var date = line[ 2..10];
|
||||
var time = line[10..15];
|
||||
var identNr = line[15..19];
|
||||
var identNr = line[15..19].Trim();
|
||||
var scaleNr = line[19..20];
|
||||
var brutto = line[20..28];
|
||||
var tara = line[28..36];
|
||||
var netto = line[36..44];
|
||||
var brutto = line[20..28].Trim();
|
||||
var tara = line[28..36].Trim();
|
||||
var netto = line[36..44].Trim();
|
||||
var unit = line[44..46];
|
||||
var taraCode = line[46..48];
|
||||
var zone = line[48..49];
|
||||
var terminalNr = line[49..52];
|
||||
var crc16 = line[52..60];
|
||||
var zone = line[48..49].Trim();
|
||||
var terminalNr = line[49..52].Trim();
|
||||
var crc16 = line[52..60].Trim();
|
||||
|
||||
if (Utils.CalcCrc16Modbus(record[..54]).ToString() != crc16) {
|
||||
throw new IOException($"Invalid response from scale: Invalid CRC16 checksum ({crc16} != {Utils.CalcCrc16Modbus(record[..54]).ToString()})");
|
||||
} else if (unit != "kg") {
|
||||
throw new IOException($"Unsupported unit in weighing response: '{unit}'");
|
||||
}
|
||||
|
||||
return new() {
|
||||
Weight = int.TryParse(netto.Trim(), out int w) ? w : null,
|
||||
WeighingId = identNr.Trim().Length > 0 ? identNr.Trim() : null,
|
||||
Date = date,
|
||||
Time = time,
|
||||
Weight = int.TryParse(netto, out int w) ? w : null,
|
||||
WeighingId = identNr.Length > 0 && identNr != "0" ? identNr : null,
|
||||
Date = DateOnly.Parse(date),
|
||||
Time = TimeOnly.Parse(time),
|
||||
};
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user