Tanoda
SerialControllerCustomDelimiter.cs
Go to the documentation of this file.
1
9using System.Threading;
10using UnityEngine;
11
18public class SerialControllerCustomDelimiter : MonoBehaviour
19{
20#if !UNITY_WEBGL
21 [Tooltip("Port name with which the SerialPort object will be created.")]
22 public string portName = "COM3";
23
24 [Tooltip("Baud rate that the serial device is using to transmit data.")]
25 public int baudRate = 9600;
26
27 [Tooltip("Reference to an scene object that will receive the events of connection, " +
28 "disconnection and the messages from the serial device.")]
29 public GameObject messageListener;
30
31 [Tooltip("After an error in the serial communication, or an unsuccessful " +
32 "connect, how many milliseconds we should wait.")]
33 public int reconnectionDelay = 1000;
34
35 [Tooltip("Maximum number of unread data messages in the queue. " +
36 "New messages will be discarded.")]
37 public int maxUnreadMessages = 1;
38
39 [Tooltip("Maximum number of unread data messages in the queue. " +
40 "New messages will be discarded.")]
41 public byte separator = 90;
42
43 // Internal reference to the Thread and the object that runs in it.
44 protected Thread thread;
45 protected SerialThreadBinaryDelimited serialThread;
46
47
48 // ------------------------------------------------------------------------
49 // Invoked whenever the SerialController gameobject is activated.
50 // It creates a new thread that tries to connect to the serial device
51 // and start reading from it.
52 // ------------------------------------------------------------------------
53 void OnEnable()
54 {
55 serialThread = new SerialThreadBinaryDelimited(portName,
59 separator);
60 thread = new Thread(serialThread.RunForever);
61 thread.Start();
62 }
63
64 // ------------------------------------------------------------------------
65 // Invoked whenever the SerialController gameobject is deactivated.
66 // It stops and destroys the thread that was reading from the serial device.
67 // ------------------------------------------------------------------------
68 void OnDisable()
69 {
70 // If there is a user-defined tear-down function, execute it before
71 // closing the underlying COM port.
72 if (userDefinedTearDownFunction != null)
73 userDefinedTearDownFunction();
74
75 // The serialThread reference should never be null at this point,
76 // unless an Exception happened in the OnEnable(), in which case I've
77 // no idea what face Unity will make.
78 if (serialThread != null)
79 {
80 serialThread.RequestStop();
81 serialThread = null;
82 }
83
84 // This reference shouldn't be null at this point anyway.
85 if (thread != null)
86 {
87 thread.Join();
88 thread = null;
89 }
90 }
91
92 // ------------------------------------------------------------------------
93 // Polls messages from the queue that the SerialThread object keeps. Once a
94 // message has been polled it is removed from the queue. There are some
95 // special messages that mark the start/end of the communication with the
96 // device.
97 // ------------------------------------------------------------------------
98 void Update()
99 {
100 // If the user prefers to poll the messages instead of receiving them
101 // via SendMessage, then the message listener should be null.
102 if (messageListener == null)
103 return;
104
105 // Read the next message from the queue
106 var message = ReadSerialMessage();
107 if (message == null)
108 return;
109
110 // Check if the message is plain data or a connect/disconnect event.
111 messageListener.SendMessage("OnMessageArrived", message);
112 }
113
114 // ------------------------------------------------------------------------
115 // Returns a new unread message from the serial device. You only need to
116 // call this if you don't provide a message listener.
117 // ------------------------------------------------------------------------
118 public byte[] ReadSerialMessage()
119 {
120 // Read the next message from the queue
121 return (byte[]) serialThread.ReadMessage();
122 }
123
124 // ------------------------------------------------------------------------
125 // Puts a message in the outgoing queue. The thread object will send the
126 // message to the serial device when it considers it's appropriate.
127 // ------------------------------------------------------------------------
128 public void SendSerialMessage(byte[] message)
129 {
130 serialThread.SendMessage(message);
131 }
132
133 // ------------------------------------------------------------------------
134 // Executes a user-defined function before Unity closes the COM port, so
135 // the user can send some tear-down message to the hardware reliably.
136 // ------------------------------------------------------------------------
137 public delegate void TearDownFunction();
138
139 private TearDownFunction userDefinedTearDownFunction;
140 public void SetTearDownFunction(TearDownFunction userFunction)
141 {
142 userDefinedTearDownFunction = userFunction;
143 }
144#endif
145}
delegate void TearDownFunction()
void SetTearDownFunction(TearDownFunction userFunction)