using System; using System.IO.Ports; using UnityEngine; using UnityEngine.UI; public class SerialPortController : MonoBehaviour { private SerialDevice _sp; private string _message; public Text text; private void Start() { if (Application.platform == RuntimePlatform.Android) { try { this._sp = new SerialDevice("/dev/ttyS4", BaudRate.B9600, Parity.None, 8, StopBits.One, Handshake.None); this._sp.DataReceived += (s, e) => { try { if (e.Length > 0) { this._message = BitConverter.ToString(e); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }; this._sp.Open(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } // Update is called once per frame private void Update() { this.text.text = this._message; } private void OnDestroy() { if (Application.platform == RuntimePlatform.Android) { try { if (this._sp != null) { this._sp.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }