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

@ -23,11 +23,11 @@ namespace Elwig.Helpers {
[GeneratedRegex("^serial://([A-Za-z0-9]+):([0-9]+)(,([5-9]),([NOEMSnoems]),(0|1|1\\.5|2|))?$", RegexOptions.Compiled)]
private static partial Regex GeneratedSerialRegex();
[GeneratedRegex("^tcp://[A-Za-z0-9:._-]+(:[0-9]+)?$", RegexOptions.Compiled)]
[GeneratedRegex("^tcp://([A-Za-z0-9._-]+):([0-9]+)$", RegexOptions.Compiled)]
private static partial Regex GeneratedTcpRegex();
public static SerialPort OpenSerialConnection(string connection) {
var m = Utils.SerialRegex.Match(connection);
var m = SerialRegex.Match(connection);
if (!m.Success)
throw new ArgumentException("Invalid connection string for scheme \"serial\"");
@ -45,19 +45,21 @@ namespace Elwig.Helpers {
DataBits = data == "" ? 8 : int.Parse(data),
StopBits = (StopBits)(stop == "" ? 1 : stop == "1.5" ? 3 : stop[0] - '0'),
Handshake = Handshake.None,
ReadTimeout = 1000,
WriteTimeout = 1000,
};
port.Open();
return port;
}
public static TcpClient OpenTcpConnection(string connection) {
var m = Utils.TcpRegex.Match(connection);
var m = TcpRegex.Match(connection);
if (!m.Success)
throw new ArgumentException("Invalid connection string for scheme \"tcp\"");
var client = new TcpClient() {
SendTimeout = 250,
ReceiveTimeout = 250,
SendTimeout = 1000,
ReceiveTimeout = 1000,
};
client.Connect(m.Groups[1].Value, int.Parse(m.Groups[2].Value));
return client;

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");
}