Search Unity

String to useable variable?

Discussion in 'Scripting' started by vautourb, Jul 30, 2021.

  1. vautourb

    vautourb

    Joined:
    Jul 8, 2021
    Posts:
    10
    I've parsed a string and if I print to log Debug.Log(GPSdata[2]); it does receive the appropriate data, however, I am unable to use the data as it is read only. I am trying to get GPSdata_received to populate a Gameobject's position and rotation, but it can't seem to get the information in the array to be useful. Any assistance is appreciated

    Edit: I have added the string format of the string below.

    $GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W * 70

    The Editor error states:

    Assets\Serial.cs(38,17): error CS0200: Property or indexer 'string.this[int]' cannot be assigned to -- it is read only.

    Here is the code


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO.Ports;
    5.  
    6. public class Serial : MonoBehaviour
    7. {
    8.     SerialPort stream = new SerialPort("COM5", 9600);
    9.     public GameObject GPS;
    10.     public Vector3 rot;
    11.     public Vector3 rot2;
    12.     public string receivedstring;
    13.     public string[] GPSdata;
    14.     public string[] GPSdata_received;
    15.     void Start()
    16.     {
    17.         stream.Open(); //Open the Serial Stream.
    18.         Debug.Log("Port open");
    19.     }
    20.     void Update()
    21.     {
    22.         receivedstring = stream.ReadLine(); //Read the Serial Stream
    23.         stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
    24.         if (receivedstring.Contains("GPRMC"))
    25.         {
    26.             string[] GPSdata = receivedstring.Split(','); //Seperate stream by "," Delimiter
    27.             foreach (var GPSdata_received in GPSdata)
    28.             {
    29.                 GPSdata_received[0] = GPSdata[0]; // NMEA Format GPRMC
    30.                 GPSdata_received[1] = GPSdata[1]; // TimeStamp UTC-0
    31.                 GPSdata_received[2] = GPSdata[2]; // Validity
    32.                 GPSdata_received[3] = GPSdata[3]; // Latitude
    33.                 GPSdata_received[4] = GPSdata[4]; // North/South
    34.                 GPSdata_received[5] = GPSdata[5]; // Longitude
    35.                 GPSdata_received[6] = GPSdata[6]; // East/West
    36.                 GPSdata_received[7] = GPSdata[7]; // Speed in Knots
    37.                 GPSdata_received[8] = GPSdata[8]; // True Course
    38.                 GPSdata_received[9] = GPSdata[9]; // Date Stamp
    39.                 GPSdata_received[10] = GPSdata[10]; // Variation
    40.                 GPSdata_received[11] = GPSdata[11]; // East/West
    41.            
    42.             }
    43.         }        
    44.     }
    45.  

    Changed my string parsing to :

    Code (CSharp):
    1.             string[] GPSdata = receivedstring.Split(','); //Seperate stream by "," Delimiter
    2.             for (int i = 0; i < dataLength; i++)
    3.             {
    4.                 GPSdata_received[i] = GPSdata[i];
    5.                 if (i == 2)
    6.                 {
    7.                     Debug.Log(GPSdata[i]);
    8.                 }
    9.             }
     
    Last edited: Jul 30, 2021
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You've named your iterator variable in the foreach the same as your class-level member, which I reckon is where the error stems from. The whole foreach feels superfluous as you're indexing the resulting array manually anyway.

    I don't know what format the data is in, you didn't give an example, but I imagine you want to use
    float.Parse
    or
    float.TryParse
    to convert it to "useable" data.
     
  3. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,336
    You are using the same var name for your member and your foreach loop, namely "GPSdata_received". The error makes sense since using it in the for loop means you are trying to write to the data you are reading from (which is readonly). Rename your member and put "GPSdata_received" to the right of the = sign and you should be fine.