| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,28 +48,6 @@ public class Serial implements SerialPortEventListener { | |||
| 48 | 48 | ||
| 49 | 49 | volatile boolean invokeSerialAvailable = false; | |
| 50 | 50 | ||
| 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 | - | ||
| 73 | 51 | // Things we are currently not exposing: | |
| 74 | 52 | // * hardware flow control | |
| 75 | 53 | // * state of the RING, RLSD line | |
@@ -101,8 +79,6 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da | |||
| 101 | 79 | parent.registerMethod("dispose", this); | |
| 102 | 80 | parent.registerMethod("pre", this); | |
| 103 | 81 | ||
| 104 | - this.baudRate = baudRate; | ||
| 105 | - | ||
| 106 | 82 | // setup parity | |
| 107 | 83 | if (parity == 'O') { | |
| 108 | 84 | parity = SerialPort.PARITY_ODD; | |
@@ -184,37 +160,13 @@ public void bufferUntil(int inByte) { | |||
| 184 | 160 | ||
| 185 | 161 | ||
| 186 | 162 | public void clear() { | |
| 187 | - long start = System.nanoTime(); | ||
| 188 | 163 | synchronized (buffer) { | |
| 189 | - long len = System.nanoTime()-start; | ||
| 190 | - if (maxSyncTime < len) { | ||
| 191 | - maxSyncTime = len; | ||
| 192 | - } | ||
| 193 | - sumSyncTime += len; | ||
| 194 | - countSyncs++; | ||
| 195 | 164 | inBuffer = 0; | |
| 196 | 165 | readOffset = 0; | |
| 197 | 166 | } | |
| 198 | 167 | } | |
| 199 | 168 | ||
| 200 | 169 | ||
| 201 | - public void debug() { | ||
| 202 | - float secs = (System.nanoTime()-firstEvent)/1000000000.0f; | ||
| 203 | - System.out.println("\nSerial: 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 | - | ||
| 218 | 170 | public boolean getCTS() { | |
| 219 | 171 | try { | |
| 220 | 172 | return port.isCTS(); | |
@@ -243,14 +195,7 @@ public int last() { | |||
| 243 | 195 | return -1; | |
| 244 | 196 | } | |
| 245 | 197 | ||
| 246 | - long start = System.nanoTime(); | ||
| 247 | 198 | synchronized (buffer) { | |
| 248 | - long len = System.nanoTime()-start; | ||
| 249 | - if (maxSyncTime < len) { | ||
| 250 | - maxSyncTime = len; | ||
| 251 | - } | ||
| 252 | - sumSyncTime += len; | ||
| 253 | - countSyncs++; | ||
| 254 | 199 | int ret = buffer[inBuffer-1] & 0xFF; | |
| 255 | 200 | inBuffer = 0; | |
| 256 | 201 | readOffset = 0; | |
@@ -276,14 +221,7 @@ public int read() { | |||
| 276 | 221 | return -1; | |
| 277 | 222 | } | |
| 278 | 223 | ||
| 279 | - long start = System.nanoTime(); | ||
| 280 | 224 | synchronized (buffer) { | |
| 281 | - long len = System.nanoTime()-start; | ||
| 282 | - if (maxSyncTime < len) { | ||
| 283 | - maxSyncTime = len; | ||
| 284 | - } | ||
| 285 | - sumSyncTime += len; | ||
| 286 | - countSyncs++; | ||
| 287 | 225 | int ret = buffer[readOffset++] & 0xFF; | |
| 288 | 226 | if (inBuffer == readOffset) { | |
| 289 | 227 | inBuffer = 0; | |
@@ -299,14 +237,7 @@ public byte[] readBytes() { | |||
| 299 | 237 | return null; | |
| 300 | 238 | } | |
| 301 | 239 | ||
| 302 | - long start = System.nanoTime(); | ||
| 303 | 240 | synchronized (buffer) { | |
| 304 | - long len = System.nanoTime()-start; | ||
| 305 | - if (maxSyncTime < len) { | ||
| 306 | - maxSyncTime = len; | ||
| 307 | - } | ||
| 308 | - sumSyncTime += len; | ||
| 309 | - countSyncs++; | ||
| 310 | 241 | byte[] ret = new byte[inBuffer-readOffset]; | |
| 311 | 242 | System.arraycopy(buffer, readOffset, ret, 0, ret.length); | |
| 312 | 243 | inBuffer = 0; | |
@@ -321,14 +252,7 @@ public int readBytes(byte[] dest) { | |||
| 321 | 252 | return 0; | |
| 322 | 253 | } | |
| 323 | 254 | ||
| 324 | - long start = System.nanoTime(); | ||
| 325 | 255 | synchronized (buffer) { | |
| 326 | - long len = System.nanoTime()-start; | ||
| 327 | - if (maxSyncTime < len) { | ||
| 328 | - maxSyncTime = len; | ||
| 329 | - } | ||
| 330 | - sumSyncTime += len; | ||
| 331 | - countSyncs++; | ||
| 332 | 256 | int toCopy = inBuffer-readOffset; | |
| 333 | 257 | if (dest.length < toCopy) { | |
| 334 | 258 | toCopy = dest.length; | |
@@ -349,14 +273,7 @@ public byte[] readBytesUntil(int inByte) { | |||
| 349 | 273 | return null; | |
| 350 | 274 | } | |
| 351 | 275 | ||
| 352 | - long start = System.nanoTime(); | ||
| 353 | 276 | synchronized (buffer) { | |
| 354 | - long len = System.nanoTime()-start; | ||
| 355 | - if (maxSyncTime < len) { | ||
| 356 | - maxSyncTime = len; | ||
| 357 | - } | ||
| 358 | - sumSyncTime += len; | ||
| 359 | - countSyncs++; | ||
| 360 | 277 | // look for needle in buffer | |
| 361 | 278 | int found = -1; | |
| 362 | 279 | for (int i=readOffset; i < inBuffer; i++) { | |
@@ -387,14 +304,7 @@ public int readBytesUntil(int inByte, byte[] dest) { | |||
| 387 | 304 | return 0; | |
| 388 | 305 | } | |
| 389 | 306 | ||
| 390 | - long start = System.nanoTime(); | ||
| 391 | 307 | synchronized (buffer) { | |
| 392 | - long len = System.nanoTime()-start; | ||
| 393 | - if (maxSyncTime < len) { | ||
| 394 | - maxSyncTime = len; | ||
| 395 | - } | ||
| 396 | - sumSyncTime += len; | ||
| 397 | - countSyncs++; | ||
| 398 | 308 | // look for needle in buffer | |
| 399 | 309 | int found = -1; | |
| 400 | 310 | for (int i=readOffset; i < inBuffer; i++) { | |
@@ -452,46 +362,22 @@ public String readStringUntil(int inByte) { | |||
| 452 | 362 | public void serialEvent(SerialPortEvent event) { | |
| 453 | 363 | if (event.getEventType() == SerialPortEvent.RXCHAR) { | |
| 454 | 364 | int toRead; | |
| 455 | - countEvents++; | ||
| 456 | - if (firstEvent == 0) { | ||
| 457 | - firstEvent = System.nanoTime(); | ||
| 458 | - } | ||
| 459 | 365 | try { | |
| 460 | 366 | while (0 < (toRead = port.getInputBufferBytesCount())) { | |
| 461 | - long start = System.nanoTime(); | ||
| 462 | 367 | // this method can be called from the context of another thread | |
| 463 | 368 | synchronized (buffer) { | |
| 464 | 369 | // read one byte at a time if the sketch is using serialEvent | |
| 465 | 370 | if (serialEventMethod != null) { | |
| 466 | 371 | toRead = 1; | |
| 467 | 372 | } | |
| 468 | - long len = System.nanoTime()-start; | ||
| 469 | - if (maxReadSyncTime < len) { | ||
| 470 | - maxReadSyncTime = len; | ||
| 471 | - } | ||
| 472 | - sumReadSyncTime += len; | ||
| 473 | 373 | // enlarge buffer if necessary | |
| 474 | 374 | if (buffer.length < inBuffer+toRead) { | |
| 475 | 375 | byte temp[] = new byte[buffer.length<<1]; | |
| 476 | 376 | System.arraycopy(buffer, 0, temp, 0, inBuffer); | |
| 477 | 377 | buffer = temp; | |
| 478 | 378 | } | |
| 479 | - if (maxBufferLength < buffer.length) { | ||
| 480 | - maxBufferLength = buffer.length; | ||
| 481 | - } | ||
| 482 | 379 | // read an array of bytes and copy it into our buffer | |
| 483 | - start = System.nanoTime(); | ||
| 484 | 380 | 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++; | ||
| 495 | 381 | System.arraycopy(read, 0, buffer, inBuffer, read.length); | |
| 496 | 382 | inBuffer += read.length; | |
| 497 | 383 | } | |
@@ -506,14 +392,7 @@ public void serialEvent(SerialPortEvent event) { | |||
| 506 | 392 | // available() and read() inside draw - but this function has no | |
| 507 | 393 | // thread-safety issues since it's being invoked during pre in the context | |
| 508 | 394 | // of the Processing applet | |
| 509 | - start = System.nanoTime(); | ||
| 510 | 395 | 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++; | ||
| 517 | 396 | } catch (Exception e) { | |
| 518 | 397 | System.err.println("Error, disabling serialEvent() for "+port.getPortName()); | |
| 519 | 398 | System.err.println(e.getLocalizedMessage()); | |
@@ -564,14 +443,7 @@ public void stop() { | |||
| 564 | 443 | public void write(byte[] src) { | |
| 565 | 444 | try { | |
| 566 | 445 | // this might block if the serial device is not yet ready (esp. tty devices under OS X) | |
| 567 | - long start = System.nanoTime(); | ||
| 568 | 446 | port.writeBytes(src); | |
| 569 | - long len = System.nanoTime()-start; | ||
| 570 | - if (maxWriteTime < len) { | ||
| 571 | - maxWriteTime = len; | ||
| 572 | - } | ||
| 573 | - sumWriteTime += len; | ||
| 574 | - countWrites++; | ||
| 575 | 447 | // we used to call flush() here | |
| 576 | 448 | } catch (SerialPortException e) { | |
| 577 | 449 | throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | |
@@ -581,14 +453,7 @@ public void write(byte[] src) { | |||
| 581 | 453 | ||
| 582 | 454 | public void write(int src) { | |
| 583 | 455 | try { | |
| 584 | - long start = System.nanoTime(); | ||
| 585 | 456 | port.writeInt(src); | |
| 586 | - long len = System.nanoTime()-start; | ||
| 587 | - if (maxWriteTime < len) { | ||
| 588 | - maxWriteTime = len; | ||
| 589 | - } | ||
| 590 | - sumWriteTime += len; | ||
| 591 | - countWrites++; | ||
| 592 | 457 | } catch (SerialPortException e) { | |
| 593 | 458 | throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | |
| 594 | 459 | } | |
@@ -597,14 +462,7 @@ public void write(int src) { | |||
| 597 | 462 | ||
| 598 | 463 | public void write(String src) { | |
| 599 | 464 | try { | |
| 600 | - long start = System.nanoTime(); | ||
| 601 | 465 | port.writeString(src); | |
| 602 | - long len = System.nanoTime()-start; | ||
| 603 | - if (maxWriteTime < len) { | ||
| 604 | - maxWriteTime = len; | ||
| 605 | - } | ||
| 606 | - sumWriteTime += len; | ||
| 607 | - countWrites++; | ||
| 608 | 466 | } catch (SerialPortException e) { | |
| 609 | 467 | throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | |
| 610 | 468 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments