Mejorado logica cuando el PLC esta siendo actualizado
This commit is contained in:
parent
430dbfbb76
commit
c0ff14a5ae
447
PLCViewModel.cs
447
PLCViewModel.cs
|
@ -52,6 +52,11 @@ namespace LibS7Adv
|
||||||
|
|
||||||
private bool IsConfigured { get; set; }
|
private bool IsConfigured { get; set; }
|
||||||
|
|
||||||
|
// Añade una bandera para controlar el estado de actualización
|
||||||
|
private bool _isUpdating = false;
|
||||||
|
private DateTime _lastUpdateStartTime = DateTime.MinValue;
|
||||||
|
private TimeSpan _updateTimeout = TimeSpan.FromSeconds(10); // Timeout para reintentar después de un tiempo
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
PlcData plcData = new PlcData();
|
PlcData plcData = new PlcData();
|
||||||
|
|
||||||
|
@ -123,20 +128,39 @@ namespace LibS7Adv
|
||||||
|
|
||||||
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
private void Instance_OnSoftwareConfigurationChanged(IInstance instance, SOnSoftwareConfigChangedParameter event_param)
|
||||||
{
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Actualizar cuando recibimos el evento
|
||||||
UpdateTagList();
|
UpdateTagList();
|
||||||
|
|
||||||
|
// Cuando la actualización se completa exitosamente, desactivar la bandera
|
||||||
|
if (IsConfigured)
|
||||||
|
{
|
||||||
|
_isUpdating = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Si no se pudo configurar, programar un reintento después de un tiempo
|
||||||
|
Task.Delay(1000).ContinueWith(_ => UpdateTagList());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateTagList()
|
public void UpdateTagList()
|
||||||
{
|
{
|
||||||
IsConfigured = false;
|
IsConfigured = false;
|
||||||
|
_isUpdating = true;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Instance?.UpdateTagList(ETagListDetails.IO | ETagListDetails.DB | ETagListDetails.M, true); // ETagListDetails.IO | ETagListDetails.DB
|
Instance?.UpdateTagList(ETagListDetails.IO | ETagListDetails.DB | ETagListDetails.M, true);
|
||||||
IsConfigured = true;
|
IsConfigured = true;
|
||||||
|
_isUpdating = false; // Éxito en la actualización
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
PlcData.LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
|
// Mantener _isUpdating = true ya que hubo un error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +173,22 @@ namespace LibS7Adv
|
||||||
PlcData.LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada,
|
||||||
|
// posiblemente se quedó trabado, intentamos de nuevo
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
{ return false; }
|
{ return false; }
|
||||||
var tag = ParseTagAddress(sTag);
|
var tag = ParseTagAddress(sTag);
|
||||||
|
@ -167,8 +207,22 @@ namespace LibS7Adv
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,6 +236,22 @@ namespace LibS7Adv
|
||||||
PlcData.LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada,
|
||||||
|
// posiblemente se quedó trabado, intentamos de nuevo
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
{ return false; }
|
{ return false; }
|
||||||
var tag = ParseTagAddress(sTag);
|
var tag = ParseTagAddress(sTag);
|
||||||
|
@ -201,13 +271,26 @@ namespace LibS7Adv
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public string? LeerNumber(string sTag, bool Signed = false)
|
public string? LeerNumber(string sTag, bool Signed = false)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -217,6 +300,21 @@ namespace LibS7Adv
|
||||||
PlcData.LastError = "Not Connected";
|
PlcData.LastError = "Not Connected";
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
if (sTag == null)
|
if (sTag == null)
|
||||||
{ return ""; }
|
{ return ""; }
|
||||||
|
|
||||||
|
@ -273,8 +371,22 @@ namespace LibS7Adv
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = sTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = sTag + ":" + ex.Message;
|
PlcData.LastError = sTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,11 +397,39 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return Instance?.OutputArea.ReadBit(pByte, (byte)pBit) ?? false;
|
return Instance?.OutputArea.ReadBit(pByte, (byte)pBit) ?? false;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = "Byte " + pByte + ", Bit " + pBit + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -298,69 +438,270 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Instance?.InputArea.WriteBit(pByte, (byte)pBit, pValue);
|
Instance?.InputArea.WriteBit(pByte, (byte)pBit, pValue);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = "Byte " + pByte + ", Bit " + pBit + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = ex.Message;
|
PlcData.LastError = ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void EscribirTag(string pTag, SDataValue Value)
|
public void EscribirTag(string pTag, SDataValue Value)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Instance?.Write(pTag, Value);
|
Instance?.Write(pTag, Value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public SDataValue LeerTag(string pTag)
|
public SDataValue LeerTag(string pTag)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return new SDataValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return new SDataValue();
|
||||||
|
}
|
||||||
|
|
||||||
return Instance.Read(pTag);
|
return Instance.Read(pTag);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return new SDataValue();
|
return new SDataValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void EscribirTagBool(string pTag, bool pValue)
|
public void EscribirTagBool(string pTag, bool pValue)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Instance?.WriteBool(pTag, pValue);
|
Instance?.WriteBool(pTag, pValue);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public void EscribirTagInt16(string pTag, int pValue)
|
public void EscribirTagInt16(string pTag, int pValue)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Instance?.WriteInt16(pTag, (short)pValue);
|
Instance?.WriteInt16(pTag, (short)pValue);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool LeerTagBool(string pTag)
|
public bool LeerTagBool(string pTag)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool result = Instance?.ReadBool(pTag) ?? false;
|
bool result = Instance?.ReadBool(pTag) ?? false;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -369,12 +710,46 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int? result = Instance?.ReadInt16(pTag);
|
int? result = Instance?.ReadInt16(pTag);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,12 +758,46 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar leer durante la actualización
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int? result = Instance?.ReadInt32(pTag);
|
int? result = Instance?.ReadInt32(pTag);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -397,13 +806,47 @@ namespace LibS7Adv
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (!isConnected)
|
||||||
|
{
|
||||||
|
PlcData.LastError = "Not Connected";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar si estamos en proceso de actualización
|
||||||
|
if (_isUpdating)
|
||||||
|
{
|
||||||
|
// Si ha pasado mucho tiempo desde la última actualización intentada
|
||||||
|
if (DateTime.Now - _lastUpdateStartTime > _updateTimeout)
|
||||||
|
{
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
UpdateTagList();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No intentar escribir durante la actualización
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Instance?.WriteInt32(pTag, pValue);
|
Instance?.WriteInt32(pTag, pValue);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Verificar si el error es NotUpToDate y activar la bandera si es así
|
||||||
|
if (ex.Message.Contains("NotUpToDate") || ex is SimulationRuntimeException sre && sre.RuntimeErrorCode == ERuntimeErrorCode.NotUpToDate)
|
||||||
|
{
|
||||||
|
_isUpdating = true;
|
||||||
|
_lastUpdateStartTime = DateTime.Now;
|
||||||
|
|
||||||
|
// Iniciar actualización de tags
|
||||||
|
Task.Run(() => UpdateTagList());
|
||||||
|
|
||||||
|
PlcData.LastError = pTag + ": Tags no actualizados, reintentando...";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
PlcData.LastError = pTag + ":" + ex.Message;
|
PlcData.LastError = pTag + ":" + ex.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static TagAddress ParseTagAddress(string tag)
|
public static TagAddress ParseTagAddress(string tag)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue