1

I am using unity 3d to visualise a serial output from an Arduino and keep geting 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 <599589bf4ce248909b8a14cbe4a2034e>:0) System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Int32.Parse (System.String s, System.IFormatProvider provider) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Convert.ToInt32 (System.String value) (at <599589bf4ce248909b8a14cbe4a2034e>:0) One_mputest.Update () (at Assets/One_mputest.cs:48)

i have tried using plugins and they did not work

C# script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
using System.Linq;

public class One_mputest : MonoBehaviour {

private int x;
private int y;
private int z;

private int roll;
private int pitch;
private int yaw;

static SerialPort serial = new SerialPort("COM3", 9600);

// Use this for initialization
void Start () {

    serial.ReadTimeout = 5000;
    if (!serial.IsOpen) {
        try {
            serial.Open ();
        } catch (TimeoutException) {

        }

    } else
        Debug.LogError ("Port already open");
    }

// Update is called once per frame
void Update () {
    if (!serial.IsOpen)
        serial.Open ();

    string ypr = serial.ReadLine ();

    List<String> stringList = ypr.Split('\t').ToList();

    int[] intArray = new int[7];

    for (int s = 0; s < 7; s++)
    {
        intArray[s] = Convert.ToInt32(stringList[s]);
    }

    x = intArray[3];
    y = intArray[0];
    z = intArray[1];

    transform.localEulerAngles = new Vector3(z, y, x);
} 

}

The serial input:

-104.65  -10     5   65536.00    0.00    0.00    0.00    
-102.74  -4  1   65536.00    0.00    0.00    0.00    
-102.19  -2  0   65536.00    0.00    0.00    0.00    
-102.09  0   0   65536.00    0.00    0.00    0.00    
-101.75  0   0   65536.00    0.00    0.00    0.00    
-101.61  0   -1  65536.00    0.00    0.00    0.00    
-101.62  0   -1  65536.00    0.00    0.00    0.00    
-101.68  0   -1  65536.00    0.00    0.00    0.00    
-101.76  0   -1  65536.00    0.00    0.00    0.00    
-102.02  0   -1  65536.00    0.00    0.00    0.00    
-102.20  0   -1  65536.00    0.00    0.00    0.00    
-102.33  0   -1  65536.00    0.00    0.00    0.00    
-102.43  0   -1  65536.00    0.00    0.00    0.00    
-102.57  0   -1  65536.00    0.00    0.00    0.00    
-102.77  0   -1  65536.00    0.00    0.00    0.00    
-102.90  0   -1  65536.00    0.00    0.00    0.00    
-102.68  0   -1  65536.00    0.00    0.00    0.00    
-102.48  0   -1  65536.00    0.00    0.00    0.00    
-102.26  0   -1  65536.00    0.00    0.00    0.00    
-102.01  0   -1  65536.00    0.00    0.00    0.00    
-101.83  0   -1  65536.00    0.00    0.00    0.00    
-102.15  0   -1  65536.00    0.00    0.00    0.00    
-102.34  0   -1  65536.00    0.00    0.00    0.00    
-102.17  0   -1  65536.00    0.00    0.00    0.00    
-101.89  0   -1  65536.00    0.00    0.00    0.00    
-101.74  0   -1  65536.00    0.00    0.00    0.00    
-101.88  0   -1  65536.00    0.00    0.00    0.00    
-101.95  0   -1  65536.00    0.00    0.00    0.00    
-102.01  0   -1  65536.00    0.00    0.00    0.00    
-102.05  0   -1  65536.00    0.00    0.00    0.00    
-102.06  0   -1  65536.00    0.00    0.00    0.00    
-102.00  0   -1  65536.00    0.00    0.00    0.00    
-101.96  0   -1  65536.00    0.00    0.00    0.00    
-102.06  0   -1  65536.00    0.00    0.00    0.00    
-102.14  0   -1  65536.00    0.00    0.00    0.00    
-102.37  0   -1  65536.00    0.00    0.00    0.00    
-102.81  0   -1  65536.00    0.00    0.00    0.00   

I expected the vector variable to change and move the object that the script was applied to

and only got the error about: Input string was not in a correct format

2
  • Parsing strings to proper values is notoriously tricky. As a general rule, you should be using TryParse(). However as you have not told us wich value you want each serial input to the parsed into, we can not help you. I mean what number(s) should "-104.65 -10 5 65536.00 0.00 0.00 0.00" be parsed to? Commented Oct 21, 2019 at 0:39
  • What is the value of stringList[s] and stringList[s].Length when the exception is thrown? Commented Oct 21, 2019 at 1:11

1 Answer 1

1

Try & catch are your friends. It's possible there is an extra \t in the file and you are trying to convert a null into a number somewhere. Also a good tip is always use {} around if statements just to make sure nothing unexpected happens (eg: you think something is inside an if statement but it's outside)

One point I would look at is that you are trying to convert a String representing a Float to an Int32, this may be where things are going wrong.

Try something like this:

void Update () {
    var whatdoing = "starting try";

    try {
        if (!serial.IsOpen) {
            serial.Open (); }

        whatdoing               = "reading line";
        string ypr              = serial.ReadLine ();

        List<String> stringList = ypr.Split('\t').ToList();
        int[] intArray          = new int[7];

         for (int s = 0; s < 7; s++) {
             whatdoing = "converting: " + stringList[s];

             if(!Int.TryParse(stringList[s], out intArray[s]) {
                 throw new Exception("Could not parse string to Int: "+whatdoing); } }
          whatdoing = "Assigning xyz";
          x         = intArray[3];
          y         = intArray[0];
          z         = intArray[1];

          transform.localEulerAngles = new Vector3(z, y, x);}
catch(Exception e) {
 BREAK HERE and check exception & whatdoing.
} }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.