Click here to Skip to main content
16,016,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am receiving a data using Rs232 in the format 98654\t96534\t45698\t98546\t45645\n98546\t4598\t45698\t456978\t456455\n ........................... so on
so i used
C#
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;

C#
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();
           }
       }

i used string array size 40 because dont how much value they send aometime single or multiple so i took 40 array size then i split the value when \n comes and again i split the value when \t comes so i take only \t values and show using textboxes using inde 0,1,2,3,4 ..here i asigned indexed upto 10 but i am sending only 4 \t values so after reach 4 index its showing out of index please check the code and help to solve this problem

What I have tried:

Hi i am receiving a data using Rs232 in the format 98654\t96534\t45698\t98546\t45645\n98546\t4598\t45698\t456978\t456455\n ........................... so on
so i used
C#
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;

C#
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();
           }
       }
Posted
Updated 13-Jul-16 6:28am
Comments
Michael_Davies 13-Jul-16 12:27pm    
Which line throws the error?

1 solution

I'd say you need to check the results of your Split operation:
result1 = result[x].Split('t');
It look like (given your sample data) the data contains not 't' characters but '\t' instead: a TAB character instead of a lowercase alphabetic.
In which case you will only get one element in the array, and an index range exception when you try to use indexes bigger than zero.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900