169 lines
7.2 KiB
C#
169 lines
7.2 KiB
C#
using LibS7Adv;
|
|
using System;
|
|
using System.Threading;
|
|
|
|
namespace ExampleUsage
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("LibS7Adv - Ejemplo de Uso");
|
|
Console.WriteLine("=========================");
|
|
|
|
// Crear instancia del ViewModel
|
|
var plcViewModel = new PLCViewModel();
|
|
|
|
// Ejemplo 1: Configurar para Snap7
|
|
Console.WriteLine("\n1. Configuración para Snap7:");
|
|
plcViewModel.PlcData.ConnectionType = ConnectionType.Sharp7;
|
|
plcViewModel.PlcData.IP = "192.168.1.100"; // Cambiar por IP real del PLC
|
|
|
|
Console.WriteLine($"Driver: {plcViewModel.PlcData.ConnectionType}");
|
|
Console.WriteLine($"IP: {plcViewModel.PlcData.IP}");
|
|
|
|
// Intentar conectar (comentado para ejemplo sin PLC real)
|
|
/*
|
|
Console.WriteLine("\nIntentando conectar...");
|
|
plcViewModel.Connect();
|
|
|
|
if (plcViewModel.IsConnected)
|
|
{
|
|
Console.WriteLine("Conectado exitosamente!");
|
|
|
|
// Ejemplo de lectura/escritura con Snap7
|
|
Console.WriteLine("\nEjemplo con Snap7 (direcciones directas):");
|
|
|
|
// Leer bit de marker
|
|
bool? markerBit = plcViewModel.LeerBool("%M0.0");
|
|
Console.WriteLine($"Marker M0.0: {markerBit}");
|
|
|
|
// Escribir bit de marker
|
|
plcViewModel.EscribirBool("%M0.1", true);
|
|
Console.WriteLine("Escrito M0.1 = true");
|
|
|
|
// Leer word de marker
|
|
string markerWord = plcViewModel.LeerNumber("%MW10");
|
|
Console.WriteLine($"Marker MW10: {markerWord}");
|
|
|
|
// Leer de Data Block
|
|
bool? dbBit = plcViewModel.LeerBool("%DB1.DBX0.0");
|
|
Console.WriteLine($"DB1.DBX0.0: {dbBit}");
|
|
|
|
plcViewModel.Disconnect();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Error de conexión: {plcViewModel.PlcData.LastError}");
|
|
}
|
|
*/
|
|
|
|
// Ejemplo 2: Configurar para AdvCoSimulator
|
|
Console.WriteLine("\n2. Configuración para AdvCoSimulator:");
|
|
plcViewModel.PlcData.ConnectionType = ConnectionType.AdvCoSimulator;
|
|
plcViewModel.PlcData.Name = "MiPLC"; // Nombre de la instancia PLCSim
|
|
|
|
Console.WriteLine($"Driver: {plcViewModel.PlcData.ConnectionType}");
|
|
Console.WriteLine($"Instancia: {plcViewModel.PlcData.Name}");
|
|
|
|
// Intentar conectar (comentado para ejemplo sin PLCSim)
|
|
/*
|
|
Console.WriteLine("\nIntentando conectar...");
|
|
plcViewModel.Connect();
|
|
|
|
if (plcViewModel.IsConnected)
|
|
{
|
|
Console.WriteLine("Conectado exitosamente!");
|
|
|
|
// Ejemplo con AdvCoSimulator (soporta tags y direcciones directas)
|
|
Console.WriteLine("\nEjemplo con AdvCoSimulator:");
|
|
|
|
// Usar direcciones directas (igual que Snap7)
|
|
bool? markerBit = plcViewModel.LeerBool("%M0.0");
|
|
Console.WriteLine($"Marker M0.0: {markerBit}");
|
|
|
|
// Usar nombres de tags simbólicos (solo AdvCoSimulator)
|
|
bool tagValue = plcViewModel.LeerTagBool("MiTagBooleano");
|
|
Console.WriteLine($"Tag 'MiTagBooleano': {tagValue}");
|
|
|
|
plcViewModel.EscribirTagBool("MiTagBooleano", !tagValue);
|
|
Console.WriteLine($"Tag 'MiTagBooleano' cambiado a: {!tagValue}");
|
|
|
|
int? intValue = plcViewModel.LeerTagInt16("MiTagEntero");
|
|
Console.WriteLine($"Tag 'MiTagEntero': {intValue}");
|
|
|
|
plcViewModel.Disconnect();
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Error de conexión: {plcViewModel.PlcData.LastError}");
|
|
}
|
|
*/
|
|
|
|
// Ejemplo 3: Mostrar API compatible entre drivers
|
|
Console.WriteLine("\n3. API Compatible entre Drivers:");
|
|
Console.WriteLine("=================================");
|
|
|
|
DemostrateAPI(plcViewModel, ConnectionType.Sharp7);
|
|
DemostrateAPI(plcViewModel, ConnectionType.AdvCoSimulator);
|
|
|
|
Console.WriteLine("\nPresione cualquier tecla para salir...");
|
|
Console.ReadKey();
|
|
}
|
|
|
|
static void DemostrateAPI(PLCViewModel viewModel, ConnectionType driverType)
|
|
{
|
|
Console.WriteLine($"\nUsando driver: {driverType}");
|
|
viewModel.PlcData.ConnectionType = driverType;
|
|
|
|
if (driverType == ConnectionType.Sharp7)
|
|
{
|
|
viewModel.PlcData.IP = "192.168.1.100";
|
|
}
|
|
else
|
|
{
|
|
viewModel.PlcData.Name = "MiPLC";
|
|
}
|
|
|
|
Console.WriteLine("Métodos disponibles:");
|
|
|
|
// Métodos que funcionan con ambos drivers
|
|
Console.WriteLine("✓ LeerBool(direccion) - Ambos drivers");
|
|
Console.WriteLine("✓ EscribirBool(direccion, valor) - Ambos drivers");
|
|
Console.WriteLine("✓ LeerNumber(direccion) - Ambos drivers");
|
|
Console.WriteLine("✓ LeerSalidaBool(byte, bit) - Ambos drivers");
|
|
Console.WriteLine("✓ EscribirInputBool(byte, bit, valor) - Ambos drivers");
|
|
|
|
// Métodos específicos de AdvCoSimulator
|
|
if (driverType == ConnectionType.AdvCoSimulator)
|
|
{
|
|
Console.WriteLine("✓ LeerTag(nombreTag) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ EscribirTag(nombreTag, valor) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ LeerTagBool(nombreTag) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ EscribirTagBool(nombreTag, valor) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ LeerTagInt16(nombreTag) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ EscribirTagInt16(nombreTag, valor) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ LeerTagDInt(nombreTag) - Solo AdvCoSimulator");
|
|
Console.WriteLine("✓ EscribirTagDInt(nombreTag, valor) - Solo AdvCoSimulator");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("✗ Métodos de Tag no disponibles en Snap7");
|
|
}
|
|
|
|
// Ejemplo de verificación de capacidades
|
|
Console.WriteLine("\nEjemplo de verificación de capacidades:");
|
|
Console.WriteLine("if (viewModel.PlcData.ConnectionType == ConnectionType.AdvCoSimulator)");
|
|
Console.WriteLine("{");
|
|
Console.WriteLine(" // Usar métodos de Tag");
|
|
Console.WriteLine(" bool valor = viewModel.LeerTagBool(\"MiTag\");");
|
|
Console.WriteLine("}");
|
|
Console.WriteLine("else");
|
|
Console.WriteLine("{");
|
|
Console.WriteLine(" // Usar solo direcciones directas");
|
|
Console.WriteLine(" bool valor = viewModel.LeerBool(\"%M0.0\");");
|
|
Console.WriteLine("}");
|
|
}
|
|
}
|
|
}
|