Fix cpu version display

- fix logger always printing to console if disabled
- add register adding at connected time and clearing
This commit is contained in:
Felix Weiß
2023-08-25 16:35:52 +02:00
parent 8bd1ce6b4d
commit 6dd2a1688a
7 changed files with 44 additions and 109 deletions

View File

@@ -198,6 +198,17 @@ namespace MewtocolNet {
/// </summary>
IEnumerable<IRegister> GetAllRegisters();
/// <summary>
/// Builds and adds registers to the device
/// </summary>
/// <param name="builder"></param>
void BuildRegisters(Action<RBuildMulti> builder);
/// <summary>
/// Clears all registers atached to the interface
/// </summary>
void ClearAllRegisters();
/// <summary>
/// Explains the register internal layout at this moment in time
/// </summary>

View File

@@ -16,7 +16,7 @@ namespace MewtocolNet.Logging {
/// <summary>
/// Defines the default output logger targets
/// </summary>
public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.None;
public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.Console;
internal static Action<DateTime, LogLevel, string> 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:

View File

@@ -465,7 +465,7 @@ namespace MewtocolNet {
#region Message sending and queuing
//internally used send task
internal async Task<MewtocolFrameResponse> SendCommandInternalAsync(string _msg, Action<double> onReceiveProgress = null) {
internal async Task<MewtocolFrameResponse> SendCommandInternalAsync(string _msg, Action<double> 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<MewtocolFrameResponse> 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<MewtocolFrameResponse> SendTwoDirectionalFrameAsync(string frame, Action<double> onReceiveProgress = null) {
try {
@@ -899,8 +862,6 @@ namespace MewtocolNet {
ClearRegisterVals();
KillPoller();
//GetAllRegisters().Cast<Register>().ToList().ForEach(x => x.OnPlcDisconnected());
//generate a new cancellation token source
tSourceMessageCancel = new CancellationTokenSource();

View File

@@ -327,66 +327,30 @@ namespace MewtocolNet {
#region Register Adding
/// <inheritdoc/>>
public void BuildRegisters (Action<RBuildMulti> builder) {
var regBuilder = new RBuildMulti(this);
builder.Invoke(regBuilder);
this.AddRegisters(regBuilder.assembler.assembled.ToArray());
}
/// <inheritdoc/>>
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

View File

@@ -31,7 +31,7 @@ namespace MewtocolNet {
/// <returns>A PLCInfo class</returns>
public async Task<PLCInfo> 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");
}

View File

@@ -72,7 +72,7 @@ namespace MewtocolNet {
/// <inheritdoc/>
public override async Task<ConnectResult> ConnectAsync(Func<Task> 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,
};
}

View File

@@ -161,8 +161,10 @@ namespace MewtocolNet {
}
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),