@@ -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 )
0 commit comments