0
Good morning! I am working on a project for my PAP, and for a company and I cannot understand what this mistake is, I have already researched in several places but still do not understand, I need to solve this mistake as soon as possible! My program is passing all the data from the Internet read by the sensor, to the console c#, sometimes the program works normally other times of error... I’ll put the code that makes me wrong:
 internal static class Program
   {
       public static void Main(string[] args)
       {
           var myPort = new SerialPort {BaudRate = 9600, PortName = "COM4"};
           myPort.Open();""
           string humidade = "", temperatura = "", heatIndex = "", lpgGas = "", monoCarbo = "", smoke = "", tempo = ""; 
           var macAddr = 
           (
               from nic in NetworkInterface.GetAllNetworkInterfaces()
               where nic.OperationalStatus == OperationalStatus.Up
               select nic.GetPhysicalAddress().ToString()
           ).FirstOrDefault();
           Console.WriteLine("Mac adress do pc: {0}", macAddr);
           
           while (true)
           {
                   var dataRx = myPort.ReadLine(); 
                  
                   var underscore = dataRx.Split('_');
                   humidade = underscore[1];
                   temperatura = underscore[2];
                   heatIndex = underscore[3];
                   tempo = underscore[4];
                   lpgGas = underscore[5];
                   monoCarbo = underscore[6];
                   smoke = underscore[7];
                   
                 
                   Console.WriteLine(tempo);
                   Console.WriteLine(humidade);
                   Console.WriteLine(temperatura);
                   Console.WriteLine(heatIndex);
                   Console.WriteLine(lpgGas);
                   Console.WriteLine(monoCarbo);
                   Console.WriteLine(smoke);
                   Console.WriteLine("___________________");
     
           }
       }
 
   }
error means you are reading some index that does not exist in the array, for example you are missing position 5, 6,7, etc. put this in the array
try/catchto capture the error and show what you have in the variableunderscoreand you’ll find the problem– Ricardo Pontual