我正在使用unity 3d可视化Arduino的串行输出并不断收到错误
FormatException:输入字符串的格式不正确。System.Number.StringToNumber(System.String str,System.Globalization.NumberStyles选项,System.Number + NumberBuffer&数字,System.Globalization.NumberFormatInfo信息,System.Boolean parseDecimal)(在<599589bf4ce248909b8a14cbe4a2034e>:0处)System.Number.ParseInt32 (System.String s,System.Globalization.NumberStyles样式,System.Globalization.NumberFormatInfo信息)(位于<599589bf4ce248909b8a14cbe4a2034e>:0)System.Int32.Parse(System.String s,System.IFormatProvider提供程序)(at <599589bf4ce248909b8a14cbe4a2034e> 0)System.Convert.ToInt32(System.String值)(在<599589bf4ce248909b8a14cbe4a2034e>:0处)One_mputest.Update()(在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
我期望向量变量会更改并移动脚本所应用于的对象
并仅收到有关以下错误:输入字符串的格式不正确
尝试并捕捉是您的朋友。\t
文件中可能有多余内容,您正在尝试将anull
转换为数字。一个很好的提示是,始终在if语句周围使用{}只是为了确保没有意外发生(例如:您认为某物在if语句内部,但在外部)
我要指出的一点是,您正在尝试将String
表示形式的a转换Float
为an 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.
} }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句