前提・実現したいこと
Unityのオブジェクトの座標データなどを,シリアル通信を用いてSimulinkに送りたいと考えています.(最初は,落下するcubeのZ座標をSimulinkでモニタリングすることを考えています)
しかし,Unityで,もしくはSimukinkでのシリアル通信をそれぞれ調べてもよくわかりませんでした.(Simulinkでの受信は,Instrument Control ToolboxのSerial Receiveというブロックを使えばいいという事だけわかっています.)
どの様なスクリプトを書けばUnityから数値を取り出せるのでしょうか.
また,参考になりそうなWebページなどがあれば紹介していただきたいです.
一応,いろいろなサイトを参考に一応作ってみた,COMポートからの送受信をおこなえる?はずのソースコードを添付します.
どんな些細なことでも構いません,ご指導よろしくお願いします.
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
C#
1using UnityEngine; //unityのデフォルトの機能を使うためのクラス 2using System.Collections; //C#のデフォルト機能を使うためのクラス 3using System.IO.Ports; //シリアル通信を使用するためのクラス 4using System.Threading; //スレッド機能を利用可能にするためのクラス 5 6 7public class SerialHandler : MonoBehaviour { 8 9 public delegate void SerialDataReceivedEventHandler(string message); //??? 10 public event SerialDataReceivedEventHandler OnDataReceived; //??? 11 12 13 public string portName = "COM2"; 14 public int baudRate = 9600; //要変更 15 16 private SerialPort serialPort_; //serial portクラスのインスタンス生成 17 private Thread thread_; //スレッドクラスのインスタンス生成 18 private bool isRunning_ = false; //シリアルポートが開いているかどうか 19 20 private string message_; 21 private bool isNewMessageReceived_ = false; //シリアルポートから値が送られてきているかどうか 22 23 24 25 26 27 // Use this for initialization 28 void Start () { 29 Debug.LogWarning("Start"); 30 Open(); 31 } 32 33 34 35 36 37 // Update is called once per frame 38 void Update () { 39 Debug.LogWarning("Serial-Update"); 40 if (isNewMessageReceived_) 41 { 42 OnDataReceived(message_); 43 } 44 } 45 46 47 48 49 50 void OnDestroy(){ 51 Debug.LogWarning("OnDestroy"); 52 Close(); 53 } 54 55 56 57 58 59 private void Open(){ //シリアルポートを開いて、新しいスレッドを開く 60 61 serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One); //シリアルポートインスタンス生成 62 serialPort_.Open(); //シリアルポートを開く 63 64 isRunning_ = true; //開いた証としてtrueに変更 65 66 thread_ = new Thread(Read); //新しいスレッドを作成してread関数を実行させる 67 thread_.Start(); //thread開始 68 } 69 70 71 72 73 74 private void Read(){ 75 Debug.LogWarning("Read1"); 76 while (isRunning_ && serialPort_ != null && serialPort_.IsOpen) //シリアルポートが開いているかつシリアルポートインスタンスが存在する 77 { 78 Debug.LogWarning("Read2"); 79 try 80 { 81 message_ = serialPort_.ReadLine(); 82 isNewMessageReceived_ = true; //メッセージを受け取った証として変更 83 Debug.LogWarning(message_); 84 } 85 catch (System.Exception e) 86 { 87 Debug.LogWarning(e.Message); 88 } 89 } 90 } 91 92 93 94 95 96 private void Close(){ 97 isRunning_ = false; //シリアルポートを閉じた証としてfalseに変更 98 99 if (thread_ != null && thread_.IsAlive) //threadが存在しているなら 100 { 101 thread_.Join(); //元のスレッドの統合 102 } 103 104 if (serialPort_ != null && serialPort_.IsOpen) //シリアルポートにインスタンスが格納されていて、シリアルポートが開いていれば 105 { 106 serialPort_.Close(); //シリアルポートを閉じる 107 serialPort_.Dispose(); //バッファの解放とか? 108 } 109 } 110 111 112 public void Write(string message){ 113 try 114 { 115 serialPort_.Write(message); 116 } 117 catch (System.Exception e) 118 { 119 Debug.LogWarning(e.Message); 120 } 121 } 122 123 124 125 126 127 128 129 130} 131
###試したこと
Unity上でも現在のCubeのY座標がわかるように,以下のscriptを追加しました.
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6 7public class CubePosition : MonoBehaviour { 8 9 10 // Use this for initialization 11 void Start () { 12 } 13 14 // Update is called once per frame 15 void Update () { 16 17 Vector3 Cubeposi = GameObject.Find("Cube").transform.position; //Cubeの座標取り出し 18 19 float CubeY = Cubeposi.y; //CubeのY座標を定義 20 21 string strCubeY = CubeY.ToString(); 22 23 this.GetComponent<Text>().text = strCubeY; 24 25 } 26} 27
###補足情報(言語/FW/ツール等のバージョンなど)
(使用しているもの)
Windows10,Unity 2017.2.0f3(64bit),C# script,Simulink(MATLAB R2015b)