using System; namespace LibS7Adv { /// /// Ejemplo de uso de la biblioteca LibS7Adv con direcciones absolutas /// Demuestra el funcionamiento con ambos drivers: AdvCoSimulator y Sharp7 /// public class ExampleAbsoluteAddresses { private PLCViewModel plc; public ExampleAbsoluteAddresses() { plc = new PLCViewModel(); } /// /// Ejemplo de uso con Sharp7 usando direcciones absolutas /// NUEVA VERSIÓN: Funciones Tag unificadas que funcionan con ambos drivers /// public void ExampleSharp7Usage() { Console.WriteLine("=== Ejemplo de uso con Sharp7 (Interfaz Unificada) ==="); // Configurar conexión Sharp7 plc.PlcData.ConnectionType = ConnectionType.Sharp7; plc.PlcData.IP = "192.168.1.100"; // IP del PLC plc.PlcData.Name = "PLC_S7"; // Nombre descriptivo // Conectar plc.Connect(); if (plc.IsConnected) { Console.WriteLine("Conectado exitosamente!"); // === FUNCIONES BÁSICAS (siempre han funcionado) === bool? input = plc.LeerBool("I0.0"); plc.EscribirBool("Q0.1", true); string? temperature = plc.LeerNumber("DB1.DBW2"); plc.EscribirNumber("DB1.DBW4", 1234); // === NUEVAS FUNCIONES TAG UNIFICADAS (¡Ahora funcionan con Sharp7!) === // Funciones Bool Tag - ¡Funcionan igual que las básicas! bool motorStatus = plc.LeerTagBool("DB1.DBX0.0"); // → LeerBool("DB1.DBX0.0") plc.EscribirTagBool("DB1.DBX0.1", false); // → EscribirBool("DB1.DBX0.1", false) // Funciones Int16 Tag - ¡Ahora compatibles con Sharp7! int? speed = plc.LeerTagInt16("DB1.DBW6"); // → LeerNumber("DB1.DBW6") plc.EscribirTagInt16("DB1.DBW8", 2500); // → EscribirNumber("DB1.DBW8", 2500) // Funciones DInt Tag - ¡También funcionan! int? counter = plc.LeerTagDInt("DB1.DBD10"); // → LeerNumber("DB1.DBD10") plc.EscribirTagDInt("DB1.DBD14", 999999); // → EscribirNumber("DB1.DBD14", 999999) Console.WriteLine($"Motor Status: {motorStatus}"); Console.WriteLine($"Speed: {speed}"); Console.WriteLine($"Counter: {counter}"); Console.WriteLine($"Temperature: {temperature}"); // === EJEMPLOS CON DIFERENTES ÁREAS DE MEMORIA === // Entradas y Salidas bool inputSensor = plc.LeerTagBool("I0.2"); // → LeerBool("I0.2") plc.EscribirTagBool("Q0.3", true); // → EscribirBool("Q0.3", true) // Marcas (Memory) bool flagStatus = plc.LeerTagBool("M0.5"); // → LeerBool("M0.5") plc.EscribirTagBool("M0.6", false); // → EscribirBool("M0.6", false) int memoryWord = plc.LeerTagInt16("MW10") ?? 0; // → LeerNumber("MW10") plc.EscribirTagInt16("MW12", 5678); // → EscribirNumber("MW12", 5678) Console.WriteLine("¡Todas las funciones Tag ahora funcionan con Sharp7!"); } else { Console.WriteLine($"Error de conexión: {plc.PlcData.LastError}"); } plc.Disconnect(); } /// /// Ejemplo de uso con AdvCoSimulator usando direcciones absolutas /// NUEVA VERSIÓN: Misma interfaz unificada que Sharp7 /// public void ExampleAdvCoSimulatorUsage() { Console.WriteLine("=== Ejemplo de uso con AdvCoSimulator (Interfaz Unificada) ==="); // Configurar conexión AdvCoSimulator plc.PlcData.ConnectionType = ConnectionType.AdvCoSimulator; plc.PlcData.IP = "127.0.0.1"; // IP del simulador plc.PlcData.Name = "PLC_Instance"; // Nombre de la instancia // Conectar plc.Connect(); if (plc.IsConnected) { Console.WriteLine("Conectado exitosamente!"); // === MISMA INTERFAZ QUE SHARP7 === // ¡El código es IDÉNTICO al ejemplo de Sharp7! // Funciones básicas (siempre han funcionado) bool? input = plc.LeerBool("I0.0"); plc.EscribirBool("Q0.1", true); string? temperature = plc.LeerNumber("DB1.DBW2"); plc.EscribirNumber("DB1.DBW4", 1234); // Funciones Tag unificadas (¡mismo código que Sharp7!) bool motorStatus = plc.LeerTagBool("DB1.DBX0.0"); // Mapeo → LeerBool() plc.EscribirTagBool("DB1.DBX0.1", false); // Mapeo → EscribirBool() int? speed = plc.LeerTagInt16("DB1.DBW6"); // Mapeo → LeerNumber() plc.EscribirTagInt16("DB1.DBW8", 2500); // Mapeo → EscribirNumber() int? counter = plc.LeerTagDInt("DB1.DBD10"); // Mapeo → LeerNumber() plc.EscribirTagDInt("DB1.DBD14", 999999); // Mapeo → EscribirNumber() Console.WriteLine($"Motor Status: {motorStatus}"); Console.WriteLine($"Speed: {speed}"); Console.WriteLine($"Counter: {counter}"); Console.WriteLine($"Temperature: {temperature}"); // === FUNCIONES ESPECÍFICAS DE ADVCOSIMUADOR (OPCIONALES) === // Estas aún están disponibles pero también usan mapeo internamente var tagValue = plc.LeerTag("DB1.DBX0.0"); // Solo AdvCoSimulator plc.EscribirTag("DB1.DBX0.2", tagValue); // Solo AdvCoSimulator Console.WriteLine("¡Interfaz completamente unificada con Sharp7!"); Console.WriteLine("El mismo código funciona con ambos drivers."); } else { Console.WriteLine($"Error de conexión: {plc.PlcData.LastError}"); } plc.Disconnect(); } /// /// Ejemplo de uso unificado - El mismo código funciona con ambos drivers /// public void ExampleUnifiedUsage() { Console.WriteLine("=== Ejemplo de Interfaz Unificada ==="); Console.WriteLine("¡El mismo código funciona con Sharp7 Y AdvCoSimulator!"); Console.WriteLine(); // Probar con ambos drivers usando el mismo código TestWithDriver(ConnectionType.Sharp7, "192.168.1.100"); TestWithDriver(ConnectionType.AdvCoSimulator, "127.0.0.1"); } private void TestWithDriver(ConnectionType driverType, string ip) { Console.WriteLine($"--- Probando con {driverType} ---"); // Configurar driver plc.PlcData.ConnectionType = driverType; plc.PlcData.IP = ip; plc.PlcData.Name = driverType == ConnectionType.Sharp7 ? "PLC_Sharp7" : "PLC_Instance"; // Conectar plc.Connect(); if (plc.IsConnected) { Console.WriteLine("✓ Conectado exitosamente!"); // === EL MISMO CÓDIGO PARA AMBOS DRIVERS === try { // Operaciones Bool bool status1 = plc.LeerTagBool("DB1.DBX0.0"); plc.EscribirTagBool("DB1.DBX0.1", !status1); Console.WriteLine($" Bool: DB1.DBX0.0 = {status1}"); // Operaciones Int16 int value1 = plc.LeerTagInt16("DB1.DBW2") ?? 0; plc.EscribirTagInt16("DB1.DBW4", value1 + 100); Console.WriteLine($" Int16: DB1.DBW2 = {value1}"); // Operaciones DInt int value2 = plc.LeerTagDInt("DB1.DBD6") ?? 0; plc.EscribirTagDInt("DB1.DBD8", value2 + 1000); Console.WriteLine($" DInt: DB1.DBD6 = {value2}"); // Operaciones con áreas diferentes bool input = plc.LeerTagBool("I0.0"); plc.EscribirTagBool("Q0.1", input); Console.WriteLine($" I/O: I0.0 = {input} → Q0.1"); bool marker = plc.LeerTagBool("M0.2"); plc.EscribirTagBool("M0.3", !marker); Console.WriteLine($" Marker: M0.2 = {marker}"); Console.WriteLine($"✓ Todas las operaciones completadas con {driverType}"); } catch (Exception ex) { Console.WriteLine($"✗ Error: {ex.Message}"); } } else { Console.WriteLine($"✗ Error de conexión: {plc.PlcData.LastError}"); } plc.Disconnect(); Console.WriteLine($"--- Fin prueba con {driverType} ---"); Console.WriteLine(); } /// /// Muestra los formatos de direcciones válidos para Sharp7 /// public void ShowAddressFormats() { Console.WriteLine("=== Formatos de direcciones válidos para Sharp7 ==="); Console.WriteLine(); Console.WriteLine("BOOL (bits):"); Console.WriteLine(" I0.0, I0.1, ... I0.7 - Entradas (Inputs)"); Console.WriteLine(" Q0.0, Q0.1, ... Q0.7 - Salidas (Outputs)"); Console.WriteLine(" M0.0, M0.1, ... M0.7 - Marcas (Memory)"); Console.WriteLine(" DB1.DBX0.0, DB1.DBX0.1 - Data Block bits"); Console.WriteLine(); Console.WriteLine("NÚMEROS:"); Console.WriteLine(" IW0, IW2, IW4 - Input Words (16 bits)"); Console.WriteLine(" QW0, QW2, QW4 - Output Words (16 bits)"); Console.WriteLine(" MW0, MW2, MW4 - Memory Words (16 bits)"); Console.WriteLine(" DB1.DBW0, DB1.DBW2 - Data Block Words (16 bits)"); Console.WriteLine(" DB1.DBD0, DB1.DBD4 - Data Block DWords (32 bits)"); Console.WriteLine(" ID0, ID4, ID8 - Input DWords (32 bits)"); Console.WriteLine(" QD0, QD4, QD8 - Output DWords (32 bits)"); Console.WriteLine(" MD0, MD4, MD8 - Memory DWords (32 bits)"); } } }