using UnityEngine;using System.Collections;using System.IO.Ports;using System.IO;using System;using System.Linq;using System.Collections.Generic;public class Arduino : MonoBehaviour{ SerialPort sp = new SerialPort("COM3", 9600); void Start() //called once when the program is run { sp.Open(); } void Update() //called once every frame { if (sp.IsOpen) { try { serialEvent(sp); } catch (Exception) { throw; } } } void serialEvent(SerialPort myPort) { string myString = myPort.ReadLine(); if (myString != null) { Debug.Log("Read " + myString); } }}
SerialPort sp = new SerialPort("COM3", 9600);sp.Open(); While true() { if (sp.IsOpen) { try { serialEvent(sp); } catch (Exception) { throw; } } } void serialEvent(SerialPort myPort) { string myString = myPort.ReadLine(); if (myString != null) { Debug.Log("Read " + myString); } }