Siddaram K.Patil Ответов: 1

Системы.indexoutofrangeexceptio...


Привет я получаю данные с помощью Rs232 в формате 98654\t96534\t45698\t98546\t45645\n98546\t4598\t45698\t456978\t456455\n ........................... и так далее
поэтому я использовал
string[] ports = SerialPort.GetPortNames();
       string[] result = new string[40];
       string[] buff = new string[] { "\\n" };
       string[] result1 = new string[40];
       string RxString = string.Empty;
       double lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9, lb10, lb11, lb12, lb13, lb14, lb15, lb16, lb17, lb18, lb19, lb20;

private void Form1_Load(object sender, EventArgs e)
       {

       }

       private void ComPort_SelectedIndexChanged(object sender, EventArgs e)
       {
           serialPort1.Close();
           serialPort1.PortName = ports[ComPort.SelectedIndex];
           serialPort1.Open();
       }

       private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           RxString = serialPort1.ReadExisting();
           Thread.Sleep(200);
           this.Invoke(new EventHandler(Display));
       }
       private void Display(object sender, EventArgs e)
       {

           result = RxString.Split(buff, StringSplitOptions.None);
           int x = result.Length;

           for (x = 0; x < result.Length; x++)
           {

               if (result[x].Length > 1)
               {
                   result1 = result[x].Split('t');

                   if ((result1[0].Length == 5) || (result1[0].Length == 4) || (result1[0].Length == 3) || (result1[0].Length == 2) || (result1[0].Length == 1))
                   {
                       lb1 = Convert.ToDouble(result1[0]);
                       textBox1.Text = lb1.ToString();
                   }
                   else if ((result1[0].Length == 0) || (result1[0].Length < 0))
                   {
                       result1[0] = string.Empty;
                   }
                   if ((result1[1].Length == 5) || (result1[1].Length == 4) || (result1[1].Length == 3) || (result1[1].Length == 2) || (result1[1].Length == 1))
                   {
                       lb2 = Convert.ToDouble(result1[1]);
                       textBox2.Text = lb2.ToString();
                   }
                   else if ((result1[1].Length == 0) || (result1[1].Length < 0))
                   {
                       result1[1] = string.Empty;
                   }

                   if ((result1[2].Length == 5) || (result1[2].Length == 4) || (result1[2].Length == 3) || (result1[2].Length == 2) || (result1[2].Length == 1))
                   {
                       lb3 = Convert.ToDouble(result1[2]);
                       textBox3.Text = lb3.ToString();
                   }
                   else if ((result1[2].Length == 0) || (result1[2].Length < 0))
                   {
                       result1[2] = string.Empty;
                   }
                   if ((result1[3].Length == 5) || (result1[3].Length == 4) || (result1[3].Length == 3) || (result1[3].Length == 2) || (result1[3].Length == 1))
                   {
                       lb4 = Convert.ToDouble(result1[3]);
                       textBox4.Text = lb4.ToString();
                   }
                   else if((result1[3].Length==0)||(result1[3].Length<0))
                   {
                       result1[3] = string.Empty;
                   }
                   if ((result1[4].Length == 5) || (result1[4].Length == 4) || (result1[4].Length == 3) || (result1[4].Length == 2) || (result1[4].Length == 1))
                   {
                       lb5 = Convert.ToDouble(result1[4]);
                       textBox5.Text = lb5.ToString();
                   }
                   else if ((result1[4].Length == 0) || (result1[4].Length < 0))
                   {
                       result1[4] = string.Empty;
                   }

                   if ((result1[5].Length == 5) || (result1[5].Length == 4) || (result1[5].Length == 3) || (result1[5].Length == 2) || (result1[5].Length == 1))
                   {
                       lb6 = Convert.ToDouble(result1[5]);
                       textBox6.Text = lb6.ToString();
                   }
                   else if ((result1[5].Length == 0) || (result1[5].Length < 0))
                   {
                       result1[5] = string.Empty;
                   }
                   if ((result1[6].Length == 5) || (result1[6].Length == 4) || (result1[6].Length == 3) || (result1[6].Length == 2) || (result1[6].Length == 1))
                   {
                       lb7 = Convert.ToDouble(result1[6]);
                       textBox7.Text = lb7.ToString();
                   }
                   else if ((result1[6].Length == 0) || (result1[6].Length < 0))
                   {
                       result1[6] = string.Empty;
                   }
                   if ((result1[7].Length == 5) || (result1[7].Length == 4) || (result1[7].Length == 3) || (result1[7].Length == 2) || (result1[7].Length == 1))
                   {
                       lb8 = Convert.ToDouble(result1[7]);
                       textBox8.Text = lb8.ToString();
                   }
                   else if ((result1[7].Length == 0) || (result1[7].Length < 0))
                   {
                       result1[7] = string.Empty;
                   }
                   if ((result1[8].Length == 5) || (result1[8].Length == 4) || (result1[8].Length == 3) || (result1[8].Length == 2) || (result1[8].Length == 1))
                   {
                       lb9 = Convert.ToDouble(result1[8]);
                       textBox9.Text = lb9.ToString();
                   }
                   else if ((result1[8].Length == 0) || (result1[8].Length < 0))
                   {
                       result1[8] = string.Empty;
                   }
                   if ((result1[9].Length == 5) || (result1[9].Length == 4) || (result1[9].Length == 3) || (result1[9].Length == 2) || (result1[9].Length == 1))
                   {
                       lb10 = Convert.ToDouble(result1[9]);
                       textBox10.Text = lb10.ToString();
                   }
                   else if ((result1[9].Length == 0) || (result1[9].Length < 0))
                   {
                       result1[9] = string.Empty;
                   }

               }
               Database();
           }
       }

я использовал массив строк, размер 40, потому что не сколько значения они посылают aometime один или несколько поэтому я взяла 40 размер массива потом разделить значение при \п приходит и снова я разбить значение при \Т не приходит, поэтому я беру только \Т значений и показывают, используя текстовые поля, используя инде 0,1,2,3,4 ..вот я присоединена проиндексированных до 10, но я отправляю только 4 \T значений, поэтому после достижения 4 индекс показывает из индекса, пожалуйста, проверьте код и помочь решить эту проблему

Что я уже пробовал:

Привет я получаю данные с помощью Rs232 в формате 98654\t96534\t45698\t98546\t45645\n98546\t4598\t45698\t456978\t456455\n ........................... и так далее
поэтому я использовал
string[] ports = SerialPort.GetPortNames();
       string[] result = new string[40];
       string[] buff = new string[] { "\\n" };
       string[] result1 = new string[40];
       string RxString = string.Empty;
       double lb1, lb2, lb3, lb4, lb5, lb6, lb7, lb8, lb9, lb10, lb11, lb12, lb13, lb14, lb15, lb16, lb17, lb18, lb19, lb20;

private void Form1_Load(object sender, EventArgs e)
       {

       }

       private void ComPort_SelectedIndexChanged(object sender, EventArgs e)
       {
           serialPort1.Close();
           serialPort1.PortName = ports[ComPort.SelectedIndex];
           serialPort1.Open();
       }

       private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           RxString = serialPort1.ReadExisting();
           Thread.Sleep(200);
           this.Invoke(new EventHandler(Display));
       }
       private void Display(object sender, EventArgs e)
       {

           result = RxString.Split(buff, StringSplitOptions.None);
           int x = result.Length;

           for (x = 0; x < result.Length; x++)
           {

               if (result[x].Length > 1)
               {
                   result1 = result[x].Split('t');

                   if ((result1[0].Length == 5) || (result1[0].Length == 4) || (result1[0].Length == 3) || (result1[0].Length == 2) || (result1[0].Length == 1))
                   {
                       lb1 = Convert.ToDouble(result1[0]);
                       textBox1.Text = lb1.ToString();
                   }
                   else if ((result1[0].Length == 0) || (result1[0].Length < 0))
                   {
                       result1[0] = string.Empty;
                   }
                   if ((result1[1].Length == 5) || (result1[1].Length == 4) || (result1[1].Length == 3) || (result1[1].Length == 2) || (result1[1].Length == 1))
                   {
                       lb2 = Convert.ToDouble(result1[1]);
                       textBox2.Text = lb2.ToString();
                   }
                   else if ((result1[1].Length == 0) || (result1[1].Length < 0))
                   {
                       result1[1] = string.Empty;
                   }

                   if ((result1[2].Length == 5) || (result1[2].Length == 4) || (result1[2].Length == 3) || (result1[2].Length == 2) || (result1[2].Length == 1))
                   {
                       lb3 = Convert.ToDouble(result1[2]);
                       textBox3.Text = lb3.ToString();
                   }
                   else if ((result1[2].Length == 0) || (result1[2].Length < 0))
                   {
                       result1[2] = string.Empty;
                   }
                   if ((result1[3].Length == 5) || (result1[3].Length == 4) || (result1[3].Length == 3) || (result1[3].Length == 2) || (result1[3].Length == 1))
                   {
                       lb4 = Convert.ToDouble(result1[3]);
                       textBox4.Text = lb4.ToString();
                   }
                   else if((result1[3].Length==0)||(result1[3].Length<0))
                   {
                       result1[3] = string.Empty;
                   }
                   if ((result1[4].Length == 5) || (result1[4].Length == 4) || (result1[4].Length == 3) || (result1[4].Length == 2) || (result1[4].Length == 1))
                   {
                       lb5 = Convert.ToDouble(result1[4]);
                       textBox5.Text = lb5.ToString();
                   }
                   else if ((result1[4].Length == 0) || (result1[4].Length < 0))
                   {
                       result1[4] = string.Empty;
                   }

                   if ((result1[5].Length == 5) || (result1[5].Length == 4) || (result1[5].Length == 3) || (result1[5].Length == 2) || (result1[5].Length == 1))
                   {
                       lb6 = Convert.ToDouble(result1[5]);
                       textBox6.Text = lb6.ToString();
                   }
                   else if ((result1[5].Length == 0) || (result1[5].Length < 0))
                   {
                       result1[5] = string.Empty;
                   }
                   if ((result1[6].Length == 5) || (result1[6].Length == 4) || (result1[6].Length == 3) || (result1[6].Length == 2) || (result1[6].Length == 1))
                   {
                       lb7 = Convert.ToDouble(result1[6]);
                       textBox7.Text = lb7.ToString();
                   }
                   else if ((result1[6].Length == 0) || (result1[6].Length < 0))
                   {
                       result1[6] = string.Empty;
                   }
                   if ((result1[7].Length == 5) || (result1[7].Length == 4) || (result1[7].Length == 3) || (result1[7].Length == 2) || (result1[7].Length == 1))
                   {
                       lb8 = Convert.ToDouble(result1[7]);
                       textBox8.Text = lb8.ToString();
                   }
                   else if ((result1[7].Length == 0) || (result1[7].Length < 0))
                   {
                       result1[7] = string.Empty;
                   }
                   if ((result1[8].Length == 5) || (result1[8].Length == 4) || (result1[8].Length == 3) || (result1[8].Length == 2) || (result1[8].Length == 1))
                   {
                       lb9 = Convert.ToDouble(result1[8]);
                       textBox9.Text = lb9.ToString();
                   }
                   else if ((result1[8].Length == 0) || (result1[8].Length < 0))
                   {
                       result1[8] = string.Empty;
                   }
                   if ((result1[9].Length == 5) || (result1[9].Length == 4) || (result1[9].Length == 3) || (result1[9].Length == 2) || (result1[9].Length == 1))
                   {
                       lb10 = Convert.ToDouble(result1[9]);
                       textBox10.Text = lb10.ToString();
                   }
                   else if ((result1[9].Length == 0) || (result1[9].Length < 0))
                   {
                       result1[9] = string.Empty;
                   }

               }
               Database();
           }
       }

Michael_Davies

Какая строка выдает ошибку?

1 Ответов

Рейтинг:
2

OriginalGriff

Я бы сказал, что вам нужно проверить результаты вашей операции разделения:

result1 = result[x].Split('t');
Это выглядит так (учитывая ваши примеры данных), что данные содержат не символы 't', а '\t': символ табуляции вместо строчного алфавита.
В этом случае вы получите только один элемент в массиве и исключение диапазона индексов при попытке использовать индексы больше нуля.