前提・実現したいこと
Arduinoから送られた数値をもとにUnity上のオブジェクトを回転させようと考えています。
発生している問題・エラーメッセージ
考えた通りの動きはするのですが動作が非常に緩慢であり、使い物になりません。
該当のソースコード
C#(オブジェクトの回転) using System.Collections; using System.Collections.Generic; using UnityEngine; public class rot : MonoBehaviour { float speed=1.0f; public IOPort IOPort; void Update () { if (IOPort.flag == 1) { //時計回り transform.RotateAround(new Vector3(0, 0, 0), Vector3.back, speed); } } } ------------------------------------------------------------------------------------------ C#(Arduinoとのシリアル通信) using System.Collections; using System.IO.Ports; using System.Collections.Generic; using UnityEngine; public class IOPort : MonoBehaviour { int data = 0; SerialPort Port1 = new SerialPort("COM3", 9600); public int flag=0; void Start () { if (Port1.IsOpen) { Port1.Close(); } else { Port1.Open(); Port1.ReadTimeout = 1000; } } void Update () { data = Port1.ReadByte(); Debug.Log(data); if (data == 30) { flag = 1;} if (data == 40) { flag = 0;} } } -------------------------------------------------------------------------------- Arduino #define UNITY 2 void setup() { pinMode(UNITY,OUTPUT); Serial.begin(9600); } void loop() { Serial.write(30); Serial.flush(); digitalWrite(UNITY,HIGH); delay(10); digitalWrite(UNITY,LOW); delay(5000); Serial.write(40); Serial.flush(); digitalWrite(UNITY,HIGH); delay(10); digitalWrite(UNITY,LOW); delay(5000); }
試したこと
Arduinoを使わずUnityのみを用いてキー入力によるフラグ操作をすると求めていた速度で動作します
補足情報(FW/ツールのバージョンなど)
コンソールを確認したところ以下のエラーが表示されていました。
TimeoutException: The operation has timed-out.
System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/10 10:06
2019/06/10 10:57 編集
2019/06/10 13:49
2019/06/11 04:19