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