Manejo de Errores
-Códigos de Error de la DLL
| Código | Constante | Descripción |
|---|---|---|
| 0 | MYDLL_OK | Operación exitosa |
| 1 | MYDLL_SETUP_ERROR | Error en configuración |
| 2 | MYDLL_OPEN_ERROR | No se pudo abrir puerto |
| 3 | MYDLL_SETPARAMS_ERROR | Error en parámetros serial |
| 4 | MYDLL_IO_ERROR | Error de entrada/salida |
| 5 | MYDLL_CANTSEND_ERROR | No se pudo enviar |
| 6 | MYDLL_CANTREAD_ERROR | No se pudo leer respuesta |
| 7 | MYDLL_PROTOCOL_ERROR | Error de protocolo |
| 8 | MYDLL_TOOMANYRETRIES_ERROR | Demasiados reintentos |
| 999 | ERROR_WRITEREAD_EXCEPTION | Excepción en WriteRead |
| 1000 | ERROR_DLL_NOT_FOUND | DLL no encontrada |
-Manejo de Excepciones en UI
```csharp
try
{
await Task.Run(() =>
{
resultado = Tmapp_IPOS.WriteRead(strjsonRequest, utimeout);
});
}
catch (OperationCanceledException)
{
MessageBox.Show("Transacción cancelada por el usuario",
"Cancelado", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
catch (Exception ex)
{
MessageBox.Show($"Error en la transacción: {ex.Message}",
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
-Validación de JSON
```csharp
bool IsValidJson(string jsonString)
{
try
{
JToken.Parse(jsonString);
return true;
}
catch (JsonReaderException)
{
return false;
}
}
// Uso:
if (IsValidJson(resultado.response))
{
var response =
JsonConvert.DeserializeObject<SaleResponse>(resultado.response);
}
```-Logging de Errores
```csharp
private void Tracelog(string strtext)
{
listBox1.SelectedIndexChanged -= ListBox1_SelectedIndexChanged;
int idx = listBox1.Items.Add(
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + strtext
);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndexChanged += ListBox1_SelectedIndexChanged;
listBox1.Refresh();
}Updated 13 days ago
What’s Next
