Add more scale types

This commit is contained in:
2023-04-26 21:01:06 +02:00
parent 6b6aba1073
commit d6059e724b
5 changed files with 158 additions and 35 deletions

View File

@ -7,12 +7,62 @@ using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using System.IO.Ports;
using PdfSharp.Charting;
using System.Net.Sockets;
namespace Elwig.Helpers {
public static class Utils {
public static partial class Utils {
public static int CurrentSeason => DateTime.Now.Year - (DateTime.Now.Month <= 3 ? 1 : 0);
public static readonly Regex SerialRegex = GeneratedSerialRegex();
public static readonly Regex TcpRegex = GeneratedTcpRegex();
[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)]
private static partial Regex GeneratedTcpRegex();
public static SerialPort OpenSerialConnection(string connection) {
var m = Utils.SerialRegex.Match(connection);
if (!m.Success)
throw new ArgumentException("Invalid connection string for scheme \"serial\"");
var stop = m.Groups[6].Value;
var parity = m.Groups[5].Value.ToUpper();
var data = m.Groups[4].Value;
var port = new SerialPort() {
PortName = m.Groups[1].Value,
BaudRate = int.Parse(m.Groups[2].Value),
Parity = parity == "E" ? Parity.Even :
parity == "O" ? Parity.Odd :
parity == "M" ? Parity.Mark :
parity == "S" ? Parity.Space :
Parity.None,
DataBits = data == "" ? 8 : int.Parse(data),
StopBits = (StopBits)(stop == "" ? 1 : stop == "1.5" ? 3 : stop[0] - '0'),
Handshake = Handshake.None,
};
port.Open();
return port;
}
public static TcpClient OpenTcpConnection(string connection) {
var m = Utils.TcpRegex.Match(connection);
if (!m.Success)
throw new ArgumentException("Invalid connection string for scheme \"tcp\"");
var client = new TcpClient() {
SendTimeout = 250,
ReceiveTimeout = 250,
};
client.Connect(m.Groups[1].Value, int.Parse(m.Groups[2].Value));
return client;
}
public static void SetInputChanged(Control input) {
var brush = Brushes.Orange;
if (input is ComboBox cb) {