Version actualizada.
This commit is contained in:
25
BackupCognex/BackupCognex.csproj
Normal file
25
BackupCognex/BackupCognex.csproj
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="Cognex.DataMan.CogNamer.PC">
|
||||||
|
<HintPath>..\..\..\..\Cognex\DataMan SDK 5.6.3\Binaries\PC\Cognex.DataMan.CogNamer.PC.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Cognex.DataMan.SDK.Discovery.PC">
|
||||||
|
<HintPath>..\..\..\..\Cognex\DataMan SDK 5.6.3\Binaries\PC\Cognex.DataMan.SDK.Discovery.PC.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Cognex.DataMan.SDK.PC">
|
||||||
|
<HintPath>..\..\..\..\Cognex\DataMan SDK 5.6.3\Binaries\PC\Cognex.DataMan.SDK.PC.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="Cognex.DataMan.SDK.Utils.PC">
|
||||||
|
<HintPath>..\..\..\..\Cognex\DataMan SDK 5.6.3\Binaries\PC\Cognex.DataMan.SDK.Utils.PC.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
150
BackupCognex/Program.cs
Normal file
150
BackupCognex/Program.cs
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using Cognex.DataMan.SDK;
|
||||||
|
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
// Leer configuración desde config.txt en la misma carpeta que el ejecutable
|
||||||
|
string exeDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
|
string configPath = Path.Combine(exeDirectory, "config.txt");
|
||||||
|
|
||||||
|
if (!File.Exists(configPath))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No se encontró el archivo de configuración: {configPath}");
|
||||||
|
Console.WriteLine("\nPresiona cualquier tecla para salir...");
|
||||||
|
Console.ReadKey();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] configLines = File.ReadAllLines(configPath);
|
||||||
|
if (configLines.Length < 2)
|
||||||
|
{
|
||||||
|
Console.WriteLine("El archivo config.txt debe tener al menos dos líneas: IP y ruta del archivo .dmb");
|
||||||
|
Console.WriteLine("\nPresiona cualquier tecla para salir...");
|
||||||
|
Console.ReadKey();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string ipString = configLines[0].Trim();
|
||||||
|
string dmbPath = configLines[1].Trim().Trim('"');
|
||||||
|
Console.WriteLine($"[DEBUG] Intentando buscar el archivo en la ruta exacta: ->{dmbPath}<-");
|
||||||
|
|
||||||
|
// 1. Crear el sistema de conexión (Ethernet) usando la IP del archivo de configuración
|
||||||
|
EthSystemConnector connector = new EthSystemConnector(System.Net.IPAddress.Parse(ipString));
|
||||||
|
DataManSystem myDevice = new DataManSystem(connector);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myDevice.Connect();
|
||||||
|
|
||||||
|
// 2. Validar que el archivo .dmb existe en la ruta
|
||||||
|
if (!File.Exists(dmbPath))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No se encontró el archivo .dmb en la ruta: {dmbPath}");
|
||||||
|
Console.WriteLine("\nPresiona cualquier tecla para salir...");
|
||||||
|
Console.ReadKey();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Enviando archivo de backup .dmb al dispositivo... esto puede tardar unos segundos.");
|
||||||
|
|
||||||
|
// Poner el lector fuera de línea para que acepte archivos
|
||||||
|
Console.WriteLine("Forzando al lector a modo Offline...");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myDevice.SendCommand("SET SYSTEM.ONLINE OFF");
|
||||||
|
// Darle un segundo para que asimile el cambio de estado
|
||||||
|
System.Threading.Thread.Sleep(1000);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("El lector ya estaba offline o no soportó el comando, continuando...");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Enviando archivo de backup .dmb al dispositivo... esto puede tardar unos segundos.");// Poner el lector fuera de línea para que acepte archivos
|
||||||
|
Console.WriteLine("Forzando al lector a modo Offline...");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myDevice.SendCommand("SET SYSTEM.ONLINE OFF");
|
||||||
|
// Darle un segundo para que asimile el cambio de estado
|
||||||
|
System.Threading.Thread.Sleep(1000);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("El lector ya estaba offline o no soportó el comando, continuando...");
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("Enviando archivo de backup .dmb al dispositivo... esto puede tardar unos segundos.");
|
||||||
|
|
||||||
|
// 3. Usar el método nativo Restore del SDK
|
||||||
|
//myDevice.Restore(dmbPath);
|
||||||
|
|
||||||
|
// 3.1 Cargar la configuración principal (.cfg)
|
||||||
|
// Asegúrate de que dmbPath ahora apunte a tu archivo .cfg extraído
|
||||||
|
|
||||||
|
Console.WriteLine("Cargando archivo de Configuración (.cfg)...");
|
||||||
|
myDevice.SetConfig(dmbPath);
|
||||||
|
|
||||||
|
// Pausa recomendada para que el hardware procese el archivo
|
||||||
|
System.Threading.Thread.Sleep(2000);
|
||||||
|
|
||||||
|
// 4. Guardar la configuración permanentemente en la memoria Flash
|
||||||
|
Console.WriteLine("Guardando configuración en la memoria flash...");
|
||||||
|
myDevice.SendCommand("CONFIG.SAVE");
|
||||||
|
System.Threading.Thread.Sleep(1000);
|
||||||
|
|
||||||
|
// 5. Reiniciar el equipo para aplicar los cambios (y hacer que suene)
|
||||||
|
Console.WriteLine("Reiniciando el lector...");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
myDevice.SendCommand("REBOOT");
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Es completamente normal que REBOOT arroje un error o excepción de red aquí.
|
||||||
|
// Al reiniciarse, el lector "corta" el cable de red internamente, lo que el SDK interpreta como una desconexión abrupta.
|
||||||
|
// Lo atrapamos silenciosamente.
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("\nRestauración y reinicio ejecutados con éxito.");
|
||||||
|
|
||||||
|
// 3.2 Cargar la calibración del lente (.cdc)
|
||||||
|
// (Opcional: Solo si tu .dmb contenía un .cdc y necesitas calibrar el lente)
|
||||||
|
// string cdcPath = @"C:\Ruta\Al\Archivo_extraido.cdc";
|
||||||
|
// Console.WriteLine("Cargando archivo de Calibración (.cdc)...");
|
||||||
|
// myDevice.Restore(cdcPath);
|
||||||
|
|
||||||
|
|
||||||
|
Console.WriteLine("Restauración ejecutada con éxito. El lector aplicará la configuración.");
|
||||||
|
}
|
||||||
|
catch (Cognex.DataMan.SDK.UnknownErrorException ex)
|
||||||
|
{
|
||||||
|
// Este catch atrapará específicamente el error del DataMan que estabas viendo
|
||||||
|
Console.WriteLine("\n[ERROR COGNEX] El lector rechazó la operación.");
|
||||||
|
Console.WriteLine("Comando interno que falló: " + ex.Command);
|
||||||
|
Console.WriteLine("Detalle técnico: " + ex.Message);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Este catch atrapará cualquier otro error general (cables desconectados, fallos de red, etc.)
|
||||||
|
Console.WriteLine("\n[ERROR GENERAL]: " + ex.ToString());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Intentamos desconectar siempre para liberar el puerto
|
||||||
|
myDevice.Disconnect();
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error al intentar desconectar el dispositivo de forma segura.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("\nPresiona cualquier tecla para salir...");
|
||||||
|
Console.ReadKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
2
BackupCognex/config.txt
Normal file
2
BackupCognex/config.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
192.168.1.10
|
||||||
|
C:\Backups\mi_lector.dmb
|
||||||
Reference in New Issue
Block a user