Fix Systec scale

This commit is contained in:
2023-08-10 18:38:53 +02:00
parent e7cb698026
commit c65ffaf161
2 changed files with 18 additions and 17 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;
namespace Elwig.Helpers.Weighing {
public class SystecScale : IScale {
@ -12,8 +13,7 @@ namespace Elwig.Helpers.Weighing {
protected SerialPort? Serial = null;
protected TcpClient? Tcp = null;
protected StreamReader Reader;
protected StreamWriter Writer;
protected Stream Stream;
protected readonly Output? EmptyMode = null;
protected readonly Output? FillingClearanceMode = null;
@ -33,13 +33,12 @@ namespace Elwig.Helpers.Weighing {
IsReady = true;
HasFillingClearance = false;
Stream stream;
if (connection.StartsWith("serial:")) {
Serial = Utils.OpenSerialConnection(connection);
stream = Serial.BaseStream;
Stream = Serial.BaseStream;
} else if (connection.StartsWith("tcp:")) {
Tcp = Utils.OpenTcpConnection(connection);
stream = Tcp.GetStream();
Stream = Tcp.GetStream();
} else {
throw new ArgumentException("Unsupported scheme");
}
@ -53,14 +52,10 @@ namespace Elwig.Helpers.Weighing {
WeightLimit = limit;
if (FillingClearanceMode != null && WeightLimit == null)
throw new ArgumentException("Weight limit has to be set, if filling clearance supervision is enalbed");
Writer = new(stream, Encoding.ASCII, -1, true);
Reader = new(stream, Encoding.ASCII, false, -1, true);
}
public void Dispose() {
Writer.Close();
Reader.Close();
Stream.Close();
Serial?.Close();
Tcp?.Close();
GC.SuppressFinalize(this);
@ -82,11 +77,15 @@ namespace Elwig.Helpers.Weighing {
}
public async Task SendCommand(string command) {
await Writer.WriteAsync($"<{command}>");
byte[] bytes = Encoding.ASCII.GetBytes($"<{command}>");
await Stream.WriteAsync(bytes);
}
public async Task<string> ReceiveResponse() {
var line = await Reader.ReadLineAsync();
string? line = null;
using (var reader = new StreamReader(Stream, Encoding.ASCII, false, -1, true)) {
line = await reader.ReadLineAsync();
}
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
throw new IOException("Invalid response from scale");
}