Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# Splitter on Bluetooth streaming data

Discussion in 'Scripting' started by UnicornLoop, Nov 30, 2018.

  1. UnicornLoop

    UnicornLoop

    Joined:
    Apr 21, 2015
    Posts:
    9
    Hello Guys

    I have a Bluetooth module that is streaming a stream of strings into a Unity application. Similar
    to the numo string variable. These are x , y and z from an imu sensor. I am trying to separate the
    values into their own x,y.z variables ie: int x , int y and int z.

    The code below is what I have done it working on the sample string numo data. But as so as I begin
    receiving the stream from the Bluetooth I receive this error.

    IndexOutOfRangeException: Index was outside the bounds of the array.

    public void fishing()
    {
    string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    string[] splitted = numo.Split(',');
    int[] nums = new int[splitted.Length];
    for (int i = 0; i < splitted.Length; i++)
    {
    nums = int.Parse(splitted);
    int x = nums[0];
    int y = nums[1];

    Debug.Log(x);
    Debug.Log(y);

    }
    }

    hope you can help.
     
  2. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    You are trying to parse an array of strings. int.Parse() expects one single string.
     
  3. UnicornLoop

    UnicornLoop

    Joined:
    Apr 21, 2015
    Posts:
    9
    Okay how can I make it work if my strings are costly been stream what are the alternative . Hope you can help
     
  4. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    You need to break down your problem piece by piece. Look at the input data, decide what you need and what you can ignore.

    Do you really get a stream of just numbers divided by a comma? How do you know which ones are x, y, z?

    Maybe the specification has example code in an other language you could just port.
     
  5. UnicornLoop

    UnicornLoop

    Joined:
    Apr 21, 2015
    Posts:
    9
    Hell

    Hello so I tested the code below is works on this test string it is only when I connect blue tooth do I receive :

    error. IndexOutOfRangeException: Index was outside the bounds of the array

    string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    string[] splitted = nomi.Split(',');
    int[] nums = new int[splitted.Length];
    for (int i = 0; i < splitted.Length; i++)
    {
    nums = int.Parse(splitted);
    int x = nums[0];
    int y = nums[1];
    int z = nums[2];
    Debug.Log(x);
    Debug.Log(y);
    Debug.Log(z);

    }
     
  6. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    I doubt that the code you typed in is the code you used. Copying and pasting it would enable us here in the forum to help you further.

    Please look up the documentation for int.Parse(String) (aka Int32.Parse(String)) and read the example code.
     
  7. UnicornLoop

    UnicornLoop

    Joined:
    Apr 21, 2015
    Posts:
    9
    Code (csharp):
    1.  
    2.     string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    3.         string[] splitted = numo.Split(',');
    4.         int[] nums = new int[splitted.Length];
    5.      
    6.         for (int i = 0; i < splitted.Length; i++)
    7.         {
    8.          
    9.             nums[I] = int.Parse(splitted[I]);
    10.             int x = nums[0];
    11.             int y = nums[1];
    12.             int z = nums[2];
    13.        
    14.             Debug.Log(x);
    15.             Debug.Log(y);
    16.             Debug.Log(z);
    17.         }
    18.      [CODE/]
    19.  
    20. This code works but as soon as I connect the Bluetooth. The error. IndexOutOfRangeException: Index was outside the bounds of the array appears. Maybe cause the code works with a fixed string length but the Bluetooth is continuous.
    21.  
    22. .[/I][/I]
     
    Last edited: Dec 1, 2018
  8. stefan_s_from_h

    stefan_s_from_h

    Joined:
    Nov 26, 2017
    Posts:
    72
    upload_2018-12-1_16-13-54.png

    Don't type in your code from memory. Copy and paste it. Read the post Using code tags properly (first sticky post in this Scripting forum).
     
  9. UnicornLoop

    UnicornLoop

    Joined:
    Apr 21, 2015
    Posts:
    9
    Hello

    So when I try the code below. This is the error:

    FormatException: Input string was not in a correct format.
    System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <f2e6809acb14476a81f399aeb800f8f2>:0)

    Code (csharp):
    1.  
    2.  
    3.   public void fishing()
    4.     {
    5.         string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    6.         string[] splitted = numo.Split(',');
    7.         int[] nums = new int[splitted.Length];
    8.         for (int i = 0; i < splitted.Length; i++)
    9.         {
    10.             //source of error.
    11.             nums[i] = int.Parse(splitted[i], System.Globalization.NumberStyles.AllowLeadingSign);
    12.             int x = nums[0];
    13.             int y = nums[1];
    14.             int z = nums[2];
    15.             Debug.Log(x);
    16.             Debug.Log(y);
    17.             Debug.Log(z);
    18.      
    19.         }
    20.     }
    21.  
    22.  
    I then tried to run the code without making the integer conversion. This is the error:

    IndexOutOfRangeException: Index was outside the bounds of the array.

    Code (csharp):
    1.  
    2.  
    3.   public void fishing()
    4.     {
    5.         string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    6.         string[] splitted = numo.Split(',');
    7.         int[] nums = new int[splitted.Length];
    8.         for (int i = 0; i < splitted.Length; i++)
    9.         {
    10.             string x = splitted[0];
    11.             string y = splitted[1];
    12.             string z = splitted[2];
    13.             Debug.Log(x);
    14.             Debug.Log(y);
    15.             Debug.Log(z);
    16.    
    17.         }
    18.     }
    19.  
    20.  
    So as explained I have a string x,y,z,x,y,z,x,y,z coming from a Bluetooth module. I would like to receive the data and set it as different variables.

    int x; int y, int z

    Hope you can help
     
  10. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    In your example you're splitting a string of 11 numbers, which isn't divisible by three.

    Also if you want to work with groups of 3 ints in your loop you need to increment i by 3 each time and not 1.

    Just to say, your second script posted above runs without error, I'm wondering if you meant it to be
    Code (CSharp):
    1.         string numo = "4,-1,5,7,8,10,5,-4,8,10,-30";
    2.         string[] splitted = numo.Split(',');
    3.         int[] nums = new int[splitted.Length];
    4.         for (int i = 0; i < splitted.Length; i++)
    5.         {
    6.             string x = splitted[i];
    7.             string y = splitted[i+1];
    8.             string z = splitted[i+2];
    9.             Debug.Log(x);
    10.             Debug.Log(y);
    11.             Debug.Log(z);
    12.  
    13.         }
    which does produce an error, because of the input string only having 11 elements. If you correct that and increment i by 3 each loop it should work properly.

    Also looking back on the earlier posts, and specifically #7; If, when the Bluetooth device is plugged, in it supplies a string with less than 3 elements then that would cause an index out of range error with the code you posted, as you're referencing indices 0,1 and 2.
     
    Last edited: Dec 2, 2018