Last active
May 24, 2017 12:29
-
-
Save Naphier/7dab5b5271f7deb1f70037a39c35ea9e to your computer and use it in GitHub Desktop.
SerialPortController based off of https://github.com/DWilches/SerialCommUnity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
using System.Threading; | |
using System.Collections; | |
public class SerialPortController : MonoBehaviour | |
{ | |
protected Thread thread; | |
protected SerialThreadLines serialThread; | |
public UnityEvent OnConnected; | |
public UnityEvent OnDisconnected; | |
public UnityEvent OnPreShutDown; | |
[System.Serializable] | |
public class StringEvent : UnityEvent<string> { } | |
public StringEvent OnMessageReceived; | |
public string PortName { get; private set; } | |
public int BaudRate { get; private set; } | |
public int MaxMessageQueue { get; private set; } | |
private bool _connected = false; | |
public bool Connected | |
{ | |
get | |
{ | |
return _connected; | |
} | |
private set | |
{ | |
_connected = value; | |
if (_connected) | |
{ | |
OnConnected.Invoke(); | |
} | |
else | |
{ | |
OnDisconnected.Invoke(); | |
} | |
} | |
} | |
private void Awake() | |
{ | |
} | |
private bool initialized; | |
public void Initialize( | |
string portName, int baudRate, int maxMessageQueue = 10, int reconnectionDelay = 1000) | |
{ | |
Shutdown(); | |
PortName = portName; | |
BaudRate = baudRate; | |
MaxMessageQueue = maxMessageQueue; | |
serialThread = | |
new SerialThreadLines(portName, baudRate, maxMessageQueue, reconnectionDelay); | |
thread = new Thread(new ThreadStart(serialThread.RunForever)); | |
thread.Start(); | |
initialized = true; | |
} | |
private void Update() | |
{ | |
if (!initialized) | |
return; | |
string message = (string)serialThread.ReadMessage(); | |
if (message == null) | |
return; | |
if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) | |
{ | |
Connected = true; | |
} | |
else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) | |
Connected = false; | |
else | |
OnMessageReceived.Invoke(message); | |
} | |
public void Shutdown() | |
{ | |
initialized = false; | |
Connected = false; | |
OnPreShutDown.Invoke(); | |
if (serialThread != null) | |
{ | |
serialThread.RequestStop(); | |
serialThread = null; | |
} | |
if (thread != null) | |
{ | |
thread.Join(); | |
thread = null; | |
} | |
} | |
private void OnDisable() | |
{ | |
Shutdown(); | |
} | |
private void OnApplicationQuit() | |
{ | |
Shutdown(); | |
StartCoroutine(DelayedClose()); | |
if (!allowQuit) | |
{ | |
Application.CancelQuit(); | |
} | |
} | |
bool allowQuit = false; | |
private IEnumerator DelayedClose() | |
{ | |
if (allowQuit) | |
yield break; | |
while (thread != null && thread.IsAlive) | |
{ | |
yield return new WaitForEndOfFrame(); | |
} | |
yield return new WaitForSeconds(5); | |
Debug.Log("Closing late"); | |
allowQuit = true; | |
Application.Quit(); | |
} | |
public void SendSerialMessage(string message) | |
{ | |
if (serialThread != null) | |
serialThread.SendMessage(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment