@@ -48,28 +48,6 @@ public class Serial implements SerialPortEventListener {
4848
4949 volatile boolean invokeSerialAvailable = false ;
5050
51- // DEBUG
52- int baudRate = 0 ;
53- int countEvents = 0 ; // number of event handler invocations
54- int countFuncs = 0 ; // number of SerialEvent invocations
55- int countReads = 0 ; // number of read operations
56- int countSyncs = 0 ; // number of synchronizations
57- int countWrites = 0 ; // number of write operations
58- long firstEvent = 0 ; // timestamp of first event handler invocation
59- int maxBufferLength = 0 ; // maximum size of input buffer
60- long maxFuncTime = 0 ; // maximum time spend invoking SerialEvent
61- int maxRead = 0 ; // maximum number of bytes read
62- long maxReadTime = 0 ; // maximum time spent on read operations
63- long maxReadSyncTime = 0 ; // maximum time spent on a synchronization in the event handler
64- long maxSyncTime = 0 ; // maximum time spent on a synchronization elsewhere
65- long maxWriteTime = 0 ; // maximum time spent on a write operation
66- long sumFuncTime = 0 ; // sum of time spent in SerialEvent
67- int sumRead = 0 ; // sum of bytes read
68- long sumReadSyncTime = 0 ; // sum of time spent on synchonizations in the event handler
69- long sumReadTime = 0 ; // sum of time spent on read operations
70- long sumSyncTime = 0 ; // sum of time spent on synchonizations elsewhere
71- long sumWriteTime = 0 ; // sum of time spent on write operations
72-
7351 // Things we are currently not exposing:
7452 // * hardware flow control
7553 // * state of the RING, RLSD line
@@ -101,8 +79,6 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da
10179 parent .registerMethod ("dispose" , this );
10280 parent .registerMethod ("pre" , this );
10381
104- this .baudRate = baudRate ;
105-
10682 // setup parity
10783 if (parity == 'O' ) {
10884 parity = SerialPort .PARITY_ODD ;
@@ -184,37 +160,13 @@ public void bufferUntil(int inByte) {
184160
185161
186162 public void clear () {
187- long start = System .nanoTime ();
188163 synchronized (buffer ) {
189- long len = System .nanoTime ()-start ;
190- if (maxSyncTime < len ) {
191- maxSyncTime = len ;
192- }
193- sumSyncTime += len ;
194- countSyncs ++;
195164 inBuffer = 0 ;
196165 readOffset = 0 ;
197166 }
198167 }
199168
200169
201- public void debug () {
202- float secs = (System .nanoTime ()-firstEvent )/1000000000.0f ;
203- System .out .println ("\n Serial: test #4" );
204- System .out .println (port .getPortName ()+" @ " +baudRate +" bps" );
205- System .out .println (secs +" sec receiving data:" );
206- System .out .println (countEvents +" events, " +(countEvents /secs )+" per sec" );
207- System .out .println (countReads +" reads, " +(countReads /secs )+" per sec, " +(sumReadTime /(float )countReads )+" ns avg, " +maxReadTime +" ns max" );
208- System .out .println ((sumRead /(float )countReads )+" bytes avg per read, " +(sumRead /secs )+" per sec, " +maxRead +" bytes max" );
209- System .out .println ("Max buffer length: " +maxBufferLength );
210- System .out .println (sumReadSyncTime /(float )countReads +" ns avg read synchronizations, " +maxReadSyncTime +" ns max" );
211- System .out .println (countFuncs +" callbacks, " +(sumFuncTime /(float )countFuncs )+" ns avg, " +maxFuncTime +" ns max" );
212- System .out .println (countWrites +" writes, " +(sumWriteTime /(float )countWrites )+" ns avg, " +maxWriteTime +" ns max" );
213- System .out .println (countSyncs +" synchonizations, " +(sumSyncTime /(float )countSyncs )+" ns avg, " +maxSyncTime +" ns max" );
214- port .debug ();
215- }
216-
217-
218170 public boolean getCTS () {
219171 try {
220172 return port .isCTS ();
@@ -243,14 +195,7 @@ public int last() {
243195 return -1 ;
244196 }
245197
246- long start = System .nanoTime ();
247198 synchronized (buffer ) {
248- long len = System .nanoTime ()-start ;
249- if (maxSyncTime < len ) {
250- maxSyncTime = len ;
251- }
252- sumSyncTime += len ;
253- countSyncs ++;
254199 int ret = buffer [inBuffer -1 ] & 0xFF ;
255200 inBuffer = 0 ;
256201 readOffset = 0 ;
@@ -276,14 +221,7 @@ public int read() {
276221 return -1 ;
277222 }
278223
279- long start = System .nanoTime ();
280224 synchronized (buffer ) {
281- long len = System .nanoTime ()-start ;
282- if (maxSyncTime < len ) {
283- maxSyncTime = len ;
284- }
285- sumSyncTime += len ;
286- countSyncs ++;
287225 int ret = buffer [readOffset ++] & 0xFF ;
288226 if (inBuffer == readOffset ) {
289227 inBuffer = 0 ;
@@ -299,14 +237,7 @@ public byte[] readBytes() {
299237 return null ;
300238 }
301239
302- long start = System .nanoTime ();
303240 synchronized (buffer ) {
304- long len = System .nanoTime ()-start ;
305- if (maxSyncTime < len ) {
306- maxSyncTime = len ;
307- }
308- sumSyncTime += len ;
309- countSyncs ++;
310241 byte [] ret = new byte [inBuffer -readOffset ];
311242 System .arraycopy (buffer , readOffset , ret , 0 , ret .length );
312243 inBuffer = 0 ;
@@ -321,14 +252,7 @@ public int readBytes(byte[] dest) {
321252 return 0 ;
322253 }
323254
324- long start = System .nanoTime ();
325255 synchronized (buffer ) {
326- long len = System .nanoTime ()-start ;
327- if (maxSyncTime < len ) {
328- maxSyncTime = len ;
329- }
330- sumSyncTime += len ;
331- countSyncs ++;
332256 int toCopy = inBuffer -readOffset ;
333257 if (dest .length < toCopy ) {
334258 toCopy = dest .length ;
@@ -349,14 +273,7 @@ public byte[] readBytesUntil(int inByte) {
349273 return null ;
350274 }
351275
352- long start = System .nanoTime ();
353276 synchronized (buffer ) {
354- long len = System .nanoTime ()-start ;
355- if (maxSyncTime < len ) {
356- maxSyncTime = len ;
357- }
358- sumSyncTime += len ;
359- countSyncs ++;
360277 // look for needle in buffer
361278 int found = -1 ;
362279 for (int i =readOffset ; i < inBuffer ; i ++) {
@@ -387,14 +304,7 @@ public int readBytesUntil(int inByte, byte[] dest) {
387304 return 0 ;
388305 }
389306
390- long start = System .nanoTime ();
391307 synchronized (buffer ) {
392- long len = System .nanoTime ()-start ;
393- if (maxSyncTime < len ) {
394- maxSyncTime = len ;
395- }
396- sumSyncTime += len ;
397- countSyncs ++;
398308 // look for needle in buffer
399309 int found = -1 ;
400310 for (int i =readOffset ; i < inBuffer ; i ++) {
@@ -452,46 +362,22 @@ public String readStringUntil(int inByte) {
452362 public void serialEvent (SerialPortEvent event ) {
453363 if (event .getEventType () == SerialPortEvent .RXCHAR ) {
454364 int toRead ;
455- countEvents ++;
456- if (firstEvent == 0 ) {
457- firstEvent = System .nanoTime ();
458- }
459365 try {
460366 while (0 < (toRead = port .getInputBufferBytesCount ())) {
461- long start = System .nanoTime ();
462367 // this method can be called from the context of another thread
463368 synchronized (buffer ) {
464369 // read one byte at a time if the sketch is using serialEvent
465370 if (serialEventMethod != null ) {
466371 toRead = 1 ;
467372 }
468- long len = System .nanoTime ()-start ;
469- if (maxReadSyncTime < len ) {
470- maxReadSyncTime = len ;
471- }
472- sumReadSyncTime += len ;
473373 // enlarge buffer if necessary
474374 if (buffer .length < inBuffer +toRead ) {
475375 byte temp [] = new byte [buffer .length <<1 ];
476376 System .arraycopy (buffer , 0 , temp , 0 , inBuffer );
477377 buffer = temp ;
478378 }
479- if (maxBufferLength < buffer .length ) {
480- maxBufferLength = buffer .length ;
481- }
482379 // read an array of bytes and copy it into our buffer
483- start = System .nanoTime ();
484380 byte [] read = port .readBytes (toRead );
485- len = System .nanoTime ()-start ;
486- if (maxReadTime < len ) {
487- maxReadTime = len ;
488- }
489- sumReadTime += len ;
490- if (maxRead < read .length ) {
491- maxRead = read .length ;
492- }
493- sumRead += read .length ;
494- countReads ++;
495381 System .arraycopy (read , 0 , buffer , inBuffer , read .length );
496382 inBuffer += read .length ;
497383 }
@@ -506,14 +392,7 @@ public void serialEvent(SerialPortEvent event) {
506392 // available() and read() inside draw - but this function has no
507393 // thread-safety issues since it's being invoked during pre in the context
508394 // of the Processing applet
509- start = System .nanoTime ();
510395 serialEventMethod .invoke (parent , new Object [] { this });
511- long len = System .nanoTime ()-start ;
512- if (maxFuncTime < len ) {
513- maxFuncTime = len ;
514- }
515- sumFuncTime += len ;
516- countFuncs ++;
517396 } catch (Exception e ) {
518397 System .err .println ("Error, disabling serialEvent() for " +port .getPortName ());
519398 System .err .println (e .getLocalizedMessage ());
@@ -564,14 +443,7 @@ public void stop() {
564443 public void write (byte [] src ) {
565444 try {
566445 // this might block if the serial device is not yet ready (esp. tty devices under OS X)
567- long start = System .nanoTime ();
568446 port .writeBytes (src );
569- long len = System .nanoTime ()-start ;
570- if (maxWriteTime < len ) {
571- maxWriteTime = len ;
572- }
573- sumWriteTime += len ;
574- countWrites ++;
575447 // we used to call flush() here
576448 } catch (SerialPortException e ) {
577449 throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
@@ -581,14 +453,7 @@ public void write(byte[] src) {
581453
582454 public void write (int src ) {
583455 try {
584- long start = System .nanoTime ();
585456 port .writeInt (src );
586- long len = System .nanoTime ()-start ;
587- if (maxWriteTime < len ) {
588- maxWriteTime = len ;
589- }
590- sumWriteTime += len ;
591- countWrites ++;
592457 } catch (SerialPortException e ) {
593458 throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
594459 }
@@ -597,14 +462,7 @@ public void write(int src) {
597462
598463 public void write (String src ) {
599464 try {
600- long start = System .nanoTime ();
601465 port .writeString (src );
602- long len = System .nanoTime ()-start ;
603- if (maxWriteTime < len ) {
604- maxWriteTime = len ;
605- }
606- sumWriteTime += len ;
607- countWrites ++;
608466 } catch (SerialPortException e ) {
609467 throw new RuntimeException ("Error writing to serial port " + e .getPortName () + ": " + e .getExceptionType ());
610468 }
0 commit comments