如何修复,输入字符串格式不正确 unity 3d 和 serial
How to fix, Input string not in correct format unity 3d and serial
我正在使用 unity 3d 来可视化 Arduino 的串行输出并不断收到错误
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)
我试过使用插件,但没有效果
C# 脚本:
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);
}
}
串行输入:
-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
我希望矢量变量能够更改并移动应用了脚本的对象
并且只收到关于以下内容的错误:输入字符串的格式不正确
Try & catch 是你的朋友。文件中可能有一个额外的 \t
,而您正试图将 null
转换为某处的数字。另外一个好的提示是总是在 if 语句周围使用 {} 只是为了确保没有意外发生(例如:你认为某些东西在 if 语句内部但它在外部)
我要看的一点是,您正在尝试将代表 Float
的 String
转换为 Int32
,这可能是问题所在。
尝试这样的事情:
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.
} }
我正在使用 unity 3d 来可视化 Arduino 的串行输出并不断收到错误
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)
我试过使用插件,但没有效果
C# 脚本:
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);
}
}
串行输入:
-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
我希望矢量变量能够更改并移动应用了脚本的对象
并且只收到关于以下内容的错误:输入字符串的格式不正确
Try & catch 是你的朋友。文件中可能有一个额外的 \t
,而您正试图将 null
转换为某处的数字。另外一个好的提示是总是在 if 语句周围使用 {} 只是为了确保没有意外发生(例如:你认为某些东西在 if 语句内部但它在外部)
我要看的一点是,您正在尝试将代表 Float
的 String
转换为 Int32
,这可能是问题所在。
尝试这样的事情:
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.
} }