169 lines
7.5 KiB
C#
169 lines
7.5 KiB
C#
using LibS7Adv;
|
|
using Siemens.Simatic.Simulation.Runtime;
|
|
|
|
namespace ExampleUsage
|
|
{
|
|
/// <summary>
|
|
/// Ejemplo de uso del comportamiento diferencial de mapeo de tags
|
|
/// </summary>
|
|
public class TagMappingExample
|
|
{
|
|
public static void DemostrarComportamientoDiferencial()
|
|
{
|
|
// ========================================
|
|
// EJEMPLO CON ADVCO SIMULATOR
|
|
// ========================================
|
|
|
|
var plcAdvco = new PLCViewModel();
|
|
plcAdvco.PlcData.ConnectionType = ConnectionType.AdvCoSimulator;
|
|
plcAdvco.PlcData.Name = "PLC_1";
|
|
|
|
Console.WriteLine("=== AdvCoSimulator - Tags directos ===");
|
|
|
|
// Con AdvCoSimulator, los tags se usan directamente
|
|
string tagAdvco = "\"Data_block_1\".Tag1"; // Tag simbólico
|
|
bool? resultAdvco = plcAdvco.LeerBool(tagAdvco);
|
|
|
|
// También soporta direcciones absolutas
|
|
string addressAdvco = "DB1.DBX0.0";
|
|
bool? resultAdvco2 = plcAdvco.LeerBool(addressAdvco);
|
|
|
|
// SDataValue funciona completamente
|
|
SDataValue valueAdvco = new SDataValue();
|
|
valueAdvco.Bool = true;
|
|
plcAdvco.EscribirTag("\"Data_block_1\".Tag1", valueAdvco);
|
|
|
|
Console.WriteLine($"AdvCoSimulator Tag '{tagAdvco}': {resultAdvco}");
|
|
Console.WriteLine($"AdvCoSimulator Address '{addressAdvco}': {resultAdvco2}");
|
|
|
|
// ========================================
|
|
// EJEMPLO CON SHARP7
|
|
// ========================================
|
|
|
|
var plcSharp7 = new PLCViewModel();
|
|
plcSharp7.PlcData.ConnectionType = ConnectionType.Sharp7;
|
|
plcSharp7.PlcData.IP = "192.168.1.100";
|
|
|
|
Console.WriteLine("\n=== Sharp7 - Mapeo automático de direcciones ===");
|
|
|
|
// Con Sharp7, las direcciones se normalizan automáticamente
|
|
string[] addressesSharp7 = {
|
|
"PEW0", // Alemán con P → será "IW0"
|
|
"EW0", // Alemán → será "IW0"
|
|
"PIW0", // Americano con P → será "IW0"
|
|
"IW0", // Ya normalizado
|
|
"PAW0", // Output alemán con P → será "QW0"
|
|
"AW0", // Output alemán → será "QW0"
|
|
"PE0.0", // Bit alemán con P → será "I0.0"
|
|
"E0.0" // Bit alemán → será "I0.0"
|
|
};
|
|
|
|
foreach (string address in addressesSharp7)
|
|
{
|
|
// Internamente se llama a MapTagToAddress(address)
|
|
bool? result = plcSharp7.LeerBool(address);
|
|
Console.WriteLine($"Sharp7 '{address}' → normalizado automáticamente: {result}");
|
|
}
|
|
|
|
// ========================================
|
|
// EJEMPLO DE SDATAVALUE CON SHARP7
|
|
// ========================================
|
|
|
|
Console.WriteLine("\n=== SDataValue con diferentes drivers ===");
|
|
|
|
// Preparar un SDataValue
|
|
SDataValue testValue = new SDataValue();
|
|
testValue.Bool = true;
|
|
|
|
try
|
|
{
|
|
// Con AdvCoSimulator: funciona directamente
|
|
plcAdvco.EscribirTag("DB1.DBX0.0", testValue);
|
|
Console.WriteLine("AdvCoSimulator: SDataValue escrito correctamente");
|
|
|
|
// Con Sharp7: detecta automáticamente el tipo y usa EscribirBool
|
|
plcSharp7.EscribirTag("I0.0", testValue); // Internamente llama EscribirBool("I0.0", true)
|
|
Console.WriteLine("Sharp7: SDataValue convertido automáticamente a EscribirBool");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|
}
|
|
|
|
// ========================================
|
|
// RECOMENDACIONES
|
|
// ========================================
|
|
|
|
Console.WriteLine("\n=== Recomendaciones de uso ===");
|
|
Console.WriteLine("• AdvCoSimulator: Usar tags simbólicos o direcciones absolutas directamente");
|
|
Console.WriteLine("• Sharp7: Cualquier notación funciona (PEW0, EW0, IW0, etc.)");
|
|
Console.WriteLine("• SDataValue: Preferir AdvCoSimulator, Sharp7 lo convierte automáticamente");
|
|
Console.WriteLine("• Migración: El código existente funciona sin cambios");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Demuestra la flexibilidad de notaciones con Sharp7
|
|
/// </summary>
|
|
public static void DemostrarNormalizacionSharp7()
|
|
{
|
|
var plc = new PLCViewModel();
|
|
plc.PlcData.ConnectionType = ConnectionType.Sharp7;
|
|
plc.PlcData.IP = "192.168.1.100";
|
|
|
|
Console.WriteLine("=== Flexibilidad de notaciones Sharp7 ===");
|
|
|
|
// Todas estas direcciones son equivalentes para IW0:
|
|
string[] equivalentAddresses = { "PEW0", "EW0", "PIW0", "IW0" };
|
|
|
|
Console.WriteLine("Direcciones equivalentes para Input Word 0:");
|
|
foreach (string addr in equivalentAddresses)
|
|
{
|
|
Console.WriteLine($" '{addr}' se normaliza automáticamente");
|
|
// Todas se convierten internamente a "IW0"
|
|
}
|
|
|
|
// Todas estas direcciones son equivalentes para QW0:
|
|
string[] outputEquivalents = { "PAW0", "AW0", "POW0", "QW0" };
|
|
|
|
Console.WriteLine("\nDirecciones equivalentes para Output Word 0:");
|
|
foreach (string addr in outputEquivalents)
|
|
{
|
|
Console.WriteLine($" '{addr}' se normaliza automáticamente");
|
|
// Todas se convierten internamente a "QW0"
|
|
}
|
|
|
|
// Ejemplo de bits
|
|
string[] bitEquivalents = { "PE0.0", "E0.0", "PI0.0", "I0.0" };
|
|
|
|
Console.WriteLine("\nDirecciones equivalentes para Input Bit 0.0:");
|
|
foreach (string addr in bitEquivalents)
|
|
{
|
|
Console.WriteLine($" '{addr}' se normaliza automáticamente");
|
|
// Todas se convierten internamente a "I0.0"
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Muestra cómo el comportamiento es completamente transparente para el usuario
|
|
/// </summary>
|
|
public static void DemostrarTransparencia()
|
|
{
|
|
Console.WriteLine("=== Transparencia del comportamiento ===");
|
|
|
|
// El mismo código funciona con cualquier driver
|
|
var plc = new PLCViewModel(); // El driver se configura en PlcData.ConnectionType
|
|
|
|
// Este código funciona igual independientemente del driver:
|
|
bool? input0 = plc.LeerBool("I0.0"); // o "PEW0", "EW0", etc. con Sharp7
|
|
bool? output0 = plc.LeerBool("Q0.0"); // o "PAW0", "AW0", etc. con Sharp7
|
|
int? word0 = plc.LeerTagInt16("IW0"); // o "PEW0", "EW0", etc. con Sharp7
|
|
|
|
plc.EscribirBool("Q0.0", true); // o "PAW0", "AW0", etc. con Sharp7
|
|
plc.EscribirTagInt16("QW0", 1234); // o "PAW0", "AW0", etc. con Sharp7
|
|
|
|
Console.WriteLine("El mismo código funciona con AdvCoSimulator y Sharp7");
|
|
Console.WriteLine("La diferencia en el comportamiento es completamente interna");
|
|
}
|
|
}
|
|
}
|