using UnityEngine;
using System.Collections;
// 記得在 Unity 中指定使用 MicroSoft 的 .NET 套件 ( Edit → Project Setting → Player → Other Settings → Api Compatibility → 選擇 .NET 2.0 )
using System.IO.Ports;
public class Test : MonoBehaviour {
void Start () {
SerialPort sp = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One); // 通訊埠為COM5、波特率(Baud rate) 9600
sp.Open(); // 打開 COM5 通訊埠
print("Sey : " + sp.ReadLine()); // 從單晶片輸出的信號會在Unity這裡印出
sp.WriteLine("Hello Kitty"); // 從 Unity 輸出至 單晶片中
print("Sey : " + sp.ReadLine()); // 從單晶片輸出的信號會在Unity這裡印出
sp.Close(); // 關閉 COM5 通訊埠
}
}
輸出結果:
全站熱搜