| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,13 +24,16 @@ | |||
| 24 | 24 | */ | |
| 25 | 25 | ||
| 26 | 26 | package processing.serial; | |
| 27 | + | ||
| 27 | 28 | import processing.core.*; | |
| 29 | + | ||
| 28 | 30 | import java.lang.reflect.*; | |
| 29 | 31 | import java.util.Map; | |
| 32 | + | ||
| 30 | 33 | import jssc.*; | |
| 31 | 34 | ||
| 32 | - public class Serial implements SerialPortEventListener { | ||
| 33 | 35 | ||
| 36 | + public class Serial implements SerialPortEventListener { | ||
| 34 | 37 | PApplet parent; | |
| 35 | 38 | public SerialPort port; | |
| 36 | 39 | Method serialAvailableMethod; | |
@@ -50,22 +53,27 @@ public class Serial implements SerialPortEventListener { | |||
| 50 | 53 | // * state of the RING, RLSD line | |
| 51 | 54 | // * sending breaks | |
| 52 | 55 | ||
| 56 | + | ||
| 53 | 57 | public Serial(PApplet parent) { | |
| 54 | 58 | this(parent, "COM1", 9600, 'N', 8, 1); | |
| 55 | 59 | } | |
| 56 | 60 | ||
| 61 | + | ||
| 57 | 62 | public Serial(PApplet parent, int baudRate) { | |
| 58 | 63 | this(parent, "COM1", baudRate, 'N', 8, 1); | |
| 59 | 64 | } | |
| 60 | 65 | ||
| 66 | + | ||
| 61 | 67 | public Serial(PApplet parent, String portName) { | |
| 62 | 68 | this(parent, portName, 9600, 'N', 8, 1); | |
| 63 | 69 | } | |
| 64 | 70 | ||
| 71 | + | ||
| 65 | 72 | public Serial(PApplet parent, String portName, int baudRate) { | |
| 66 | 73 | this(parent, portName, baudRate, 'N', 8, 1); | |
| 67 | 74 | } | |
| 68 | 75 | ||
| 76 | + | ||
| 69 | 77 | public Serial(PApplet parent, String portName, int baudRate, char parity, int dataBits, float stopBits) { | |
| 70 | 78 | this.parent = parent; | |
| 71 | 79 | parent.registerMethod("dispose", this); | |
@@ -101,7 +109,7 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da | |||
| 101 | 109 | port.addEventListener(this, SerialPort.MASK_RXCHAR); | |
| 102 | 110 | } catch (SerialPortException e) { | |
| 103 | 111 | // this used to be a RuntimeException before, so stick with it | |
| 104 | - throw new RuntimeException("Error opening serial port "+e.getPortName()+": "+e.getExceptionType()); | ||
| 112 | + throw new RuntimeException("Error opening serial port " + e.getPortName() + ": " + e.getExceptionType()); | ||
| 105 | 113 | } | |
| 106 | 114 | ||
| 107 | 115 | try { | |
@@ -115,10 +123,12 @@ public Serial(PApplet parent, String portName, int baudRate, char parity, int da | |||
| 115 | 123 | } | |
| 116 | 124 | } | |
| 117 | 125 | ||
| 126 | + | ||
| 118 | 127 | public void dispose() { | |
| 119 | 128 | stop(); | |
| 120 | 129 | } | |
| 121 | 130 | ||
| 131 | + | ||
| 122 | 132 | public void pre() { | |
| 123 | 133 | if (serialAvailableMethod != null && invokeSerialAvailable) { | |
| 124 | 134 | invokeSerialAvailable = false; | |
@@ -137,43 +147,51 @@ public int available() { | |||
| 137 | 147 | return (inBuffer-readOffset); | |
| 138 | 148 | } | |
| 139 | 149 | ||
| 150 | + | ||
| 140 | 151 | public void buffer(int size) { | |
| 141 | 152 | bufferUntilSize = size; | |
| 142 | 153 | } | |
| 143 | 154 | ||
| 155 | + | ||
| 144 | 156 | public void bufferUntil(int inByte) { | |
| 145 | 157 | bufferUntilSize = 0; | |
| 146 | 158 | bufferUntilByte = (byte)inByte; | |
| 147 | 159 | } | |
| 148 | 160 | ||
| 161 | + | ||
| 149 | 162 | public void clear() { | |
| 150 | 163 | synchronized (buffer) { | |
| 151 | 164 | inBuffer = 0; | |
| 152 | 165 | readOffset = 0; | |
| 153 | 166 | } | |
| 154 | 167 | } | |
| 155 | 168 | ||
| 169 | + | ||
| 156 | 170 | public boolean getCTS() { | |
| 157 | 171 | try { | |
| 158 | 172 | return port.isCTS(); | |
| 159 | 173 | } catch (SerialPortException e) { | |
| 160 | - throw new RuntimeException("Error reading the CTS line: "+e.getExceptionType()); | ||
| 174 | + throw new RuntimeException("Error reading the CTS line: " + e.getExceptionType()); | ||
| 161 | 175 | } | |
| 162 | 176 | } | |
| 163 | 177 | ||
| 178 | + | ||
| 164 | 179 | public boolean getDSR() { | |
| 165 | 180 | try { | |
| 166 | 181 | return port.isDSR(); | |
| 167 | 182 | } catch (SerialPortException e) { | |
| 168 | - throw new RuntimeException("Error reading the DSR line: "+e.getExceptionType()); | ||
| 183 | + throw new RuntimeException("Error reading the DSR line: " + e.getExceptionType()); | ||
| 169 | 184 | } | |
| 170 | 185 | } | |
| 171 | 186 | ||
| 187 | + | ||
| 172 | 188 | public static Map<String, String> getProperties(String portName) { | |
| 173 | - SerialPortList list = new SerialPortList(); | ||
| 174 | - return list.getPortProperties(portName); | ||
| 189 | + //SerialPortList list = new SerialPortList(); | ||
| 190 | + //return list.getPortProperties(portName); | ||
| 191 | + return SerialPortList.getPortProperties(portName); | ||
| 175 | 192 | } | |
| 176 | 193 | ||
| 194 | + | ||
| 177 | 195 | public int last() { | |
| 178 | 196 | if (inBuffer == readOffset) { | |
| 179 | 197 | return -1; | |
@@ -187,17 +205,21 @@ public int last() { | |||
| 187 | 205 | } | |
| 188 | 206 | } | |
| 189 | 207 | ||
| 208 | + | ||
| 190 | 209 | public char lastChar() { | |
| 191 | 210 | return (char)last(); | |
| 192 | 211 | } | |
| 193 | 212 | ||
| 213 | + | ||
| 194 | 214 | public static String[] list() { | |
| 195 | - SerialPortList list = new SerialPortList(); | ||
| 196 | 215 | // returns list sorted alphabetically, thus cu.* comes before tty.* | |
| 197 | 216 | // this was different with RXTX | |
| 198 | - return list.getPortNames(); | ||
| 217 | + //SerialPortList list = new SerialPortList(); | ||
| 218 | + //return list.getPortNames(); | ||
| 219 | + return SerialPortList.getPortNames(); | ||
| 199 | 220 | } | |
| 200 | 221 | ||
| 222 | + | ||
| 201 | 223 | public int read() { | |
| 202 | 224 | if (inBuffer == readOffset) { | |
| 203 | 225 | return -1; | |
@@ -213,6 +235,7 @@ public int read() { | |||
| 213 | 235 | } | |
| 214 | 236 | } | |
| 215 | 237 | ||
| 238 | + | ||
| 216 | 239 | public byte[] readBytes() { | |
| 217 | 240 | if (inBuffer == readOffset) { | |
| 218 | 241 | return null; | |
@@ -226,6 +249,7 @@ public byte[] readBytes() { | |||
| 226 | 249 | return ret; | |
| 227 | 250 | } | |
| 228 | 251 | } | |
| 252 | + | ||
| 229 | 253 | ||
| 230 | 254 | public int readBytes(byte[] dest) { | |
| 231 | 255 | if (inBuffer == readOffset) { | |
@@ -246,6 +270,7 @@ public int readBytes(byte[] dest) { | |||
| 246 | 270 | return toCopy; | |
| 247 | 271 | } | |
| 248 | 272 | } | |
| 273 | + | ||
| 249 | 274 | ||
| 250 | 275 | public byte[] readBytesUntil(int inByte) { | |
| 251 | 276 | if (inBuffer == readOffset) { | |
@@ -277,6 +302,7 @@ public byte[] readBytesUntil(int inByte) { | |||
| 277 | 302 | } | |
| 278 | 303 | } | |
| 279 | 304 | ||
| 305 | + | ||
| 280 | 306 | public int readBytesUntil(int inByte, byte[] dest) { | |
| 281 | 307 | if (inBuffer == readOffset) { | |
| 282 | 308 | return 0; | |
@@ -313,18 +339,20 @@ public int readBytesUntil(int inByte, byte[] dest) { | |||
| 313 | 339 | } | |
| 314 | 340 | } | |
| 315 | 341 | ||
| 342 | + | ||
| 316 | 343 | public char readChar() { | |
| 317 | - return (char)read(); | ||
| 344 | + return (char) read(); | ||
| 318 | 345 | } | |
| 319 | 346 | ||
| 347 | + | ||
| 320 | 348 | public String readString() { | |
| 321 | 349 | if (inBuffer == readOffset) { | |
| 322 | 350 | return null; | |
| 323 | 351 | } | |
| 324 | - | ||
| 325 | 352 | return new String(readBytes()); | |
| 326 | 353 | } | |
| 327 | 354 | ||
| 355 | + | ||
| 328 | 356 | public void serialEvent(SerialPortEvent event) { | |
| 329 | 357 | if (event.getEventType() == SerialPortEvent.RXCHAR) { | |
| 330 | 358 | int toRead; | |
@@ -365,61 +393,68 @@ public void serialEvent(SerialPortEvent event) { | |||
| 365 | 393 | } | |
| 366 | 394 | } | |
| 367 | 395 | } catch (SerialPortException e) { | |
| 368 | - throw new RuntimeException("Error reading from serial port "+e.getPortName()+": "+e.getExceptionType()); | ||
| 396 | + throw new RuntimeException("Error reading from serial port " + e.getPortName() + ": " + e.getExceptionType()); | ||
| 369 | 397 | } | |
| 370 | 398 | } | |
| 371 | 399 | } | |
| 372 | 400 | ||
| 401 | + | ||
| 373 | 402 | public void setDTR(boolean state) { | |
| 374 | 403 | // there is no way to influence the behavior of the DTR line when opening the serial port | |
| 375 | 404 | // this means that at least on Linux and OS X, Arduino devices are always reset | |
| 376 | 405 | try { | |
| 377 | 406 | port.setDTR(state); | |
| 378 | 407 | } catch (SerialPortException e) { | |
| 379 | - throw new RuntimeException("Error setting the DTR line: "+e.getExceptionType()); | ||
| 408 | + throw new RuntimeException("Error setting the DTR line: " + e.getExceptionType()); | ||
| 380 | 409 | } | |
| 381 | 410 | } | |
| 382 | 411 | ||
| 412 | + | ||
| 383 | 413 | public void setRTS(boolean state) { | |
| 384 | 414 | try { | |
| 385 | 415 | port.setRTS(state); | |
| 386 | 416 | } catch (SerialPortException e) { | |
| 387 | - throw new RuntimeException("Error setting the RTS line: "+e.getExceptionType()); | ||
| 417 | + throw new RuntimeException("Error setting the RTS line: " + e.getExceptionType()); | ||
| 388 | 418 | } | |
| 389 | 419 | } | |
| 390 | 420 | ||
| 421 | + | ||
| 391 | 422 | public void stop() { | |
| 392 | 423 | try { | |
| 393 | 424 | port.closePort(); | |
| 394 | 425 | } catch (SerialPortException e) { | |
| 426 | + // ignored | ||
| 395 | 427 | } | |
| 396 | 428 | inBuffer = 0; | |
| 397 | 429 | readOffset = 0; | |
| 398 | 430 | } | |
| 399 | 431 | ||
| 432 | + | ||
| 400 | 433 | public void write(byte[] src) { | |
| 401 | 434 | try { | |
| 402 | 435 | // this might block if the serial device is not yet ready (esp. tty devices under OS X) | |
| 403 | 436 | port.writeBytes(src); | |
| 404 | 437 | // we used to call flush() here | |
| 405 | 438 | } catch (SerialPortException e) { | |
| 406 | - throw new RuntimeException("Error writing to serial port "+e.getPortName()+": "+e.getExceptionType()); | ||
| 439 | + throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | ||
| 407 | 440 | } | |
| 408 | 441 | } | |
| 409 | 442 | ||
| 443 | + | ||
| 410 | 444 | public void write(int src) { | |
| 411 | 445 | try { | |
| 412 | 446 | port.writeInt(src); | |
| 413 | 447 | } catch (SerialPortException e) { | |
| 414 | - throw new RuntimeException("Error writing to serial port "+e.getPortName()+": "+e.getExceptionType()); | ||
| 448 | + throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | ||
| 415 | 449 | } | |
| 416 | 450 | } | |
| 417 | 451 | ||
| 452 | + | ||
| 418 | 453 | public void write(String src) { | |
| 419 | 454 | try { | |
| 420 | 455 | port.writeString(src); | |
| 421 | 456 | } catch (SerialPortException e) { | |
| 422 | - throw new RuntimeException("Error writing to serial port "+e.getPortName()+": "+e.getExceptionType()); | ||
| 457 | + throw new RuntimeException("Error writing to serial port " + e.getPortName() + ": " + e.getExceptionType()); | ||
| 423 | 458 | } | |
| 424 | 459 | } | |
| 425 | 460 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,11 +7,10 @@ X https://github.com/processing/processing/issues/2088 | |||
| 7 | 7 | X looks like issue that was covered in 2.0.3 changes | |
| 8 | 8 | X non-compliant libraries cause crash on "Add Library" | |
| 9 | 9 | X https://github.com/processing/processing/issues/2026 | |
| 10 | - | ||
| 11 | - _ incorporate the new serial library | ||
| 12 | - _ https://github.com/processing/processing/pull/2093 | ||
| 13 | - _ moviemaker problem with tga files | ||
| 14 | - _ maybe same as the OS X problem? | ||
| 10 | + X Open new PDE maximized when current PDE is maximized | ||
| 11 | + X https://github.com/processing/processing/pull/2037 | ||
| 12 | + X incorporate the new serial library | ||
| 13 | + X https://github.com/processing/processing/pull/2093 | ||
| 15 | 14 | ||
| 16 | 15 | _ fix console font on Windows and Linux with 7u40 | |
| 17 | 16 | _ the message area text also looks ugly.. can we fix? | |
| Back | FazBrowse Home | New Git URL |
0 commit comments