27
27
import processing.serial.* ;
28
28
import java.util.Map ;
29
29
30
+ // FLAG FOR DEBUG MODE
31
+ final boolean DEBUG = false ;
32
+
30
33
// CONSTANTS
31
34
final char OUTER_KEY = ' #' ;
32
35
final String INNER_KEY = " @" ;
@@ -71,6 +74,11 @@ void setup()
71
74
frameRate ( 100 );
72
75
73
76
// Serial comms
77
+ while ( Serial . list(). length < 1 )
78
+ {
79
+ text ( " No serial ports available. Waiting..." , 20 , 20 );
80
+ delay ( 100 );
81
+ }
74
82
portIndex = 0 ;
75
83
lastPortSwitch = millis ();
76
84
attemptConnect( portIndex );
@@ -92,15 +100,22 @@ void draw()
92
100
}
93
101
else
94
102
{
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
+
97
106
if ( millis () - lastPortSwitch > PORT_INTERVAL )
98
- { // Go to next port
107
+ { // Go to next port
99
108
portIndex++ ;
100
109
if ( portIndex >= Serial . list(). length )
101
110
{
102
111
portIndex = 0 ;
103
112
}
113
+
114
+ if ( DEBUG )
115
+ {
116
+ println ( " Trying next port... index: " + portIndex + " , name: " + Serial . list()[portIndex] );
117
+ }
118
+
104
119
attemptConnect( portIndex );
105
120
}
106
121
}
@@ -126,6 +141,10 @@ void serialEvent( Serial ser )
126
141
try
127
142
{
128
143
String message = ser. readStringUntil( OUTER_KEY );
144
+ if ( message == null || message. isEmpty() || message. equals( " \n\n " + OUTER_KEY ) )
145
+ {
146
+ return ;
147
+ }
129
148
String [] arrayMain = message. split( " \n " );
130
149
131
150
// ********************************************************* //
@@ -137,11 +156,19 @@ void serialEvent( Serial ser )
137
156
{
138
157
String [] arraySub = arrayMain[0 ]. split( INNER_KEY );
139
158
// 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
+ }
141
167
if ( arrayMain. length != numGraphs + 1 )
142
168
{
143
- throw new Exception () ;
169
+ return ;
144
170
}
171
+
145
172
configured = false ;
146
173
String concatLabels = " " ;
147
174
@@ -169,6 +196,10 @@ void serialEvent( Serial ser )
169
196
colorsTemp[j] = COLORMAP . get( arraySub[5 + 3 * j] );
170
197
if ( colorsTemp[j] == 0 )
171
198
{
199
+ if ( DEBUG )
200
+ {
201
+ println ( " Invalid color: " + arraySub[5 + 3 * j] + " , defaulting to green." );
202
+ }
172
203
colorsTemp[j] = COLORMAP . get( " green" );
173
204
}
174
205
concatLabels += labelsTemp[j];
@@ -184,15 +215,20 @@ void serialEvent( Serial ser )
184
215
xvyTemp, numVars, maxPoints, title, labelsTemp, colorsTemp );
185
216
graphs. add( temp );
186
217
}
187
- println ( " Configured " + graphs. size() + " graphs" );
188
218
189
219
// Set new config code
190
220
if ( concatLabels. equals( lastLabels ) ) // Only when we're sure on labels
191
221
{
192
222
configCode = arrayMain[0 ];
193
223
lastConfig = millis ();
224
+ println ( " Configured " + graphs. size() + " graphs" );
194
225
}
195
226
lastLabels = concatLabels;
227
+
228
+ if ( DEBUG )
229
+ {
230
+ println ( " Config code: " + configCode + " , Label config: " + concatLabels );
231
+ }
196
232
}
197
233
else
198
234
{
@@ -209,20 +245,24 @@ void serialEvent( Serial ser )
209
245
String [] arraySub = arrayMain[i+ 1 ]. split( INNER_KEY );
210
246
211
247
double [] tempData = new double [ (arraySub. length - 5 ) / 3 ];
212
-
248
+
213
249
// Update graph objects with new data
214
250
int q = 0 ;
215
251
for ( int j = 6 ; j < arraySub. length; j += 3 )
216
252
{
217
253
tempData[q] = Double . parseDouble( arraySub[j] );
218
254
q++ ;
219
255
}
220
- graphs. get( i ). Update ( tempData, tempTime );
256
+ graphs. get( i ). Update ( tempData, tempTime );
221
257
}
222
258
}
223
259
}
224
- catch (Exception e) {
225
- // println("exception....");
260
+ catch ( Exception e )
261
+ {
262
+ if ( DEBUG )
263
+ {
264
+ println ( " Exception... " + e. getMessage() );
265
+ }
226
266
}
227
267
}
228
268
@@ -281,16 +321,22 @@ float[][] setupGraphPosition( int numGraphs )
281
321
void attemptConnect( int index )
282
322
{
283
323
// 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 );
285
326
try
286
327
{
287
328
// 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
+
290
331
lastPortSwitch = millis (); // at end so that we try again immediately on invalid port
332
+ println ( " Connected on " + portName + " . Listening for configuration..." );
291
333
}
292
334
catch ( Exception e )
293
335
{
294
- println ( e. getMessage() );
336
+ if ( DEBUG )
337
+ {
338
+ println ( e. getMessage() );
339
+ }
340
+ delay ( 100 );
295
341
}
296
342
}
0 commit comments