C# Arduino-Unity串行通信

C# Arduino-Unity串行通信,c#,unity3d,arduino,lag,serial-communication,C#,Unity3d,Arduino,Lag,Serial Communication,我将在Unity和Arduino之间进行一次基本的交流。我遇到的问题是,当我开始从unity向arduino发送数据时,它工作正常。但当我开始读Arduino的连载时。我被拒绝访问错误 在我读了更多关于这个问题的文章后,我找不到真正的原因。我需要理解为什么会发生这样的事情 using UnityEngine; using System.Collections; using System; using System.IO.Ports; using System.Text; using

我将在Unity和Arduino之间进行一次基本的交流。我遇到的问题是,当我开始从unity向arduino发送数据时,它工作正常。但当我开始读Arduino的连载时。我被拒绝访问错误

在我读了更多关于这个问题的文章后,我找不到真正的原因。我需要理解为什么会发生这样的事情

 using UnityEngine;
 using System.Collections;
 using System;
 using System.IO.Ports;
 using System.Text;
 using System.Threading;

 public class Program : MonoBehaviour {

public SerialPort mySerialPort = new SerialPort("COM7", 9600);
public volatile float speed=0;
volatile bool isPassed = true;
GameObject cube ;
public GUIStyle style ;
//  int readInterval = 4;
//  int alreadyCounter = 0;
//  string rxString = "";

void Start () {
    isPassed = true;
    speed = 0;
    //mySerialPort.ReadTimeout = 25;
    mySerialPort.Parity = Parity.None;
    mySerialPort.StopBits = StopBits.One;
    mySerialPort.DataBits = 8;
    mySerialPort.DtrEnable = true;

    cube = GameObject.FindGameObjectWithTag ("cube");

    /*foreach(string str in SerialPort.GetPortNames())
    {
        Debug.Log(string.Format("Existing COM port: {0}", str));
    };
    */
    try{
    mySerialPort.Open();
    }catch(Exception ex)
    {
        print("Start Error Opening : " + ex.ToString());
    }

    /*
new Thread (delegate() {
        updateSpeed();
    }).Start();
     */

}

void OnGUI(){
    GUI.Box (new Rect(100,100,100,100),"Speed : " + speed , style);
}
// Update is called once per frame
void Update () {
    cube.transform.Rotate(Vector3.up * speed *Time.deltaTime);
    if (mySerialPort.IsOpen) {
        speed = float.Parse (mySerialPort.ReadLine ()); 
    } else {
        if(mySerialPort!= null)
        {
            mySerialPort.Close();
            mySerialPort.Open();
        }
    }

        /*if (isPassed == true) {
        new Thread(delegate() {
            if(mySerialPort.IsOpen) {
                updateSpeed();
                isPassed = false;
            } else {
                try {
                    mySerialPort.Close();
                    mySerialPort.Open();
                } catch (Exception ex) {
                    print("Update Error Opening : " + ex.ToString());
                }
            }
        }).Start();
    }*/

}

public void updateSpeed()
{
    while (true) {
        speed = float.Parse(mySerialPort.ReadLine ());
        print ("Speed = " + speed);
        Thread.Sleep (60);
        mySerialPort.BaseStream.Flush ();
    }
}

void OnDestroy () 
{
    mySerialPort.Close();
}

}
当您调用SerialPort时,会按照您在MSDN页面上看到的方式释放该端口。它还建议不要试图在关闭后立即重新打开它,因为它可能还没有关闭

此外,由于SerialPort将被释放,因此您必须创建一个新的SerialPort。我建议不要在更新中关闭和打开尚未打开的端口。在Start中打开它一次,然后在OnDestroy中关闭它

编辑 在调用Close时,似乎不必创建新的SerialPort。因此,它可以在更新后重新使用。

更新:


现在,您可以使用Microsoft.net开源框架,而不是unity默认框架,串行通信在这里非常有用。

发布准确的错误消息,不要转述或缩写,并突出显示错误发生的行。好的,我将尝试您的建议: