1
- /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
-
3
1
/*
4
2
PSerial - class for serial port goodness
5
3
Part of the Processing project - http://processing.org
@@ -53,20 +51,6 @@ public class Serial implements SerialPortEventListener {
53
51
int parity ;
54
52
int databits ;
55
53
int stopbits ;
56
- boolean monitor = false ;
57
-
58
- byte buffer [] = new byte [32768 ];
59
- int bufferIndex ;
60
- int bufferLast ;
61
-
62
- public Serial (boolean monitor ) throws SerialException {
63
- this (PreferencesData .get ("serial.port" ),
64
- PreferencesData .getInteger ("serial.debug_rate" ),
65
- PreferencesData .get ("serial.parity" ).charAt (0 ),
66
- PreferencesData .getInteger ("serial.databits" ),
67
- new Float (PreferencesData .get ("serial.stopbits" )).floatValue ());
68
- this .monitor = monitor ;
69
- }
70
54
71
55
public Serial () throws SerialException {
72
56
this (PreferencesData .get ("serial.port" ),
@@ -170,15 +154,7 @@ public synchronized void serialEvent(SerialPortEvent serialEvent) {
170
154
try {
171
155
byte [] buf = port .readBytes (serialEvent .getEventValue ());
172
156
if (buf .length > 0 ) {
173
- if (bufferLast == buffer .length ) {
174
- byte temp [] = new byte [bufferLast << 1 ];
175
- System .arraycopy (buffer , 0 , temp , 0 , bufferLast );
176
- buffer = temp ;
177
- }
178
157
String msg = new String (buf );
179
- if (monitor ) {
180
- System .out .print (msg );
181
- }
182
158
char [] chars = msg .toCharArray ();
183
159
message (chars , chars .length );
184
160
}
@@ -199,200 +175,6 @@ protected void message(char[] chars, int length) {
199
175
// Empty
200
176
}
201
177
202
- /**
203
- * Returns the number of bytes that have been read from serial
204
- * and are waiting to be dealt with by the user.
205
- */
206
- public synchronized int available () {
207
- return (bufferLast - bufferIndex );
208
- }
209
-
210
-
211
- /**
212
- * Ignore all the bytes read so far and empty the buffer.
213
- */
214
- public synchronized void clear () {
215
- bufferLast = 0 ;
216
- bufferIndex = 0 ;
217
- }
218
-
219
-
220
- /**
221
- * Returns a number between 0 and 255 for the next byte that's
222
- * waiting in the buffer.
223
- * Returns -1 if there was no byte (although the user should
224
- * first check available() to see if things are ready to avoid this)
225
- */
226
- public synchronized int read () {
227
- if (bufferIndex == bufferLast ) return -1 ;
228
-
229
- int outgoing = buffer [bufferIndex ++] & 0xff ;
230
- if (bufferIndex == bufferLast ) { // rewind
231
- bufferIndex = 0 ;
232
- bufferLast = 0 ;
233
- }
234
- return outgoing ;
235
- }
236
-
237
-
238
- /**
239
- * Returns the next byte in the buffer as a char.
240
- * Returns -1, or 0xffff, if nothing is there.
241
- */
242
- public synchronized char readChar () {
243
- if (bufferIndex == bufferLast ) return (char ) (-1 );
244
- return (char ) read ();
245
- }
246
-
247
-
248
- /**
249
- * Return a byte array of anything that's in the serial buffer.
250
- * Not particularly memory/speed efficient, because it creates
251
- * a byte array on each read, but it's easier to use than
252
- * readBytes(byte b[]) (see below).
253
- */
254
- public synchronized byte [] readBytes () {
255
- if (bufferIndex == bufferLast ) return null ;
256
-
257
- int length = bufferLast - bufferIndex ;
258
- byte outgoing [] = new byte [length ];
259
- System .arraycopy (buffer , bufferIndex , outgoing , 0 , length );
260
-
261
- bufferIndex = 0 ; // rewind
262
- bufferLast = 0 ;
263
- return outgoing ;
264
- }
265
-
266
-
267
- /**
268
- * Grab whatever is in the serial buffer, and stuff it into a
269
- * byte buffer passed in by the user. This is more memory/time
270
- * efficient than readBytes() returning a byte[] array.
271
- * <p/>
272
- * Returns an int for how many bytes were read. If more bytes
273
- * are available than can fit into the byte array, only those
274
- * that will fit are read.
275
- */
276
- public synchronized int readBytes (byte outgoing []) {
277
- if (bufferIndex == bufferLast ) return 0 ;
278
-
279
- int length = bufferLast - bufferIndex ;
280
- if (length > outgoing .length ) length = outgoing .length ;
281
- System .arraycopy (buffer , bufferIndex , outgoing , 0 , length );
282
-
283
- bufferIndex += length ;
284
- if (bufferIndex == bufferLast ) {
285
- bufferIndex = 0 ; // rewind
286
- bufferLast = 0 ;
287
- }
288
- return length ;
289
- }
290
-
291
-
292
- /**
293
- * Reads from the serial port into a buffer of bytes up to and
294
- * including a particular character. If the character isn't in
295
- * the serial buffer, then 'null' is returned.
296
- */
297
- public synchronized byte [] readBytesUntil (int interesting ) {
298
- if (bufferIndex == bufferLast ) return null ;
299
- byte what = (byte ) interesting ;
300
-
301
- int found = -1 ;
302
- for (int k = bufferIndex ; k < bufferLast ; k ++) {
303
- if (buffer [k ] == what ) {
304
- found = k ;
305
- break ;
306
- }
307
- }
308
- if (found == -1 ) return null ;
309
-
310
- int length = found - bufferIndex + 1 ;
311
- byte outgoing [] = new byte [length ];
312
- System .arraycopy (buffer , bufferIndex , outgoing , 0 , length );
313
-
314
- bufferIndex = 0 ; // rewind
315
- bufferLast = 0 ;
316
- return outgoing ;
317
- }
318
-
319
-
320
- /**
321
- * Reads from the serial port into a buffer of bytes until a
322
- * particular character. If the character isn't in the serial
323
- * buffer, then 'null' is returned.
324
- * <p/>
325
- * If outgoing[] is not big enough, then -1 is returned,
326
- * and an error message is printed on the console.
327
- * If nothing is in the buffer, zero is returned.
328
- * If 'interesting' byte is not in the buffer, then 0 is returned.
329
- */
330
- public synchronized int readBytesUntil (int interesting , byte outgoing []) {
331
- if (bufferIndex == bufferLast ) return 0 ;
332
- byte what = (byte ) interesting ;
333
-
334
- int found = -1 ;
335
- for (int k = bufferIndex ; k < bufferLast ; k ++) {
336
- if (buffer [k ] == what ) {
337
- found = k ;
338
- break ;
339
- }
340
- }
341
- if (found == -1 ) return 0 ;
342
-
343
- int length = found - bufferIndex + 1 ;
344
- if (length > outgoing .length ) {
345
- System .err .println (
346
- I18n .format (
347
- _ ("readBytesUntil() byte buffer is too small for the {0}" +
348
- " bytes up to and including char {1}" ),
349
- length ,
350
- interesting
351
- )
352
- );
353
- return -1 ;
354
- }
355
- //byte outgoing[] = new byte[length];
356
- System .arraycopy (buffer , bufferIndex , outgoing , 0 , length );
357
-
358
- bufferIndex += length ;
359
- if (bufferIndex == bufferLast ) {
360
- bufferIndex = 0 ; // rewind
361
- bufferLast = 0 ;
362
- }
363
- return length ;
364
- }
365
-
366
-
367
- /**
368
- * Return whatever has been read from the serial port so far
369
- * as a String. It assumes that the incoming characters are ASCII.
370
- * <p/>
371
- * If you want to move Unicode data, you can first convert the
372
- * String to a byte stream in the representation of your choice
373
- * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
374
- */
375
- public synchronized String readString () {
376
- if (bufferIndex == bufferLast ) return null ;
377
- return new String (readBytes ());
378
- }
379
-
380
-
381
- /**
382
- * Combination of readBytesUntil and readString. See caveats in
383
- * each function. Returns null if it still hasn't found what
384
- * you're looking for.
385
- * <p/>
386
- * If you want to move Unicode data, you can first convert the
387
- * String to a byte stream in the representation of your choice
388
- * (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.
389
- */
390
- public synchronized String readStringUntil (int interesting ) {
391
- byte b [] = readBytesUntil (interesting );
392
- if (b == null ) return null ;
393
- return new String (b );
394
- }
395
-
396
178
397
179
/**
398
180
* This will handle both ints, bytes and chars transparently.
0 commit comments