Fixes in Scales

This commit is contained in:
2023-08-11 22:11:44 +02:00
parent 041e674af9
commit b11dcd273d
7 changed files with 39 additions and 19 deletions

View File

@ -4,6 +4,7 @@ using System.IO.Ports;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;
namespace Elwig.Helpers.Weighing {
public class SystecScale : IScale {
@ -25,12 +26,14 @@ namespace Elwig.Helpers.Weighing {
public bool IsReady { get; private set; }
public bool HasFillingClearance { get; private set; }
public int? WeightLimit { get; private set; }
public string? LogPath { get; private set; }
public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null) {
public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null, string? log = null) {
ScaleId = id;
Model = model;
IsReady = true;
HasFillingClearance = false;
LogPath = log;
if (connection.StartsWith("serial:")) {
Serial = Utils.OpenSerialConnection(connection);
@ -61,29 +64,27 @@ namespace Elwig.Helpers.Weighing {
}
protected static Output? ConvertOutput(string? value) {
if (value == null) return null;
value = value.ToUpper();
if (value == "RTS") {
return Output.RTS;
} else if (value == "DTR") {
return Output.DTR;
} else if (value == "OUT1") {
return Output.OUT1;
} else if (value == "OUT2") {
return Output.OUT2;
}
throw new ArgumentException("Invalid value for argument empty");
return value switch {
null => null,
"RTS" => Output.RTS,
"DTR" => Output.DTR,
"OUT1" => Output.OUT1,
"OUT2" => Output.OUT2,
_ => throw new ArgumentException($"Invalid value for argument: '{value}'"),
};
}
protected async Task SendCommand(string command) {
byte[] bytes = Encoding.ASCII.GetBytes($"<{command}>");
await Stream.WriteAsync(bytes);
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"<{command}>");
}
protected async Task<string> ReceiveResponse() {
string? line = null;
using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) {
line = await reader.ReadLineAsync();
if (LogPath != null) await File.AppendAllTextAsync(LogPath, $"{line}\r\n");
}
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
throw new IOException("Invalid response from scale");