| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,26 +65,40 @@ public class Serial implements SerialPortEventListener { | |||
| 65 | 65 | // * state of the RING, RLSD line | |
| 66 | 66 | // * sending breaks | |
| 67 | 67 | ||
| 68 | + /** | ||
| 69 | + * @param parent typically use "this" | ||
| 70 | + */ | ||
| 68 | 71 | public Serial(PApplet parent) { | |
| 69 | 72 | this(parent, "COM1", 9600, 'N', 8, 1); | |
| 70 | 73 | } | |
| 71 | 74 | ||
| 72 | - | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * @param baudRate 9600 is the default | ||
| 78 | + */ | ||
| 73 | 79 | public Serial(PApplet parent, int baudRate) { | |
| 74 | 80 | this(parent, "COM1", baudRate, 'N', 8, 1); | |
| 75 | 81 | } | |
| 76 | 82 | ||
| 77 | - | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * @param portName name of the port (COM1 is the default) | ||
| 86 | + */ | ||
| 78 | 87 | public Serial(PApplet parent, String portName) { | |
| 79 | 88 | this(parent, portName, 9600, 'N', 8, 1); | |
| 80 | 89 | } | |
| 81 | 90 | ||
| 82 | - | ||
| 91 | + | ||
| 83 | 92 | public Serial(PApplet parent, String portName, int baudRate) { | |
| 84 | 93 | this(parent, portName, baudRate, 'N', 8, 1); | |
| 85 | 94 | } | |
| 86 | 95 | ||
| 87 | - | ||
| 96 | + | ||
| 97 | + /** | ||
| 98 | + * @param parity 'N' for none, 'E' for even, 'O' for odd, 'M' for mark, 'S' for space ('N' is the default) | ||
| 99 | + * @param dataBits 8 is the default | ||
| 100 | + * @param stopBits 1.0, 1.5, or 2.0 (1.0 is the default) | ||
| 101 | + */ | ||
| 88 | 102 | public Serial(PApplet parent, String portName, int baudRate, char parity, int dataBits, float stopBits) { | |
| 89 | 103 | this.parent = parent; | |
| 90 | 104 | parent.registerMethod("dispose", this); | |
@@ -140,12 +154,15 @@ private Method findCallback(final String name) { | |||
| 140 | 154 | return null; | |
| 141 | 155 | } | |
| 142 | 156 | ||
| 143 | - | ||
| 157 | + | ||
| 158 | + /** | ||
| 159 | + * Used by PApplet to shut things down. | ||
| 160 | + */ | ||
| 144 | 161 | public void dispose() { | |
| 145 | 162 | stop(); | |
| 146 | 163 | } | |
| 147 | 164 | ||
| 148 | - | ||
| 165 | + | ||
| 149 | 166 | /** | |
| 150 | 167 | * Return true if this port is still active and hasn't run | |
| 151 | 168 | * into any trouble. | |
@@ -169,26 +186,44 @@ public void pre() { | |||
| 169 | 186 | } | |
| 170 | 187 | ||
| 171 | 188 | ||
| 189 | + /** | ||
| 190 | + * @generate Serial_available.xml | ||
| 191 | + * @webref serial:serial | ||
| 192 | + * @usage web_application | ||
| 193 | + */ | ||
| 172 | 194 | public int available() { | |
| 173 | 195 | return (inBuffer-readOffset); | |
| 174 | 196 | } | |
| 175 | 197 | ||
| 176 | - | ||
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * @generate Serial_buffer.xml | ||
| 201 | + * @webref serial:serial | ||
| 202 | + * @usage web_application | ||
| 203 | + * @param size number of bytes to buffer | ||
| 204 | + */ | ||
| 177 | 205 | public void buffer(int size) { | |
| 178 | 206 | bufferUntilSize = size; | |
| 179 | 207 | } | |
| 180 | 208 | ||
| 209 | + | ||
| 181 | 210 | /** | |
| 182 | - * @webref | ||
| 183 | - * @brief | ||
| 184 | - * @param inByte byte to buffer until it's reached | ||
| 211 | + * @generate Serial_bufferUntil.xml | ||
| 212 | + * @webref serial:serial | ||
| 213 | + * @usage web_application | ||
| 214 | + * @param inByte the value to buffer until | ||
| 185 | 215 | */ | |
| 186 | 216 | public void bufferUntil(int inByte) { | |
| 187 | 217 | bufferUntilSize = 0; | |
| 188 | 218 | bufferUntilByte = (byte)inByte; | |
| 189 | 219 | } | |
| 190 | 220 | ||
| 191 | - | ||
| 221 | + | ||
| 222 | + /** | ||
| 223 | + * @generate Serial_clear.xml | ||
| 224 | + * @webref serial:serial | ||
| 225 | + * @usage web_application | ||
| 226 | + */ | ||
| 192 | 227 | public void clear() { | |
| 193 | 228 | synchronized (buffer) { | |
| 194 | 229 | inBuffer = 0; | |
@@ -205,7 +240,7 @@ public boolean getCTS() { | |||
| 205 | 240 | } | |
| 206 | 241 | } | |
| 207 | 242 | ||
| 208 | - | ||
| 243 | + | ||
| 209 | 244 | public boolean getDSR() { | |
| 210 | 245 | try { | |
| 211 | 246 | return port.isDSR(); | |
@@ -214,12 +249,21 @@ public boolean getDSR() { | |||
| 214 | 249 | } | |
| 215 | 250 | } | |
| 216 | 251 | ||
| 217 | - | ||
| 252 | + | ||
| 218 | 253 | public static Map<String, String> getProperties(String portName) { | |
| 219 | 254 | return SerialPortList.getPortProperties(portName); | |
| 220 | 255 | } | |
| 221 | 256 | ||
| 222 | 257 | ||
| 258 | + /** | ||
| 259 | + * @generate Serial_last.xml | ||
| 260 | + * <h3>Advanced</h3> | ||
| 261 | + * Same as read() but returns the very last value received | ||
| 262 | + * and clears the buffer. Useful when you just want the most | ||
| 263 | + * recent value sent over the port. | ||
| 264 | + * @webref serial:serial | ||
| 265 | + * @usage web_application | ||
| 266 | + */ | ||
| 223 | 267 | public int last() { | |
| 224 | 268 | if (inBuffer == readOffset) { | |
| 225 | 269 | return -1; | |
@@ -233,19 +277,34 @@ public int last() { | |||
| 233 | 277 | } | |
| 234 | 278 | } | |
| 235 | 279 | ||
| 236 | - | ||
| 280 | + | ||
| 281 | + /** | ||
| 282 | + * @generate Serial_lastChar.xml | ||
| 283 | + * @webref serial:serial | ||
| 284 | + * @usage web_application | ||
| 285 | + */ | ||
| 237 | 286 | public char lastChar() { | |
| 238 | 287 | return (char)last(); | |
| 239 | 288 | } | |
| 240 | 289 | ||
| 241 | - | ||
| 290 | + | ||
| 291 | + /** | ||
| 292 | + * @generate Serial_list.xml | ||
| 293 | + * @webref serial | ||
| 294 | + * @usage web_application | ||
| 295 | + */ | ||
| 242 | 296 | public static String[] list() { | |
| 243 | 297 | // returns list sorted alphabetically, thus cu.* comes before tty.* | |
| 244 | 298 | // this was different with RXTX | |
| 245 | 299 | return SerialPortList.getPortNames(); | |
| 246 | 300 | } | |
| 247 | 301 | ||
| 248 | - | ||
| 302 | + | ||
| 303 | + /** | ||
| 304 | + * @generate Serial_read.xml | ||
| 305 | + * @webref serial:serial | ||
| 306 | + * @usage web_application | ||
| 307 | + */ | ||
| 249 | 308 | public int read() { | |
| 250 | 309 | if (inBuffer == readOffset) { | |
| 251 | 310 | return -1; | |
@@ -261,7 +320,12 @@ public int read() { | |||
| 261 | 320 | } | |
| 262 | 321 | } | |
| 263 | 322 | ||
| 264 | - | ||
| 323 | + | ||
| 324 | + /** | ||
| 325 | + * @generate Serial_readBytes.xml | ||
| 326 | + * @webref serial:serial | ||
| 327 | + * @usage web_application | ||
| 328 | + */ | ||
| 265 | 329 | public byte[] readBytes() { | |
| 266 | 330 | if (inBuffer == readOffset) { | |
| 267 | 331 | return null; | |
@@ -277,6 +341,16 @@ public byte[] readBytes() { | |||
| 277 | 341 | } | |
| 278 | 342 | ||
| 279 | 343 | ||
| 344 | + /** | ||
| 345 | + * <h3>Advanced</h3> | ||
| 346 | + * Grab whatever is in the serial buffer, and stuff it into a | ||
| 347 | + * byte buffer passed in by the user. This is more memory/time | ||
| 348 | + * efficient than readBytes() returning a byte[] array. | ||
| 349 | + * | ||
| 350 | + * Returns an int for how many bytes were read. If more bytes | ||
| 351 | + * are available than can fit into the byte array, only those | ||
| 352 | + * that will fit are read. | ||
| 353 | + */ | ||
| 280 | 354 | public int readBytes(byte[] dest) { | |
| 281 | 355 | if (inBuffer == readOffset) { | |
| 282 | 356 | return 0; | |
@@ -298,6 +372,12 @@ public int readBytes(byte[] dest) { | |||
| 298 | 372 | } | |
| 299 | 373 | ||
| 300 | 374 | ||
| 375 | + /** | ||
| 376 | + * @generate Serial_readBytesUntil.xml | ||
| 377 | + * @webref serial:serial | ||
| 378 | + * @usage web_application | ||
| 379 | + * @param inByte character designated to mark the end of the data | ||
| 380 | + */ | ||
| 301 | 381 | public byte[] readBytesUntil(int inByte) { | |
| 302 | 382 | if (inBuffer == readOffset) { | |
| 303 | 383 | return null; | |
@@ -328,7 +408,15 @@ public byte[] readBytesUntil(int inByte) { | |||
| 328 | 408 | } | |
| 329 | 409 | } | |
| 330 | 410 | ||
| 331 | - | ||
| 411 | + | ||
| 412 | + /** | ||
| 413 | + * <h3>Advanced</h3> | ||
| 414 | + * If dest[] is not big enough, then -1 is returned, | ||
| 415 | + * and an error message is printed on the console. | ||
| 416 | + * If nothing is in the buffer, zero is returned. | ||
| 417 | + * If 'interesting' byte is not in the buffer, then 0 is returned. | ||
| 418 | + * @param dest passed in byte array to be altered | ||
| 419 | + */ | ||
| 332 | 420 | public int readBytesUntil(int inByte, byte[] dest) { | |
| 333 | 421 | if (inBuffer == readOffset) { | |
| 334 | 422 | return 0; | |
@@ -365,20 +453,41 @@ public int readBytesUntil(int inByte, byte[] dest) { | |||
| 365 | 453 | } | |
| 366 | 454 | } | |
| 367 | 455 | ||
| 368 | - | ||
| 456 | + | ||
| 457 | + /** | ||
| 458 | + * @generate Serial_readChar.xml | ||
| 459 | + * @webref serial:serial | ||
| 460 | + * @usage web_application | ||
| 461 | + */ | ||
| 369 | 462 | public char readChar() { | |
| 370 | 463 | return (char) read(); | |
| 371 | 464 | } | |
| 372 | 465 | ||
| 373 | - | ||
| 466 | + | ||
| 467 | + /** | ||
| 468 | + * @generate Serial_readString.xml | ||
| 469 | + * @webref serial:serial | ||
| 470 | + * @usage web_application | ||
| 471 | + */ | ||
| 374 | 472 | public String readString() { | |
| 375 | 473 | if (inBuffer == readOffset) { | |
| 376 | 474 | return null; | |
| 377 | 475 | } | |
| 378 | 476 | return new String(readBytes()); | |
| 379 | 477 | } | |
| 380 | 478 | ||
| 381 | - | ||
| 479 | + | ||
| 480 | + /** | ||
| 481 | + * @generate Serial_readStringUntil.xml | ||
| 482 | + *<h3>Advanced</h3> | ||
| 483 | + * If you want to move Unicode data, you can first convert the | ||
| 484 | + * String to a byte stream in the representation of your choice | ||
| 485 | + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. | ||
| 486 | + * | ||
| 487 | + * @webref serial:serial | ||
| 488 | + * @usage web_application | ||
| 489 | + * @param inByte character designated to mark the end of the data | ||
| 490 | + */ | ||
| 382 | 491 | public String readStringUntil(int inByte) { | |
| 383 | 492 | byte temp[] = readBytesUntil(inByte); | |
| 384 | 493 | if (temp == null) { | |
@@ -389,6 +498,12 @@ public String readStringUntil(int inByte) { | |||
| 389 | 498 | } | |
| 390 | 499 | ||
| 391 | 500 | ||
| 501 | + /** | ||
| 502 | + * @generate serialEvent.xml | ||
| 503 | + * @webref serial:events | ||
| 504 | + * @usage web_application | ||
| 505 | + * @param serialPorttEvent the port where new data is available | ||
| 506 | + */ | ||
| 392 | 507 | public void serialEvent(SerialPortEvent event) { | |
| 393 | 508 | if (event.getEventType() == SerialPortEvent.RXCHAR) { | |
| 394 | 509 | int toRead; | |
@@ -438,7 +553,10 @@ public void serialEvent(SerialPortEvent event) { | |||
| 438 | 553 | } | |
| 439 | 554 | } | |
| 440 | 555 | ||
| 441 | - | ||
| 556 | + | ||
| 557 | + /** | ||
| 558 | + * Set the DTR line | ||
| 559 | + */ | ||
| 442 | 560 | public void setDTR(boolean state) { | |
| 443 | 561 | // there is no way to influence the behavior of the DTR line when opening the serial port | |
| 444 | 562 | // this means that at least on Linux and OS X, Arduino devices are always reset | |
@@ -449,7 +567,10 @@ public void setDTR(boolean state) { | |||
| 449 | 567 | } | |
| 450 | 568 | } | |
| 451 | 569 | ||
| 452 | - | ||
| 570 | + | ||
| 571 | + /** | ||
| 572 | + * Set the RTS line | ||
| 573 | + */ | ||
| 453 | 574 | public void setRTS(boolean state) { | |
| 454 | 575 | try { | |
| 455 | 576 | port.setRTS(state); | |
@@ -458,7 +579,12 @@ public void setRTS(boolean state) { | |||
| 458 | 579 | } | |
| 459 | 580 | } | |
| 460 | 581 | ||
| 461 | - | ||
| 582 | + | ||
| 583 | + /** | ||
| 584 | + * @generate Serial_stop.xml | ||
| 585 | + * @webref serial:serial | ||
| 586 | + * @usage web_application | ||
| 587 | + */ | ||
| 462 | 588 | public void stop() { | |
| 463 | 589 | try { | |
| 464 | 590 | port.closePort(); | |
@@ -469,7 +595,10 @@ public void stop() { | |||
| 469 | 595 | readOffset = 0; | |
| 470 | 596 | } | |
| 471 | 597 | ||
| 472 | - | ||
| 598 | + | ||
| 599 | + /** | ||
| 600 | + * @param src data to write | ||
| 601 | + */ | ||
| 473 | 602 | public void write(byte[] src) { | |
| 474 | 603 | try { | |
| 475 | 604 | // this might block if the serial device is not yet ready (esp. tty devices under OS X) | |
@@ -480,7 +609,12 @@ public void write(byte[] src) { | |||
| 480 | 609 | } | |
| 481 | 610 | } | |
| 482 | 611 | ||
| 483 | - | ||
| 612 | + | ||
| 613 | + /** | ||
| 614 | + * <h3>Advanced</h3> | ||
| 615 | + * This will handle both ints, bytes and chars transparently. | ||
| 616 | + * @param src data to write | ||
| 617 | + */ | ||
| 484 | 618 | public void write(int src) { | |
| 485 | 619 | try { | |
| 486 | 620 | port.writeInt(src); | |
@@ -489,7 +623,25 @@ public void write(int src) { | |||
| 489 | 623 | } | |
| 490 | 624 | } | |
| 491 | 625 | ||
| 492 | - | ||
| 626 | + | ||
| 627 | + /** | ||
| 628 | + * @generate Serial_write.xml | ||
| 629 | + * <h3>Advanced</h3> | ||
| 630 | + * Write a String to the output. Note that this doesn't account | ||
| 631 | + * for Unicode (two bytes per char), nor will it send UTF8 | ||
| 632 | + * characters.. It assumes that you mean to send a byte buffer | ||
| 633 | + * (most often the case for networking and serial i/o) and | ||
| 634 | + * will only use the bottom 8 bits of each char in the string. | ||
| 635 | + * (Meaning that internally it uses String.getBytes) | ||
| 636 | + * | ||
| 637 | + * If you want to move Unicode data, you can first convert the | ||
| 638 | + * String to a byte stream in the representation of your choice | ||
| 639 | + * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array. | ||
| 640 | + * | ||
| 641 | + * @webref serial:serial | ||
| 642 | + * @usage web_application | ||
| 643 | + * @param src data to write | ||
| 644 | + */ | ||
| 493 | 645 | public void write(String src) { | |
| 494 | 646 | try { | |
| 495 | 647 | port.writeString(src); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments