USB Communication from Arduino to Unity: Timeout Error

一笑奈何 提交于 2019-12-11 03:03:28

问题


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.

来源:https://stackoverflow.com/questions/31205982/usb-communication-from-arduino-to-unity-timeout-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!