From 6dd2a1688a63c6d7e6bf0f65e3972c2b601ad1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Wei=C3=9F?= <72068105+Sandoun@users.noreply.github.com> Date: Fri, 25 Aug 2023 16:35:52 +0200 Subject: [PATCH] Fix cpu version display - fix logger always printing to console if disabled - add register adding at connected time and clearing --- MewtocolNet/IPlc.cs | 11 +++ MewtocolNet/Logging/Logger.cs | 4 +- MewtocolNet/MewtocolInterface.cs | 43 +---------- .../MewtocolInterfaceRegisterHandling.cs | 72 +++++-------------- MewtocolNet/MewtocolInterfaceRequests.cs | 4 +- MewtocolNet/MewtocolInterfaceTcp.cs | 8 +-- MewtocolNet/PLCInfo.cs | 11 ++- 7 files changed, 44 insertions(+), 109 deletions(-) diff --git a/MewtocolNet/IPlc.cs b/MewtocolNet/IPlc.cs index 1bf2bb0..aa459cc 100644 --- a/MewtocolNet/IPlc.cs +++ b/MewtocolNet/IPlc.cs @@ -198,6 +198,17 @@ namespace MewtocolNet { /// IEnumerable GetAllRegisters(); + /// + /// Builds and adds registers to the device + /// + /// + void BuildRegisters(Action builder); + + /// + /// Clears all registers atached to the interface + /// + void ClearAllRegisters(); + /// /// Explains the register internal layout at this moment in time /// diff --git a/MewtocolNet/Logging/Logger.cs b/MewtocolNet/Logging/Logger.cs index 12226fd..63b99e8 100644 --- a/MewtocolNet/Logging/Logger.cs +++ b/MewtocolNet/Logging/Logger.cs @@ -16,7 +16,7 @@ namespace MewtocolNet.Logging { /// /// Defines the default output logger targets /// - public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.None; + public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.Console; internal static Action LogInvoked; @@ -26,7 +26,7 @@ namespace MewtocolNet.Logging { OnNewLogMessage((d, l, m) => { - if(isConsoleApplication || DefaultTargets.HasFlag(LoggerTargets.Console)) { + if(isConsoleApplication && DefaultTargets.HasFlag(LoggerTargets.Console)) { switch (l) { case LogLevel.Error: diff --git a/MewtocolNet/MewtocolInterface.cs b/MewtocolNet/MewtocolInterface.cs index a9eb18a..62c5620 100644 --- a/MewtocolNet/MewtocolInterface.cs +++ b/MewtocolNet/MewtocolInterface.cs @@ -465,7 +465,7 @@ namespace MewtocolNet { #region Message sending and queuing //internally used send task - internal async Task SendCommandInternalAsync(string _msg, Action onReceiveProgress = null) { + internal async Task SendCommandInternalAsync(string _msg, Action onReceiveProgress = null, int? overrideTimeout = null) { if (tSourceMessageCancel.Token.IsCancellationRequested) return MewtocolFrameResponse.Canceled; @@ -484,7 +484,7 @@ namespace MewtocolNet { //send request regularSendTask = SendTwoDirectionalFrameAsync(_msg, onReceiveProgress); - var timeoutAwaiter = await Task.WhenAny(regularSendTask, Task.Delay(sendReceiveTimeoutMs, tSourceMessageCancel.Token)); + var timeoutAwaiter = await Task.WhenAny(regularSendTask, Task.Delay(overrideTimeout ?? sendReceiveTimeoutMs, tSourceMessageCancel.Token)); if (timeoutAwaiter != regularSendTask) { @@ -530,43 +530,6 @@ namespace MewtocolNet { } - private protected async Task SendOneDirectionalFrameAsync (string frame) { - - try { - - if (stream == null) return MewtocolFrameResponse.NotIntialized; - - frame = $"{frame.BCC_Mew()}\r"; - - SetUpstreamStopWatchStart(); - - IsSending = true; - - if (tSourceMessageCancel.Token.IsCancellationRequested) return MewtocolFrameResponse.Canceled; - - //write inital command - byte[] writeBuffer = Encoding.UTF8.GetBytes(frame); - await stream.WriteAsync(writeBuffer, 0, writeBuffer.Length, tSourceMessageCancel.Token); - - IsSending = false; - - //calc upstream speed - CalcUpstreamSpeed(writeBuffer.Length); - - OnOutMsg(frame); - OnEndMsg(); - - } catch (Exception ex) { - - IsSending = false; - return new MewtocolFrameResponse(400, ex.Message.ToString()); - - } - - return MewtocolFrameResponse.EmptySuccess; - - } - private protected async Task SendTwoDirectionalFrameAsync(string frame, Action onReceiveProgress = null) { try { @@ -899,8 +862,6 @@ namespace MewtocolNet { ClearRegisterVals(); KillPoller(); - //GetAllRegisters().Cast().ToList().ForEach(x => x.OnPlcDisconnected()); - //generate a new cancellation token source tSourceMessageCancel = new CancellationTokenSource(); diff --git a/MewtocolNet/MewtocolInterfaceRegisterHandling.cs b/MewtocolNet/MewtocolInterfaceRegisterHandling.cs index 8b8a5dc..10fb572 100644 --- a/MewtocolNet/MewtocolInterfaceRegisterHandling.cs +++ b/MewtocolNet/MewtocolInterfaceRegisterHandling.cs @@ -327,66 +327,30 @@ namespace MewtocolNet { #region Register Adding + /// > + public void BuildRegisters (Action builder) { + + var regBuilder = new RBuildMulti(this); + + builder.Invoke(regBuilder); + + this.AddRegisters(regBuilder.assembler.assembled.ToArray()); + + } + + /// > + public void ClearAllRegisters () { + + memoryManager.ClearAllRegisters(); + + } + internal void AddRegisters(params Register[] registers) { memoryManager.LinkAndMergeRegisters(registers.ToList()); } - private bool CheckDuplicateRegister(Register instance, out Register foundDupe) { - - foundDupe = RegistersInternal.FirstOrDefault(x => x.CompareIsDuplicate(instance)); - - return RegistersInternal.Contains(instance) || foundDupe != null; - - } - - private bool CheckDuplicateRegister(Register instance) { - - var foundDupe = RegistersInternal.FirstOrDefault(x => x.CompareIsDuplicate(instance)); - - return RegistersInternal.Contains(instance) || foundDupe != null; - - } - - private bool CheckDuplicateNameRegister(Register instance) { - - return RegistersInternal.Any(x => x.CompareIsNameDuplicate(instance)); - - } - - private bool CheckOverlappingRegister(Register instance, out Register regB) { - - //ignore bool registers, they have their own address spectrum - regB = null; - if (instance is BoolRegister) return false; - - uint addressFrom = instance.MemoryAddress; - uint addressTo = addressFrom + instance.GetRegisterAddressLen(); - - var foundOverlapping = RegistersInternal.FirstOrDefault(x => { - - //ignore bool registers, they have their own address spectrum - if (x is BoolRegister) return false; - - uint addressF = x.MemoryAddress; - uint addressT = addressF + x.GetRegisterAddressLen(); - - bool matchingBaseAddress = addressFrom < addressT && addressF < addressTo; - - return matchingBaseAddress; - - }); - - if (foundOverlapping != null) { - regB = foundOverlapping; - return true; - } - - return false; - - } - #endregion #region Register accessing diff --git a/MewtocolNet/MewtocolInterfaceRequests.cs b/MewtocolNet/MewtocolInterfaceRequests.cs index a53d4ce..d8d478f 100644 --- a/MewtocolNet/MewtocolInterfaceRequests.cs +++ b/MewtocolNet/MewtocolInterfaceRequests.cs @@ -31,7 +31,7 @@ namespace MewtocolNet { /// A PLCInfo class public async Task GetInfoAsync(bool detailed = true) { - MewtocolFrameResponse resRT = await SendCommandInternalAsync("%EE#RT"); + MewtocolFrameResponse resRT = await SendCommandInternalAsync($"%{GetStationNumber()}#RT"); if (!resRT.Success || tSourceMessageCancel.Token.IsCancellationRequested) return null; @@ -39,7 +39,7 @@ namespace MewtocolNet { if (isConnectingStage && detailed) { - resEXRT = await SendCommandInternalAsync("%EE#EX00RT00"); + resEXRT = await SendCommandInternalAsync($"%{GetStationNumber()}#EX00RT00"); } diff --git a/MewtocolNet/MewtocolInterfaceTcp.cs b/MewtocolNet/MewtocolInterfaceTcp.cs index 760d602..d0bfc31 100644 --- a/MewtocolNet/MewtocolInterfaceTcp.cs +++ b/MewtocolNet/MewtocolInterfaceTcp.cs @@ -72,7 +72,7 @@ namespace MewtocolNet { /// public override async Task ConnectAsync(Func callBack = null) => await ConnectAsyncPriv(callBack); - private void BuildTcpClient () { + protected internal void BuildTcpClient () { if (HostEndpoint != null) { @@ -87,9 +87,6 @@ namespace MewtocolNet { client = new TcpClient(HostEndpoint) { ReceiveBufferSize = RecBufferSize, - NoDelay = false, - ReceiveTimeout = sendReceiveTimeoutMs, - SendTimeout = sendReceiveTimeoutMs, }; var ep = (IPEndPoint)client.Client.LocalEndPoint; @@ -99,9 +96,6 @@ namespace MewtocolNet { client = new TcpClient() { ReceiveBufferSize = RecBufferSize, - NoDelay = false, - ReceiveTimeout = sendReceiveTimeoutMs, - SendTimeout = sendReceiveTimeoutMs, }; } diff --git a/MewtocolNet/PLCInfo.cs b/MewtocolNet/PLCInfo.cs index 6557592..7a7012d 100644 --- a/MewtocolNet/PLCInfo.cs +++ b/MewtocolNet/PLCInfo.cs @@ -160,9 +160,11 @@ namespace MewtocolNet { this.TypeCode = (PlcType)tempTypeCode; } - + + var cpuVerStr = match.Groups["ver"].Value; + //overwrite the other vals that are also contained in EXRT - this.CpuVersion = match.Groups["ver"].Value.Insert(1, "."); + this.CpuVersion = string.Join(".", cpuVerStr.Select(x => byte.Parse($"{x}", NumberStyles.HexNumber).ToString())); this.HardwareInformation = (HWInformation)byte.Parse(match.Groups["hwif"].Value, NumberStyles.HexNumber); return true; @@ -206,9 +208,12 @@ namespace MewtocolNet { } + var cpuVerStr = match.Groups["cpuver"].Value; + var cpuVer = string.Join(".", cpuVerStr.Select(x => byte.Parse($"{x}").ToString("X1"))); + inf = new PLCInfo (onInterface) { TypeCode = typeCodeFull, - CpuVersion = match.Groups["cpuver"].Value.Insert(1, "."), + CpuVersion = cpuVer, ProgramCapacity = definedProgCapacity, SelfDiagnosticError = match.Groups["sdiag"].Value, OperationMode = (OPMode)byte.Parse(match.Groups["op"].Value, NumberStyles.HexNumber),