Tratamento de exceções no módulo GNSS

This commit is contained in:
Diego Freitas 2026-03-04 08:51:49 -03:00
parent ea78440695
commit 8ea61c158e
1 changed files with 30 additions and 9 deletions

View File

@ -139,8 +139,15 @@ namespace AgroBase.Services
foreach (string comando in comandos) foreach (string comando in comandos)
{ {
byte[] bytesComando = Encoding.ASCII.GetBytes(comando); try
PortaGPS?.Write(bytesComando, 0, bytesComando.Length); {
byte[] bytesComando = Encoding.ASCII.GetBytes(comando);
PortaGPS?.Write(bytesComando, 0, bytesComando.Length);
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao enviar comando de configuração GNSS: {ex.Message}");
}
await Task.Delay(1000); // Pequeno delay para evitar sobrecarga na comunicação await Task.Delay(1000); // Pequeno delay para evitar sobrecarga na comunicação
} }
} }
@ -183,8 +190,15 @@ namespace AgroBase.Services
foreach (string cmd in comandos) foreach (string cmd in comandos)
{ {
byte[] bytes = Encoding.ASCII.GetBytes(cmd); try
PortaGPS.Write(bytes, 0, bytes.Length); {
byte[] bytes = Encoding.ASCII.GetBytes(cmd);
PortaGPS.Write(bytes, 0, bytes.Length);
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao enviar comando de configuração GNSS: {ex.Message}");
}
await Task.Delay(500); await Task.Delay(500);
} }
} }
@ -861,7 +875,7 @@ namespace AgroBase.Services
int port = 2101; int port = 2101;
string mountpoint = "EESC0"; string mountpoint = "EESC0";
string username = "Zendion"; // Geralmente vazio para RTK2Go string username = "Zendion"; // Geralmente vazio para RTK2Go
string password = "vyNEF$5*"; // Geralmente vazio para RTK2Go string password = "QD&m1p60"; // Geralmente vazio para RTK2Go
while (CorrecaoRTK_Ntrip && APIService.HasInternet) // Loop para reconectar em caso de falha while (CorrecaoRTK_Ntrip && APIService.HasInternet) // Loop para reconectar em caso de falha
{ {
@ -901,11 +915,18 @@ namespace AgroBase.Services
int bytesRead; int bytesRead;
while (CorrecaoRTK_Ntrip && (bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) while (CorrecaoRTK_Ntrip && (bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{ {
if (PortaGPS?.IsOpen ?? false) try
{ {
PortaGPS.Write(buffer, 0, bytesRead); // Envia os dados RTCM para o GPS if (PortaGPS?.IsOpen ?? false)
//Console.WriteLine($"Enviando {bytesRead} bytes de correção RTCM para o GPS"); {
UltimoEnvioCorrecaoRTK = DateTime.Now; PortaGPS.Write(buffer, 0, bytesRead); // Envia os dados RTCM para o GPS
//Console.WriteLine($"Enviando {bytesRead} bytes de correção RTCM para o GPS");
UltimoEnvioCorrecaoRTK = DateTime.Now;
}
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao enviar correção RTCM para o módulo GNSS: {ex.Message}");
} }
} }
} }