Skip to content

Commit

Permalink
Merge pull request #75 from Xaverix/read-whole-buffer
Browse files Browse the repository at this point in the history
Add functionality to read all messages in queue
  • Loading branch information
dwilches authored Mar 31, 2024
2 parents 42a047a + d163f7f commit 5e1534d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
27 changes: 23 additions & 4 deletions UnityProject/Assets/Ardity/Scripts/SerialController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class SerialController : MonoBehaviour
"newest messages from the port.")]
public bool dropOldMessage;

[Tooltip("Read all unread messages in the queue during every Update loop. " +
"Only used when \"Message Listener\" is provided.")]
public bool readAllMessages;

// Constants used to mark the start and end of a connection. There is no
// way you can generate clashing messages from your serial device, as I
// compare the references of these strings, no their contents. So if you
Expand Down Expand Up @@ -106,10 +110,8 @@ void OnDisable()
}

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

// If the user prefers to read all messages, then enter a loop
// and read messages until the queue is empty
if (readAllMessages)
while (serialThread.InputQueueCount() > 0)
ReadSerialMessageToMessageListener();
else
ReadSerialMessageToMessageListener();
}

// ------------------------------------------------------------------------
// Polls messages from the queue that the SerialThread object keeps. Once a
// message has been polled it is removed from the queue. There are some
// special messages that mark the start/end of the communication with the
// device.
// ------------------------------------------------------------------------
private void ReadSerialMessageToMessageListener()
{
// Read the next message from the queue
string message = (string) serialThread.ReadMessage();
if (message == null)
Expand Down
12 changes: 12 additions & 0 deletions UnityProject/Assets/Ardity/Scripts/Threads/AbstractSerialThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public object ReadMessage()
return inputQueue.Dequeue();
}

// ------------------------------------------------------------------------
// Returns number of messages in the input queue.
// It returns 0 if queue is not initialized.
// ------------------------------------------------------------------------
public int InputQueueCount()
{
if (inputQueue == null)
return 0;

return inputQueue.Count;
}

// ------------------------------------------------------------------------
// Schedules a message to be sent. It writes the message to the
// output queue, later the method 'RunOnce' reads this queue and sends
Expand Down

0 comments on commit 5e1534d

Please sign in to comment.