mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
Added serial port support
- complete restructure of codebase
This commit is contained in:
@@ -13,6 +13,7 @@ using Microsoft.Win32;
|
||||
using MewtocolNet.ComCassette;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace Examples;
|
||||
|
||||
@@ -21,7 +22,7 @@ public class ExampleScenarios {
|
||||
public void SetupLogger () {
|
||||
|
||||
//attaching the logger
|
||||
Logger.LogLevel = LogLevel.Error;
|
||||
Logger.LogLevel = LogLevel.Verbose;
|
||||
Logger.OnNewLogMessage((date, level, msg) => {
|
||||
|
||||
if (level == LogLevel.Error) Console.ForegroundColor = ConsoleColor.Red;
|
||||
@@ -38,7 +39,7 @@ public class ExampleScenarios {
|
||||
public async Task RunDisposalAndDisconnectAsync () {
|
||||
|
||||
//automatic disposal
|
||||
using (var interf = new MewtocolInterface("192.168.115.210")) {
|
||||
using (var interf = Mewtocol.Ethernet("192.168.115.210")) {
|
||||
|
||||
await interf.ConnectAsync();
|
||||
|
||||
@@ -55,7 +56,7 @@ public class ExampleScenarios {
|
||||
Console.WriteLine("Disposed, closed connection");
|
||||
|
||||
//manual close
|
||||
var interf2 = new MewtocolInterface("192.168.115.210");
|
||||
var interf2 = Mewtocol.Ethernet("192.168.115.210");
|
||||
|
||||
await interf2.ConnectAsync();
|
||||
|
||||
@@ -77,7 +78,7 @@ public class ExampleScenarios {
|
||||
public async Task RunReadTest () {
|
||||
|
||||
//setting up a new PLC interface and register collection
|
||||
MewtocolInterface interf = new MewtocolInterface("192.168.115.210").WithPoller();
|
||||
var interf = Mewtocol.Ethernet("192.168.115.210").WithPoller();
|
||||
|
||||
//auto add all built registers to the interface
|
||||
var builder = RegBuilder.ForInterface(interf);
|
||||
@@ -147,20 +148,18 @@ public class ExampleScenarios {
|
||||
|
||||
}
|
||||
|
||||
[Scenario("Test read speed 100 R registers")]
|
||||
public async Task ReadRSpeedTest() {
|
||||
[Scenario("Test read speed TCP (n) R registers")]
|
||||
public async Task ReadRSpeedTest (string registerCount) {
|
||||
|
||||
var preLogLevel = Logger.LogLevel;
|
||||
Logger.LogLevel = LogLevel.Critical;
|
||||
|
||||
//setting up a new PLC interface and register collection
|
||||
MewtocolInterface interf = new MewtocolInterface("192.168.115.210") {
|
||||
ConnectTimeout = 3000,
|
||||
};
|
||||
using var interf = Mewtocol.Ethernet("192.168.115.210");
|
||||
|
||||
//auto add all built registers to the interface
|
||||
var builder = RegBuilder.ForInterface(interf);
|
||||
for (int i = 0; i < 100; i++) {
|
||||
for (int i = 0; i < int.Parse(registerCount); i++) {
|
||||
|
||||
builder.FromPlcRegName($"R{i}A").Build();
|
||||
|
||||
@@ -169,6 +168,11 @@ public class ExampleScenarios {
|
||||
//connect
|
||||
await interf.ConnectAsync();
|
||||
|
||||
if(!interf.IsConnected) {
|
||||
Console.WriteLine("Aborted, connection failed");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Poller cycle started");
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
@@ -180,12 +184,75 @@ public class ExampleScenarios {
|
||||
|
||||
Console.WriteLine($"Single frame excec time: {sw.ElapsedMilliseconds:N0}ms for {cmdCount} commands and {interf.Registers.Count()} R registers");
|
||||
|
||||
interf.Disconnect();
|
||||
|
||||
await Task.Delay(1000);
|
||||
|
||||
}
|
||||
|
||||
[Scenario("Test read speed Serial (n) R registers")]
|
||||
public async Task ReadRSpeedTestSerial (string registerCount) {
|
||||
|
||||
var preLogLevel = Logger.LogLevel;
|
||||
Logger.LogLevel = LogLevel.Critical;
|
||||
|
||||
//setting up a new PLC interface and register collection
|
||||
//MewtocolInterfaceShared interf = Mewtocol.SerialAuto("COM4");
|
||||
using var interf = Mewtocol.Serial("COM4", BaudRate._115200, DataBits.Eight, Parity.Odd, StopBits.One);
|
||||
|
||||
//auto add all built registers to the interface
|
||||
var builder = RegBuilder.ForInterface(interf);
|
||||
for (int i = 0; i < int.Parse(registerCount); i++) {
|
||||
|
||||
builder.FromPlcRegName($"R{i}A").Build();
|
||||
|
||||
}
|
||||
|
||||
//connect
|
||||
await interf.ConnectAsync();
|
||||
|
||||
if (!interf.IsConnected) {
|
||||
Console.WriteLine("Aborted, connection failed");
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Poller cycle started");
|
||||
var sw = Stopwatch.StartNew();
|
||||
|
||||
int cmdCount = await interf.RunPollerCylceManual();
|
||||
|
||||
sw.Stop();
|
||||
|
||||
Console.WriteLine("Poller cycle finished");
|
||||
|
||||
Console.WriteLine($"Single frame excec time: {sw.ElapsedMilliseconds:N0}ms for {cmdCount} commands and {interf.Registers.Count()} R registers");
|
||||
|
||||
}
|
||||
|
||||
[Scenario("Test automatic serial port setup")]
|
||||
public async Task TestAutoSerialSetup () {
|
||||
|
||||
var preLogLevel = Logger.LogLevel;
|
||||
Logger.LogLevel = LogLevel.Critical;
|
||||
|
||||
//setting up a new PLC interface and register collection
|
||||
var interf = Mewtocol.SerialAuto("COM4");
|
||||
|
||||
//connect
|
||||
await interf.ConnectAsync();
|
||||
|
||||
if (!interf.IsConnected) {
|
||||
|
||||
Console.WriteLine("Aborted, connection failed");
|
||||
return;
|
||||
|
||||
} else {
|
||||
|
||||
Console.WriteLine("Serial port settings found");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
[Scenario("Find all COM5 cassettes in the network")]
|
||||
public async Task FindCassettes () {
|
||||
|
||||
@@ -209,10 +276,10 @@ public class ExampleScenarios {
|
||||
|
||||
}
|
||||
|
||||
await Task.Delay(5000);
|
||||
|
||||
var found = casettes.FirstOrDefault(x => x.Endpoint.Address.ToString() == "10.237.191.75");
|
||||
|
||||
if (found == null) return;
|
||||
|
||||
found.IPAddress = IPAddress.Parse($"192.168.1.{new Random().Next(20, 120)}");
|
||||
found.Name = $"Rand{new Random().Next(5, 15)}";
|
||||
|
||||
|
||||
@@ -54,7 +54,8 @@ class Program {
|
||||
|
||||
if(foundAtt != null && foundAtt is ScenarioAttribute att) {
|
||||
|
||||
Console.WriteLine($"[{j + 1}] {method.Name}() - {att.Description}");
|
||||
string paramsStr = string.Join(" ", method.GetParameters().Select(x => x.Name));
|
||||
Console.WriteLine($"[{j + 1}] {method.Name}({paramsStr}) - {att.Description}");
|
||||
invokeableMethods.Add(method);
|
||||
|
||||
j++;
|
||||
@@ -78,6 +79,8 @@ class Program {
|
||||
var line = Console.ReadLine();
|
||||
|
||||
var loggerMatch = Regex.Match(line, @"logger (?<level>[a-zA-Z]{0,})");
|
||||
var splitInput = Regex.Split(line, " ");
|
||||
|
||||
|
||||
if (loggerMatch.Success && Enum.TryParse<LogLevel>(loggerMatch.Groups["level"].Value, out var loglevel)) {
|
||||
|
||||
@@ -93,13 +96,26 @@ class Program {
|
||||
|
||||
Console.Clear();
|
||||
|
||||
} else if (int.TryParse(line, out var lineNum)) {
|
||||
} else if (int.TryParse(splitInput[0], out var lineNum)) {
|
||||
|
||||
var index = Math.Clamp(lineNum - 1, 0, invokeableMethods.Count - 1);
|
||||
|
||||
var task = (Task)invokeableMethods.ElementAt(index).Invoke(ExampleSzenarios, null);
|
||||
object[] invParams = null;
|
||||
|
||||
task.Wait();
|
||||
if(splitInput.Length > 1) {
|
||||
invParams = splitInput.Skip(1).Cast<object>().ToArray();
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
var task = (Task)invokeableMethods.ElementAt(index).Invoke(ExampleSzenarios, invParams);
|
||||
task.Wait();
|
||||
|
||||
} catch (TargetParameterCountException) {
|
||||
|
||||
Console.WriteLine("Missing parameters");
|
||||
|
||||
}
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine("The program ran to completition");
|
||||
|
||||
Reference in New Issue
Block a user