mirror of
https://github.com/OpenLogics/MewtocolNet.git
synced 2025-12-06 03:01:24 +00:00
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:
@@ -198,6 +198,17 @@ namespace MewtocolNet {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
IEnumerable<IRegister> GetAllRegisters();
|
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>
|
/// <summary>
|
||||||
/// Explains the register internal layout at this moment in time
|
/// Explains the register internal layout at this moment in time
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ namespace MewtocolNet.Logging {
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defines the default output logger targets
|
/// Defines the default output logger targets
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.None;
|
public static LoggerTargets DefaultTargets { get; set; } = LoggerTargets.Console;
|
||||||
|
|
||||||
internal static Action<DateTime, LogLevel, string> LogInvoked;
|
internal static Action<DateTime, LogLevel, string> LogInvoked;
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ namespace MewtocolNet.Logging {
|
|||||||
|
|
||||||
OnNewLogMessage((d, l, m) => {
|
OnNewLogMessage((d, l, m) => {
|
||||||
|
|
||||||
if(isConsoleApplication || DefaultTargets.HasFlag(LoggerTargets.Console)) {
|
if(isConsoleApplication && DefaultTargets.HasFlag(LoggerTargets.Console)) {
|
||||||
|
|
||||||
switch (l) {
|
switch (l) {
|
||||||
case LogLevel.Error:
|
case LogLevel.Error:
|
||||||
|
|||||||
@@ -465,7 +465,7 @@ namespace MewtocolNet {
|
|||||||
#region Message sending and queuing
|
#region Message sending and queuing
|
||||||
|
|
||||||
//internally used send task
|
//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;
|
if (tSourceMessageCancel.Token.IsCancellationRequested) return MewtocolFrameResponse.Canceled;
|
||||||
|
|
||||||
@@ -484,7 +484,7 @@ namespace MewtocolNet {
|
|||||||
//send request
|
//send request
|
||||||
regularSendTask = SendTwoDirectionalFrameAsync(_msg, onReceiveProgress);
|
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) {
|
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) {
|
private protected async Task<MewtocolFrameResponse> SendTwoDirectionalFrameAsync(string frame, Action<double> onReceiveProgress = null) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -899,8 +862,6 @@ namespace MewtocolNet {
|
|||||||
ClearRegisterVals();
|
ClearRegisterVals();
|
||||||
KillPoller();
|
KillPoller();
|
||||||
|
|
||||||
//GetAllRegisters().Cast<Register>().ToList().ForEach(x => x.OnPlcDisconnected());
|
|
||||||
|
|
||||||
//generate a new cancellation token source
|
//generate a new cancellation token source
|
||||||
tSourceMessageCancel = new CancellationTokenSource();
|
tSourceMessageCancel = new CancellationTokenSource();
|
||||||
|
|
||||||
|
|||||||
@@ -327,66 +327,30 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
#region Register Adding
|
#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) {
|
internal void AddRegisters(params Register[] registers) {
|
||||||
|
|
||||||
memoryManager.LinkAndMergeRegisters(registers.ToList());
|
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
|
#endregion
|
||||||
|
|
||||||
#region Register accessing
|
#region Register accessing
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ namespace MewtocolNet {
|
|||||||
/// <returns>A PLCInfo class</returns>
|
/// <returns>A PLCInfo class</returns>
|
||||||
public async Task<PLCInfo> GetInfoAsync(bool detailed = true) {
|
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;
|
if (!resRT.Success || tSourceMessageCancel.Token.IsCancellationRequested) return null;
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
if (isConnectingStage && detailed) {
|
if (isConnectingStage && detailed) {
|
||||||
|
|
||||||
resEXRT = await SendCommandInternalAsync("%EE#EX00RT00");
|
resEXRT = await SendCommandInternalAsync($"%{GetStationNumber()}#EX00RT00");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ namespace MewtocolNet {
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override async Task<ConnectResult> ConnectAsync(Func<Task> callBack = null) => await ConnectAsyncPriv(callBack);
|
public override async Task<ConnectResult> ConnectAsync(Func<Task> callBack = null) => await ConnectAsyncPriv(callBack);
|
||||||
|
|
||||||
private void BuildTcpClient () {
|
protected internal void BuildTcpClient () {
|
||||||
|
|
||||||
if (HostEndpoint != null) {
|
if (HostEndpoint != null) {
|
||||||
|
|
||||||
@@ -87,9 +87,6 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
client = new TcpClient(HostEndpoint) {
|
client = new TcpClient(HostEndpoint) {
|
||||||
ReceiveBufferSize = RecBufferSize,
|
ReceiveBufferSize = RecBufferSize,
|
||||||
NoDelay = false,
|
|
||||||
ReceiveTimeout = sendReceiveTimeoutMs,
|
|
||||||
SendTimeout = sendReceiveTimeoutMs,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
var ep = (IPEndPoint)client.Client.LocalEndPoint;
|
||||||
@@ -99,9 +96,6 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
client = new TcpClient() {
|
client = new TcpClient() {
|
||||||
ReceiveBufferSize = RecBufferSize,
|
ReceiveBufferSize = RecBufferSize,
|
||||||
NoDelay = false,
|
|
||||||
ReceiveTimeout = sendReceiveTimeoutMs,
|
|
||||||
SendTimeout = sendReceiveTimeoutMs,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,8 +161,10 @@ namespace MewtocolNet {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cpuVerStr = match.Groups["ver"].Value;
|
||||||
|
|
||||||
//overwrite the other vals that are also contained in EXRT
|
//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);
|
this.HardwareInformation = (HWInformation)byte.Parse(match.Groups["hwif"].Value, NumberStyles.HexNumber);
|
||||||
|
|
||||||
return true;
|
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) {
|
inf = new PLCInfo (onInterface) {
|
||||||
TypeCode = typeCodeFull,
|
TypeCode = typeCodeFull,
|
||||||
CpuVersion = match.Groups["cpuver"].Value.Insert(1, "."),
|
CpuVersion = cpuVer,
|
||||||
ProgramCapacity = definedProgCapacity,
|
ProgramCapacity = definedProgCapacity,
|
||||||
SelfDiagnosticError = match.Groups["sdiag"].Value,
|
SelfDiagnosticError = match.Groups["sdiag"].Value,
|
||||||
OperationMode = (OPMode)byte.Parse(match.Groups["op"].Value, NumberStyles.HexNumber),
|
OperationMode = (OPMode)byte.Parse(match.Groups["op"].Value, NumberStyles.HexNumber),
|
||||||
|
|||||||
Reference in New Issue
Block a user