diff --git a/PLCViewModel.cs b/PLCViewModel.cs index 1121943..21be546 100644 --- a/PLCViewModel.cs +++ b/PLCViewModel.cs @@ -594,9 +594,78 @@ namespace LibS7Adv } else if (_connection?.ConnectionType == ConnectionType.Sharp7) { - // Sharp7: no soporta SDataValue directamente - // El usuario debería usar funciones específicas como LeerBool, LeerNumber - PlcData.LastError = "LeerTag with SDataValue is not supported with Sharp7. Use specific type methods like LeerBool, LeerNumber."; + // Sharp7: mapear tag y determinar el tipo basado en la dirección + string address = MapTagToAddress(pTag); + + // 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(); } else