Fix Systec scale
This commit is contained in:
@ -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)]
|
[GeneratedRegex("^serial://([A-Za-z0-9]+):([0-9]+)(,([5-9]),([NOEMSnoems]),(0|1|1\\.5|2|))?$", RegexOptions.Compiled)]
|
||||||
private static partial Regex GeneratedSerialRegex();
|
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();
|
private static partial Regex GeneratedTcpRegex();
|
||||||
|
|
||||||
public static SerialPort OpenSerialConnection(string connection) {
|
public static SerialPort OpenSerialConnection(string connection) {
|
||||||
var m = Utils.SerialRegex.Match(connection);
|
var m = SerialRegex.Match(connection);
|
||||||
if (!m.Success)
|
if (!m.Success)
|
||||||
throw new ArgumentException("Invalid connection string for scheme \"serial\"");
|
throw new ArgumentException("Invalid connection string for scheme \"serial\"");
|
||||||
|
|
||||||
@ -45,19 +45,21 @@ namespace Elwig.Helpers {
|
|||||||
DataBits = data == "" ? 8 : int.Parse(data),
|
DataBits = data == "" ? 8 : int.Parse(data),
|
||||||
StopBits = (StopBits)(stop == "" ? 1 : stop == "1.5" ? 3 : stop[0] - '0'),
|
StopBits = (StopBits)(stop == "" ? 1 : stop == "1.5" ? 3 : stop[0] - '0'),
|
||||||
Handshake = Handshake.None,
|
Handshake = Handshake.None,
|
||||||
|
ReadTimeout = 1000,
|
||||||
|
WriteTimeout = 1000,
|
||||||
};
|
};
|
||||||
port.Open();
|
port.Open();
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TcpClient OpenTcpConnection(string connection) {
|
public static TcpClient OpenTcpConnection(string connection) {
|
||||||
var m = Utils.TcpRegex.Match(connection);
|
var m = TcpRegex.Match(connection);
|
||||||
if (!m.Success)
|
if (!m.Success)
|
||||||
throw new ArgumentException("Invalid connection string for scheme \"tcp\"");
|
throw new ArgumentException("Invalid connection string for scheme \"tcp\"");
|
||||||
|
|
||||||
var client = new TcpClient() {
|
var client = new TcpClient() {
|
||||||
SendTimeout = 250,
|
SendTimeout = 1000,
|
||||||
ReceiveTimeout = 250,
|
ReceiveTimeout = 1000,
|
||||||
};
|
};
|
||||||
client.Connect(m.Groups[1].Value, int.Parse(m.Groups[2].Value));
|
client.Connect(m.Groups[1].Value, int.Parse(m.Groups[2].Value));
|
||||||
return client;
|
return client;
|
||||||
|
@ -4,6 +4,7 @@ using System.IO.Ports;
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
|
||||||
namespace Elwig.Helpers.Weighing {
|
namespace Elwig.Helpers.Weighing {
|
||||||
public class SystecScale : IScale {
|
public class SystecScale : IScale {
|
||||||
@ -12,8 +13,7 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
|
|
||||||
protected SerialPort? Serial = null;
|
protected SerialPort? Serial = null;
|
||||||
protected TcpClient? Tcp = null;
|
protected TcpClient? Tcp = null;
|
||||||
protected StreamReader Reader;
|
protected Stream Stream;
|
||||||
protected StreamWriter Writer;
|
|
||||||
|
|
||||||
protected readonly Output? EmptyMode = null;
|
protected readonly Output? EmptyMode = null;
|
||||||
protected readonly Output? FillingClearanceMode = null;
|
protected readonly Output? FillingClearanceMode = null;
|
||||||
@ -33,13 +33,12 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
IsReady = true;
|
IsReady = true;
|
||||||
HasFillingClearance = false;
|
HasFillingClearance = false;
|
||||||
|
|
||||||
Stream stream;
|
|
||||||
if (connection.StartsWith("serial:")) {
|
if (connection.StartsWith("serial:")) {
|
||||||
Serial = Utils.OpenSerialConnection(connection);
|
Serial = Utils.OpenSerialConnection(connection);
|
||||||
stream = Serial.BaseStream;
|
Stream = Serial.BaseStream;
|
||||||
} else if (connection.StartsWith("tcp:")) {
|
} else if (connection.StartsWith("tcp:")) {
|
||||||
Tcp = Utils.OpenTcpConnection(connection);
|
Tcp = Utils.OpenTcpConnection(connection);
|
||||||
stream = Tcp.GetStream();
|
Stream = Tcp.GetStream();
|
||||||
} else {
|
} else {
|
||||||
throw new ArgumentException("Unsupported scheme");
|
throw new ArgumentException("Unsupported scheme");
|
||||||
}
|
}
|
||||||
@ -53,14 +52,10 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
WeightLimit = limit;
|
WeightLimit = limit;
|
||||||
if (FillingClearanceMode != null && WeightLimit == null)
|
if (FillingClearanceMode != null && WeightLimit == null)
|
||||||
throw new ArgumentException("Weight limit has to be set, if filling clearance supervision is enalbed");
|
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() {
|
public void Dispose() {
|
||||||
Writer.Close();
|
Stream.Close();
|
||||||
Reader.Close();
|
|
||||||
Serial?.Close();
|
Serial?.Close();
|
||||||
Tcp?.Close();
|
Tcp?.Close();
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
@ -82,11 +77,15 @@ namespace Elwig.Helpers.Weighing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendCommand(string command) {
|
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() {
|
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(">")) {
|
if (line == null || line.Length < 4 || !line.StartsWith("<") || !line.EndsWith(">")) {
|
||||||
throw new IOException("Invalid response from scale");
|
throw new IOException("Invalid response from scale");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user