USB Communication from Arduino to Unity: Timeout E

2019-07-11 07:52发布

I'm trying to set up the simplest possible USB communication between an Arduino and Unity 3D: sending a few bytes from the Arduino and then reading them from Unity.

I'm using an older MBP, Unity 4.6.6, and an Arduino Uno with Arduino 1.0.6.

Arduino:

void setup() {

  Serial.begin(9600);

}

void loop() {

 int val=45;
  Serial.write(val);
  delay(10);   

}

Unity:

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

public class ardCom : MonoBehaviour {


    SerialPort stream = new SerialPort("/dev/tty.usbmodem621",9600);

    // Use this for initialization
    void Start () {

        stream.Open ();

    }

    // Update is called once per frame
    void Update () {

        int value = stream.ReadByte ();
        Debug.Log (value);

    }

    void OnApplicationQuit() 

    {
        stream.Close();
    }

}

But... Unity keeps throwing a timeout error:

TimeoutException: The operation has timed-out. System.IO.Ports.SerialPortStream.Read (System.Byte[] buffer, Int32 offset, Int32 count) System.IO.Ports.SerialPort.read_byte () System.IO.Ports.SerialPort.ReadByte () (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadByte () ardCom.Update () (at Assets/ardCom.cs:20)

What's odd is that I've been able to get communication working the other way, from Unity to Arduino-- ye olde Blinky LED tutorial works fine.

I've tried just about every permutation of this code that I can think of, changing baudrates, delay times, Serial write and read calls on the Arduino and Unity code, and been all over the net looking for solutions. No luck.

0条回答
登录 后发表回答