using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class RS232 : MonoBehaviour
{
My m;
void Start ()
{
// 建立一條執行緒讀取RS232,不這麼做 Unity 會死當
m = new My ();
new Thread (m.RunMe).Start ();
}
void OnApplicationQuit ()
{
// 結束 Unity 關閉執行緒
m.isRun = false;
}
}
class My
{
public bool isRun = true;
public void RunMe ()
{
// 設定、初始化 RS232
SerialPort port = new SerialPort ("COM3", 9600, Parity.None, 8, StopBits.One);
// 開啟 RS232
port.Open ();
// 持續讀取 RS232
while (isRun) {
string read = port.ReadLine ();
MonoBehaviour.print (read);
}
}
}