Refactorization : Common.Utils.GetParity( ) / GetStopBits( ) / GetHandshake( ) ...

This commit is contained in:
Milan Hanajik 2023-03-01 10:45:48 +01:00
parent f47e7670a3
commit 6e04933eeb
12 changed files with 74 additions and 63 deletions

View File

@ -2,6 +2,7 @@
/// Copyright (c) 2021-2022 Sensus Slovensko a.s.
///
using System;
using System.IO.Ports;
namespace Common
{
@ -92,5 +93,33 @@ namespace Common
else return "F5";
}
}
public static Parity GetParity(string str, Parity defaultParity = Parity.None)
{
if (str.Equals(Parity.None.ToString())) return Parity.None;
if (str.Equals(Parity.Even.ToString())) return Parity.Even;
if (str.Equals(Parity.Odd.ToString())) return Parity.Odd;
if (str.Equals(Parity.Mark.ToString())) return Parity.Mark;
if (str.Equals(Parity.Space.ToString())) return Parity.Space;
return defaultParity;
}
public static StopBits GetStopBits(string str, StopBits defaultStopBits = StopBits.One)
{
if (str.Equals(StopBits.None.ToString())) return StopBits.None;
if (str.Equals(StopBits.One.ToString())) return StopBits.One;
if (str.Equals(StopBits.OnePointFive.ToString())) return StopBits.OnePointFive;
if (str.Equals(StopBits.Two.ToString())) return StopBits.Two;
return defaultStopBits;
}
public static Handshake GetHandshake(string str, Handshake defaultHandshake = Handshake.None)
{
if (str.Equals(Handshake.None.ToString())) return Handshake.None;
if (str.Equals(Handshake.RequestToSend.ToString())) return Handshake.RequestToSend;
if (str.Equals(Handshake.XOnXOff.ToString())) return Handshake.XOnXOff;
if (str.Equals(Handshake.RequestToSendXOnXOff.ToString())) return Handshake.RequestToSendXOnXOff;
return defaultHandshake;
}
}
}

View File

@ -88,10 +88,10 @@ namespace TBF.Rig.Ambient.Comet
config.Name = nameTextBox.Text;
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.SetDtrToOne = setDtrCheckBox.Checked;
return flags;

View File

@ -86,10 +86,10 @@ namespace TBF.Rig.Ambient.Greco
config.Name = nameTextBox.Text;
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -96,9 +96,9 @@ namespace TBF.Rig.Danfoss.VLT2800
config.BusAddress = int.Parse(busAddressTextBox.Text);
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -88,10 +88,10 @@ namespace TBF.Rig.Keithley.Multimeter_2010_RS232
config.Name = nameTextBox.Text;
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -200,10 +200,10 @@ namespace TBF.Rig.MettlerToledo.Multi
{
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
}
return flags;

View File

@ -214,10 +214,10 @@ namespace TBF.Rig.MettlerToledo.Standard
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -5,7 +5,6 @@ using System;
using System.Windows.Forms;
using System.IO.Ports;
using Common;
using Config.Entities;
using TBF.Rig.Generic;
using TBF.Resources;
@ -88,10 +87,10 @@ namespace TBF.Rig.Modbus.Common
config.Name = nameTextBox.Text;
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = global::Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = global::Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = global::Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -88,10 +88,10 @@ namespace TBF.Rig.RegisterReaders.KPackE.Radio
config.Name = nameTextBox.Text;
config.ComPortNr = int.Parse(comPortNrTextBox.Text);
config.BaudRate = int.Parse(baudRateTextBox.Text);
config.Parity = Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.Parity = Common.Utils.GetParity(parityComboBox.SelectedItem.ToString());
config.DataBits = int.Parse(dataBitsTextBox.Text);
config.StopBits = Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
config.StopBits = Common.Utils.GetStopBits(stopBitsComboBox.SelectedItem.ToString());
config.Handshake = Common.Utils.GetHandshake(handshakeComboBox.SelectedItem.ToString());
return flags;
}

View File

@ -120,10 +120,10 @@ namespace TBF.Rig.RegisterReaders.S640Stream
case 0: Position = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 1: ComPortNr = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 2: BaudRate = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 3: Parity = Utils.GetParity(str); return CfgUpdateFlags.RestartRqrd;
case 3: Parity = Common.Utils.GetParity(str); return CfgUpdateFlags.RestartRqrd;
case 4: DataBits = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 5: StopBits = Utils.GetStopBits(str); return CfgUpdateFlags.RestartRqrd;
case 6: Handshake = Utils.GetHandshake(str); return CfgUpdateFlags.RestartRqrd;
case 5: StopBits = Common.Utils.GetStopBits(str); return CfgUpdateFlags.RestartRqrd;
case 6: Handshake = Common.Utils.GetHandshake(str); return CfgUpdateFlags.RestartRqrd;
default: return CfgUpdateFlags.None;
}
}

View File

@ -123,10 +123,10 @@ namespace TBF.Rig.RegisterReaders.SerialStream
{
case 0: ComPortNr = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 1: BaudRate = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 2: Parity = Utils.GetParity(str); return CfgUpdateFlags.RestartRqrd;
case 2: Parity = Common.Utils.GetParity(str); return CfgUpdateFlags.RestartRqrd;
case 3: DataBits = int.Parse(str); return CfgUpdateFlags.RestartRqrd;
case 4: StopBits = Utils.GetStopBits(str); return CfgUpdateFlags.RestartRqrd;
case 5: Handshake = Utils.GetHandshake(str); return CfgUpdateFlags.RestartRqrd;
case 4: StopBits = Common.Utils.GetStopBits(str); return CfgUpdateFlags.RestartRqrd;
case 5: Handshake = Common.Utils.GetHandshake(str); return CfgUpdateFlags.RestartRqrd;
default: return CfgUpdateFlags.None;
}
}

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO.Ports;
using System.Linq;
using Config.Entities;
using Dirichlet.Numerics;
@ -354,32 +353,16 @@ namespace TBF
!fname.Contains('|');
}
public static Parity GetParity(string str, Parity defaultParity = Parity.None)
/// <summary>
/// Returns true when component named 'method' exists and it is a hydraulical test method.
/// </summary>
/// <param name="method">Component name</param>
/// <returns>true = is a hydraulical test method</returns>
public static bool IsHydroTest(string method)
{
if (str.Equals(Parity.None.ToString())) return Parity.None;
if (str.Equals(Parity.Even.ToString())) return Parity.Even;
if (str.Equals(Parity.Odd.ToString())) return Parity.Odd;
if (str.Equals(Parity.Mark.ToString())) return Parity.Mark;
if (str.Equals(Parity.Space.ToString())) return Parity.Space;
return defaultParity;
}
public static StopBits GetStopBits(string str, StopBits defaultStopBits = StopBits.One)
{
if (str.Equals(StopBits.None.ToString())) return StopBits.None;
if (str.Equals(StopBits.One.ToString())) return StopBits.One;
if (str.Equals(StopBits.OnePointFive.ToString())) return StopBits.OnePointFive;
if (str.Equals(StopBits.Two.ToString())) return StopBits.Two;
return defaultStopBits;
}
public static Handshake GetHandshake(string str, Handshake defaultHandshake = Handshake.None)
{
if (str.Equals(Handshake.None.ToString())) return Handshake.None;
if (str.Equals(Handshake.RequestToSend.ToString())) return Handshake.RequestToSend;
if (str.Equals(Handshake.XOnXOff.ToString())) return Handshake.XOnXOff;
if (str.Equals(Handshake.RequestToSendXOnXOff.ToString())) return Handshake.RequestToSendXOnXOff;
return defaultHandshake;
if (string.IsNullOrEmpty(method)) return false;
var tm = TBF.Rig.TbfComponents.FindComponent(method) as TBF.Rig.GenericDevices.ITestMethod;
return (tm != null) && tm.DoTransitions();
}
}
}