|
25 | 25 | package processing.app;
|
26 | 26 | //import processing.core.*;
|
27 | 27 |
|
28 |
| -import processing.app.debug.MessageConsumer; |
29 | 28 | import static processing.app.I18n._;
|
30 |
| - |
31 | 29 | import gnu.io.*;
|
32 | 30 |
|
33 | 31 | import java.io.*;
|
@@ -55,15 +53,13 @@ public class Serial implements SerialPortEventListener {
|
55 | 53 |
|
56 | 54 | // read buffer and streams
|
57 | 55 |
|
58 |
| - InputStream input; |
| 56 | + InputStreamReader input; |
59 | 57 | OutputStream output;
|
60 | 58 |
|
61 | 59 | byte buffer[] = new byte[32768];
|
62 | 60 | int bufferIndex;
|
63 | 61 | int bufferLast;
|
64 | 62 |
|
65 |
| - MessageConsumer consumer; |
66 |
| - |
67 | 63 | public Serial(boolean monitor) throws SerialException {
|
68 | 64 | this(Preferences.get("serial.port"),
|
69 | 65 | Preferences.getInteger("serial.debug_rate"),
|
@@ -158,7 +154,7 @@ public Serial(String iname, int irate,
|
158 | 154 | if (portId.getName().equals(iname)) {
|
159 | 155 | //System.out.println("looking for "+iname);
|
160 | 156 | port = (SerialPort)portId.open("serial madness", 2000);
|
161 |
| - input = port.getInputStream(); |
| 157 | + input = new InputStreamReader(port.getInputStream()); |
162 | 158 | output = port.getOutputStream();
|
163 | 159 | port.setSerialPortParams(rate, databits, stopbits, parity);
|
164 | 160 | port.addEventListener(this);
|
@@ -237,62 +233,41 @@ public void dispose() {
|
237 | 233 | port = null;
|
238 | 234 | }
|
239 | 235 |
|
| 236 | + char serialBuffer[] = new char[4096]; |
240 | 237 |
|
241 |
| - public void addListener(MessageConsumer consumer) { |
242 |
| - this.consumer = consumer; |
243 |
| - } |
244 |
| - |
245 |
| - |
246 | 238 | synchronized public void serialEvent(SerialPortEvent serialEvent) {
|
247 |
| - //System.out.println("serial port event"); // " + serialEvent); |
248 |
| - //System.out.flush(); |
249 |
| - //System.out.println("into"); |
250 |
| - //System.out.flush(); |
251 |
| - //System.err.println("type " + serialEvent.getEventType()); |
252 |
| - //System.err.println("ahoooyey"); |
253 |
| - //System.err.println("ahoooyeysdfsdfsdf"); |
254 | 239 | if (serialEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
|
255 |
| - //System.out.println("data available"); |
256 |
| - //System.err.flush(); |
257 | 240 | try {
|
258 |
| - while (input.available() > 0) { |
259 |
| - //if (input.available() > 0) { |
260 |
| - //serial = input.read(); |
261 |
| - //serialEvent(); |
262 |
| - //buffer[bufferCount++] = (byte) serial; |
| 241 | + while (input.ready()) { |
263 | 242 | synchronized (buffer) {
|
264 | 243 | if (bufferLast == buffer.length) {
|
265 | 244 | byte temp[] = new byte[bufferLast << 1];
|
266 | 245 | System.arraycopy(buffer, 0, temp, 0, bufferLast);
|
267 | 246 | buffer = temp;
|
268 | 247 | }
|
269 |
| - //buffer[bufferLast++] = (byte) input.read(); |
270 |
| - if(monitor == true) |
271 |
| - System.out.print((char) input.read()); |
272 |
| - if (this.consumer != null) |
273 |
| - this.consumer.message("" + (char) input.read()); |
274 |
| - |
275 |
| - /* |
276 |
| - System.err.println(input.available() + " " + |
277 |
| - ((char) buffer[bufferLast-1])); |
278 |
| - */ //} |
| 248 | + int n = input.read(serialBuffer); |
| 249 | + message(serialBuffer, n); |
279 | 250 | }
|
280 | 251 | }
|
281 |
| - //System.out.println("no more"); |
282 |
| - |
283 | 252 | } catch (IOException e) {
|
284 | 253 | errorMessage("serialEvent", e);
|
285 |
| - //e.printStackTrace(); |
286 |
| - //System.out.println("angry"); |
287 | 254 | }
|
288 | 255 | catch (Exception e) {
|
289 | 256 | }
|
290 | 257 | }
|
291 |
| - //System.out.println("out of"); |
292 |
| - //System.err.println("out of event " + serialEvent.getEventType()); |
293 | 258 | }
|
294 | 259 |
|
295 | 260 |
|
| 261 | + /** |
| 262 | + * This method is intended to be redefined by users of Serial class |
| 263 | + * |
| 264 | + * @param buff |
| 265 | + * @param n |
| 266 | + */ |
| 267 | + protected void message(char buff[], int n) { |
| 268 | + // Empty |
| 269 | + } |
| 270 | + |
296 | 271 | /**
|
297 | 272 | * Returns the number of bytes that have been read from serial
|
298 | 273 | * and are waiting to be dealt with by the user.
|
|
0 commit comments