Skip to content

Commit 5e1534d

Browse files
authored
Merge pull request #75 from Xaverix/read-whole-buffer
Add functionality to read all messages in queue
2 parents 42a047a + d163f7f commit 5e1534d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

UnityProject/Assets/Ardity/Scripts/SerialController.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class SerialController : MonoBehaviour
4848
"newest messages from the port.")]
4949
public bool dropOldMessage;
5050

51+
[Tooltip("Read all unread messages in the queue during every Update loop. " +
52+
"Only used when \"Message Listener\" is provided.")]
53+
public bool readAllMessages;
54+
5155
// Constants used to mark the start and end of a connection. There is no
5256
// way you can generate clashing messages from your serial device, as I
5357
// compare the references of these strings, no their contents. So if you
@@ -106,10 +110,8 @@ void OnDisable()
106110
}
107111

108112
// ------------------------------------------------------------------------
109-
// Polls messages from the queue that the SerialThread object keeps. Once a
110-
// message has been polled it is removed from the queue. There are some
111-
// special messages that mark the start/end of the communication with the
112-
// device.
113+
// Calls message polling every frame. Does nothing when message listener
114+
// is not provided.
113115
// ------------------------------------------------------------------------
114116
void Update()
115117
{
@@ -118,6 +120,23 @@ void Update()
118120
if (messageListener == null)
119121
return;
120122

123+
// If the user prefers to read all messages, then enter a loop
124+
// and read messages until the queue is empty
125+
if (readAllMessages)
126+
while (serialThread.InputQueueCount() > 0)
127+
ReadSerialMessageToMessageListener();
128+
else
129+
ReadSerialMessageToMessageListener();
130+
}
131+
132+
// ------------------------------------------------------------------------
133+
// Polls messages from the queue that the SerialThread object keeps. Once a
134+
// message has been polled it is removed from the queue. There are some
135+
// special messages that mark the start/end of the communication with the
136+
// device.
137+
// ------------------------------------------------------------------------
138+
private void ReadSerialMessageToMessageListener()
139+
{
121140
// Read the next message from the queue
122141
string message = (string) serialThread.ReadMessage();
123142
if (message == null)

UnityProject/Assets/Ardity/Scripts/Threads/AbstractSerialThread.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ public object ReadMessage()
108108
return inputQueue.Dequeue();
109109
}
110110

111+
// ------------------------------------------------------------------------
112+
// Returns number of messages in the input queue.
113+
// It returns 0 if queue is not initialized.
114+
// ------------------------------------------------------------------------
115+
public int InputQueueCount()
116+
{
117+
if (inputQueue == null)
118+
return 0;
119+
120+
return inputQueue.Count;
121+
}
122+
111123
// ------------------------------------------------------------------------
112124
// Schedules a message to be sent. It writes the message to the
113125
// output queue, later the method 'RunOnce' reads this queue and sends

0 commit comments

Comments
 (0)