2727import processing.serial.* ;
2828import java.util.Map ;
2929
30+ // FLAG FOR DEBUG MODE
31+ final boolean DEBUG = false ;
32+
3033// CONSTANTS
3134final char OUTER_KEY = ' #' ;
3235final String INNER_KEY = " @" ;
@@ -71,6 +74,11 @@ void setup()
7174 frameRate ( 100 );
7275
7376 // Serial comms
77+ while ( Serial . list(). length < 1 )
78+ {
79+ text ( " No serial ports available. Waiting..." , 20 , 20 );
80+ delay ( 100 );
81+ }
7482 portIndex = 0 ;
7583 lastPortSwitch = millis ();
7684 attemptConnect( portIndex );
@@ -92,15 +100,22 @@ void draw()
92100 }
93101 else
94102 {
95- text ( " Scanning serial ports... (Port " + portIndex + " )" , 20 , 20 );
96- // Continue to scan ports if not configuring
103+ // Continue to scan ports if not configuring
104+ text ( " Scanning serial ports... (" + Serial . list()[portIndex] + " )" , 20 , 20 );
105+
97106 if ( millis () - lastPortSwitch > PORT_INTERVAL )
98- { // Go to next port
107+ { // Go to next port
99108 portIndex++ ;
100109 if ( portIndex >= Serial . list(). length )
101110 {
102111 portIndex = 0 ;
103112 }
113+
114+ if ( DEBUG )
115+ {
116+ println ( " Trying next port... index: " + portIndex + " , name: " + Serial . list()[portIndex] );
117+ }
118+
104119 attemptConnect( portIndex );
105120 }
106121 }
@@ -126,6 +141,10 @@ void serialEvent( Serial ser )
126141 try
127142 {
128143 String message = ser. readStringUntil( OUTER_KEY );
144+ if ( message == null || message. isEmpty() || message. equals( " \n\n " + OUTER_KEY ) )
145+ {
146+ return ;
147+ }
129148 String [] arrayMain = message. split( " \n " );
130149
131150 // ********************************************************* //
@@ -137,11 +156,19 @@ void serialEvent( Serial ser )
137156 {
138157 String [] arraySub = arrayMain[0 ]. split( INNER_KEY );
139158 // Check for size of full transmission against expected to flag bad transmission
140- numGraphs = Integer . parseInt( arraySub[0 ] );
159+ try
160+ {
161+ numGraphs = Integer . parseInt( arraySub[0 ] );
162+ }
163+ catch ( Exception e )
164+ {
165+ return ;
166+ }
141167 if ( arrayMain. length != numGraphs + 1 )
142168 {
143- throw new Exception () ;
169+ return ;
144170 }
171+
145172 configured = false ;
146173 String concatLabels = " " ;
147174
@@ -169,6 +196,10 @@ void serialEvent( Serial ser )
169196 colorsTemp[j] = COLORMAP . get( arraySub[5 + 3 * j] );
170197 if ( colorsTemp[j] == 0 )
171198 {
199+ if ( DEBUG )
200+ {
201+ println ( " Invalid color: " + arraySub[5 + 3 * j] + " , defaulting to green." );
202+ }
172203 colorsTemp[j] = COLORMAP . get( " green" );
173204 }
174205 concatLabels += labelsTemp[j];
@@ -184,15 +215,20 @@ void serialEvent( Serial ser )
184215 xvyTemp, numVars, maxPoints, title, labelsTemp, colorsTemp );
185216 graphs. add( temp );
186217 }
187- println ( " Configured " + graphs. size() + " graphs" );
188218
189219 // Set new config code
190220 if ( concatLabels. equals( lastLabels ) ) // Only when we're sure on labels
191221 {
192222 configCode = arrayMain[0 ];
193223 lastConfig = millis ();
224+ println ( " Configured " + graphs. size() + " graphs" );
194225 }
195226 lastLabels = concatLabels;
227+
228+ if ( DEBUG )
229+ {
230+ println ( " Config code: " + configCode + " , Label config: " + concatLabels );
231+ }
196232 }
197233 else
198234 {
@@ -209,20 +245,24 @@ void serialEvent( Serial ser )
209245 String [] arraySub = arrayMain[i+ 1 ]. split( INNER_KEY );
210246
211247 double [] tempData = new double [ (arraySub. length - 5 ) / 3 ];
212-
248+
213249 // Update graph objects with new data
214250 int q = 0 ;
215251 for ( int j = 6 ; j < arraySub. length; j += 3 )
216252 {
217253 tempData[q] = Double . parseDouble( arraySub[j] );
218254 q++ ;
219255 }
220- graphs. get( i ). Update ( tempData, tempTime );
256+ graphs. get( i ). Update ( tempData, tempTime );
221257 }
222258 }
223259 }
224- catch (Exception e) {
225- // println("exception....");
260+ catch ( Exception e )
261+ {
262+ if ( DEBUG )
263+ {
264+ println ( " Exception... " + e. getMessage() );
265+ }
226266 }
227267}
228268
@@ -281,16 +321,22 @@ float[][] setupGraphPosition( int numGraphs )
281321void attemptConnect( int index )
282322{
283323 // Attempt connect on specified serial port
284- println ( " Attempting connect on port index: " + index );
324+ String portName = Serial . list()[portIndex];
325+ println ( " Attempting connect on port: " + portName );
285326 try
286327 {
287328 // Configure
288- port = new Serial ( this , Serial . list()[portIndex] , BAUD_RATE );
289- port . bufferUntil( OUTER_KEY );
329+ port = new Serial ( this , portName , BAUD_RATE );
330+
290331 lastPortSwitch = millis (); // at end so that we try again immediately on invalid port
332+ println ( " Connected on " + portName + " . Listening for configuration..." );
291333 }
292334 catch ( Exception e )
293335 {
294- println ( e. getMessage() );
336+ if ( DEBUG )
337+ {
338+ println ( e. getMessage() );
339+ }
340+ delay ( 100 );
295341 }
296342}
0 commit comments