Manejo de Errores

-Códigos de Error de la DLL


CódigoConstanteDescripción
0MYDLL_OKOperación exitosa
1MYDLL_SETUP_ERRORError en configuración
2MYDLL_OPEN_ERRORNo se pudo abrir puerto
3MYDLL_SETPARAMS_ERRORError en parámetros serial
4MYDLL_IO_ERRORError de entrada/salida
5MYDLL_CANTSEND_ERRORNo se pudo enviar
6MYDLL_CANTREAD_ERRORNo se pudo leer respuesta
7MYDLL_PROTOCOL_ERRORError de protocolo
8MYDLL_TOOMANYRETRIES_ERRORDemasiados reintentos
999ERROR_WRITEREAD_EXCEPTIONExcepción en WriteRead
1000ERROR_DLL_NOT_FOUNDDLL 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();
}


What’s Next