feat: Enhance Sharp7 support by mapping tags and determining data types for SDataValue

This commit is contained in:
Miguel 2025-09-04 14:16:20 +02:00
parent 9d6c554534
commit 8ec03c2434
1 changed files with 72 additions and 3 deletions

View File

@ -594,9 +594,78 @@ namespace LibS7Adv
} }
else if (_connection?.ConnectionType == ConnectionType.Sharp7) else if (_connection?.ConnectionType == ConnectionType.Sharp7)
{ {
// Sharp7: no soporta SDataValue directamente // Sharp7: mapear tag y determinar el tipo basado en la dirección
// El usuario debería usar funciones específicas como LeerBool, LeerNumber string address = MapTagToAddress(pTag);
PlcData.LastError = "LeerTag with SDataValue is not supported with Sharp7. Use specific type methods like LeerBool, LeerNumber.";
// Detectar el tipo de datos basado en la dirección para crear el SDataValue apropiado
var tagAddress = ParseTagAddress("%" + address); // Agregar % para el parser
if (tagAddress != null)
{
switch (tagAddress.tagType)
{
case EDataType.Bool:
var boolResult = LeerBool(address);
if (boolResult.HasValue)
{
return new SDataValue { Bool = boolResult.Value };
}
break;
case EDataType.Byte:
var byteResult = LeerNumber(address, false);
if (byte.TryParse(byteResult, out byte byteValue))
{
return new SDataValue { UInt8 = byteValue };
}
break;
case EDataType.Word:
var wordResult = LeerNumber(address, false);
if (ushort.TryParse(wordResult, out ushort wordValue))
{
return new SDataValue { UInt16 = wordValue };
}
break;
case EDataType.DWord:
var dwordResult = LeerNumber(address, false);
if (uint.TryParse(dwordResult, out uint dwordValue))
{
return new SDataValue { UInt32 = dwordValue };
}
break;
default:
// Si no se puede determinar el tipo, intentar como string/número
var defaultResult = LeerNumber(address, false);
if (int.TryParse(defaultResult, out int intValue))
{
return new SDataValue { Int32 = intValue };
}
break;
}
}
else
{
// Si no se puede parsear la dirección, intentar diferentes tipos
// Primero intentar como bool si la dirección tiene formato de bit
if (address.Contains("."))
{
var boolResult = LeerBool(address);
if (boolResult.HasValue)
{
return new SDataValue { Bool = boolResult.Value };
}
}
else
{
// Intentar como número
var numberResult = LeerNumber(address, false);
if (int.TryParse(numberResult, out int intValue))
{
return new SDataValue { Int32 = intValue };
}
}
}
PlcData.LastError = $"Could not determine data type for address: {address}";
return new SDataValue(); return new SDataValue();
} }
else else