diff --git a/Examples/Examples.BasicEthernet/Program.cs b/Examples/Examples.BasicEthernet/Program.cs index 810981b..8069635 100644 --- a/Examples/Examples.BasicEthernet/Program.cs +++ b/Examples/Examples.BasicEthernet/Program.cs @@ -14,6 +14,7 @@ internal class Program { //the library provides a logging tool, comment this out if needed Logger.LogLevel = LogLevel.Critical; + Logger.OnNewLogMessage((t, l, m) => { Console.WriteLine(m); }); //create a new interface to the plc using ethernet / tcp ip //the using keyword is optional, if you want to use your PLC instance diff --git a/MewtocolNet/IPlcSerial.cs b/MewtocolNet/IPlcSerial.cs index a5d18d8..c975aa0 100644 --- a/MewtocolNet/IPlcSerial.cs +++ b/MewtocolNet/IPlcSerial.cs @@ -34,6 +34,11 @@ namespace MewtocolNet { /// StopBits SerialStopBits { get; } + /// + /// Is RTS (Request to send) enabled? + /// + bool RtsEnabled { get; } + /// /// Sets up the connection settings for the device /// @@ -42,8 +47,9 @@ namespace MewtocolNet { /// The serial connection data bits /// The serial connection parity /// The serial connection stop bits + /// Is RTS (Request to send) enabled? /// The station number of the PLC - void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, int _station = 1); + void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, bool _rtsEnable = true, int _station = 1); /// /// Tries to establish a connection with the device asynchronously diff --git a/MewtocolNet/Mewtocol.cs b/MewtocolNet/Mewtocol.cs index feaa1a0..aa00e82 100644 --- a/MewtocolNet/Mewtocol.cs +++ b/MewtocolNet/Mewtocol.cs @@ -128,12 +128,13 @@ namespace MewtocolNet { /// DataBits of the plc toolport /// Parity rate of the plc toolport /// Stop bits of the plc toolport + /// Is RTS (Request to send) enabled? /// Plc station number 0xEE for direct communication /// - public static PostInit Serial(string portName, BaudRate baudRate = BaudRate._19200, DataBits dataBits = DataBits.Eight, Parity parity = Parity.Odd, StopBits stopBits = StopBits.One, int station = 0xEE) { + public static PostInit Serial(string portName, BaudRate baudRate = BaudRate._19200, DataBits dataBits = DataBits.Eight, Parity parity = Parity.Odd, StopBits stopBits = StopBits.One, bool rtsEnabled = true, int station = 0xEE) { var instance = new MewtocolInterfaceSerial(); - instance.ConfigureConnection(portName, (int)baudRate, (int)dataBits, parity, stopBits, station); + instance.ConfigureConnection(portName, (int)baudRate, (int)dataBits, parity, stopBits, rtsEnabled, station); return new PostInit { intf = instance }; @@ -144,12 +145,13 @@ namespace MewtocolNet { /// Builds a serial mewtocol interface that finds the correct settings for the given port name automatically /// /// + /// Is RTS (Request to send) enabled? /// Plc station number 0xEE for direct communication /// - public static PostInit SerialAuto(string portName, int station = 0xEE) { + public static PostInit SerialAuto(string portName, bool rtsEnabled = true, int station = 0xEE) { var instance = new MewtocolInterfaceSerial(); - instance.ConfigureConnection(portName, station); + instance.ConfigureConnection(portName, station, rtsEnable: rtsEnabled); instance.ConfigureConnectionAuto(); return new PostInit { intf = instance diff --git a/MewtocolNet/MewtocolInterface.cs b/MewtocolNet/MewtocolInterface.cs index 62c5620..38e6fb0 100644 --- a/MewtocolNet/MewtocolInterface.cs +++ b/MewtocolNet/MewtocolInterface.cs @@ -106,7 +106,7 @@ namespace MewtocolNet { internal int tryReconnectDelayMs = 1000; internal bool usePoller = false; - internal bool alwaysGetMetadata = true; + internal bool alwaysGetMetadata = false; internal Func onBeforeReconnectTryTask; diff --git a/MewtocolNet/MewtocolInterfaceSerial.cs b/MewtocolNet/MewtocolInterfaceSerial.cs index 3f547e8..99d6546 100644 --- a/MewtocolNet/MewtocolInterfaceSerial.cs +++ b/MewtocolNet/MewtocolInterfaceSerial.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; namespace MewtocolNet { + /// public sealed class MewtocolInterfaceSerial : MewtocolInterface, IPlcSerial { private bool autoSerial; @@ -32,6 +33,9 @@ namespace MewtocolNet { /// public StopBits SerialStopBits { get; private set; } + /// + public bool RtsEnabled { get; private set; } + //Serial internal SerialPort serialClient; @@ -76,7 +80,7 @@ namespace MewtocolNet { } /// - public void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, int _station = 0xEE) { + public void ConfigureConnection(string _portName, int _baudRate = 19200, int _dataBits = 8, Parity _parity = Parity.Odd, StopBits _stopBits = StopBits.One, bool rtsEnable = true, int _station = 0xEE) { if (IsConnected) throw new NotSupportedException("Can't change the connection settings while the PLC is connected"); @@ -87,6 +91,7 @@ namespace MewtocolNet { SerialParity = _parity; SerialStopBits = _stopBits; stationNumber = _station; + RtsEnabled = rtsEnable; if (stationNumber != 0xEE && stationNumber > 99) throw new NotSupportedException("Station number can't be greater than 99"); @@ -231,7 +236,8 @@ namespace MewtocolNet { Parity = par, StopBits = sbits, ReadTimeout = 100, - Handshake = Handshake.None + Handshake = Handshake.None, + RtsEnable = RtsEnabled, }; PortName = port; diff --git a/MewtocolNet/PLCInfo.cs b/MewtocolNet/PLCInfo.cs index 7a7012d..e9aa7ee 100644 --- a/MewtocolNet/PLCInfo.cs +++ b/MewtocolNet/PLCInfo.cs @@ -134,7 +134,7 @@ namespace MewtocolNet { internal bool TryExtendFromEXRT(string msg) { - var regexEXRT = new Regex(@"\%EE\$EX00RT00(?..)(?..)..(?..)(?..)..(?..)(?....)(?..)(?..)(?.)(?...)(?...).*", RegexOptions.IgnoreCase); + var regexEXRT = new Regex(@"\%..\$EX00RT00(?..)(?..)..(?..)(?..)..(?..)(?....)(?..)(?..)(?.)(?...)(?...).*", RegexOptions.IgnoreCase); var match = regexEXRT.Match(msg); if (match.Success) { @@ -177,7 +177,7 @@ namespace MewtocolNet { internal static bool TryFromRT(string msg, MewtocolInterface onInterface, out PLCInfo inf) { - var regexRT = new Regex(@"\%EE\$RT(?..)(?..)(?..)(?..)..(?..)(?....).*", RegexOptions.IgnoreCase); + var regexRT = new Regex(@"\%..\$RT(?..)(?..)(?..)(?..)..(?..)(?....).*", RegexOptions.IgnoreCase); var match = regexRT.Match(msg); if (match.Success) { diff --git a/MewtocolNet/RegisterBuilding/BuilderPatterns/RBuildAnon.cs b/MewtocolNet/RegisterBuilding/BuilderPatterns/RBuildAnon.cs index f2b1515..a0ea124 100644 --- a/MewtocolNet/RegisterBuilding/BuilderPatterns/RBuildAnon.cs +++ b/MewtocolNet/RegisterBuilding/BuilderPatterns/RBuildAnon.cs @@ -23,15 +23,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns { public async Task WriteAsync(T value) { - var reg = (IRegister)builder.Assemble(this); - await reg.WriteAsync(value); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + await ((IRegister)reg).WriteAsync(value); } public async Task ReadAsync() { - var reg = (IRegister)builder.Assemble(this); - return await reg.ReadAsync(); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + return await ((IRegister)reg).ReadAsync(); } @@ -41,15 +43,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns { public async Task WriteAsync(string value) { - var reg = (IStringRegister)builder.Assemble(this); - await reg.WriteAsync(value); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + await ((IStringRegister)reg).WriteAsync(value); } public async Task ReadAsync() { - var reg = (IStringRegister)builder.Assemble(this); - return await reg.ReadAsync(); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + return await ((IStringRegister)reg).ReadAsync(); } @@ -71,15 +75,19 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns { public async Task WriteAsync(T[] value) { - var reg = (IArrayRegister)builder.Assemble(this); - await reg.WriteAsync(value); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + var tReg = (IArrayRegister)reg; + await tReg.WriteAsync(value); } public async Task ReadAsync() { - var reg = (IArrayRegister)builder.Assemble(this); - return await reg.ReadAsync(); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + var tReg = (IArrayRegister)reg; + return await tReg.ReadAsync(); } @@ -91,15 +99,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns { public async Task WriteAsync(T[,] value) { - var reg = (IArrayRegister2D)builder.Assemble(this); - await reg.WriteAsync(value); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + await ((IArrayRegister2D)reg).WriteAsync(value); } public async Task ReadAsync() { - var reg = (IArrayRegister2D)builder.Assemble(this); - return await reg.ReadAsync(); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + return await ((IArrayRegister2D)reg).ReadAsync(); } @@ -111,15 +121,17 @@ namespace MewtocolNet.RegisterBuilding.BuilderPatterns { public async Task WriteAsync(T[,,] value) { - var reg = (IArrayRegister3D)builder.Assemble(this); - await reg.WriteAsync(value); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + await ((IArrayRegister3D)reg).WriteAsync(value); } public async Task ReadAsync() { - var reg = (IArrayRegister3D)builder.Assemble(this); - return await reg.ReadAsync(); + var reg = builder.Assemble(this); + reg.isAnonymous = true; + return await ((IArrayRegister3D)reg).ReadAsync(); } diff --git a/MewtocolNet/SetupClasses/InterfaceSettings.cs b/MewtocolNet/SetupClasses/InterfaceSettings.cs index 874b26b..73956c7 100644 --- a/MewtocolNet/SetupClasses/InterfaceSettings.cs +++ b/MewtocolNet/SetupClasses/InterfaceSettings.cs @@ -59,7 +59,7 @@ namespace MewtocolNet.SetupClasses { /// /// Sets wether or not the interface should always retrieve metadata on connection start /// - public bool AlwaysGetMetadata { get; set; } = true; + public bool AlwaysGetMetadata { get; set; } = false; }