Skip to content

Commit e63001b

Browse files
author
jantje
committed
issue #241 (extra blank lines in serial monitor)
Actually I fixed quite some things in the serial monitor. The filter scope data is now far better (havn't seen any glitches).
1 parent 7c688e0 commit e63001b

File tree

4 files changed

+147
-351
lines changed

4 files changed

+147
-351
lines changed

it.baeyens.arduino.common/src/it/baeyens/arduino/arduino/Serial.java

Lines changed: 4 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ public static Vector<String> list() {
9999
int stopbits;
100100
boolean monitor = false;
101101

102-
byte buffer[] = new byte[32768];
103-
int bufferIndex;
104-
105-
int bufferLast;
106-
107102
String PortName;
108103

109104
private ServiceRegistration<Serial> fServiceRegistration;
@@ -148,22 +143,6 @@ public void removeListener(MessageConsumer consumer) {
148143
this.fConsumers.remove(consumer);
149144
}
150145

151-
/**
152-
* Returns the number of bytes that have been read from serial and are
153-
* waiting to be dealt with by the user.
154-
*/
155-
public int available() {
156-
return (this.bufferLast - this.bufferIndex);
157-
}
158-
159-
/**
160-
* Ignore all the bytes read so far and empty the buffer.
161-
*/
162-
public void clear() {
163-
this.bufferLast = 0;
164-
this.bufferIndex = 0;
165-
}
166-
167146
public void connect() {
168147
connect(1);
169148
}
@@ -251,187 +230,6 @@ private void notifyConsumersOfEvent(String message) {
251230
}
252231
}
253232

254-
/**
255-
* Returns a number between 0 and 255 for the next byte that's waiting in
256-
* the buffer. Returns -1 if there was no byte (although the user should
257-
* first check available() to see if things are ready to avoid this)
258-
*/
259-
public int read() {
260-
if (this.bufferIndex == this.bufferLast)
261-
return -1;
262-
263-
synchronized (this.buffer) {
264-
int outgoing = this.buffer[this.bufferIndex++] & 0xff;
265-
if (this.bufferIndex == this.bufferLast) { // rewind
266-
this.bufferIndex = 0;
267-
this.bufferLast = 0;
268-
}
269-
return outgoing;
270-
}
271-
}
272-
273-
/**
274-
* Return a byte array of anything that's in the serial buffer. Not
275-
* particularly memory/speed efficient, because it creates a byte array on
276-
* each read, but it's easier to use than readBytes(byte b[]) (see below).
277-
*/
278-
public byte[] readBytes() {
279-
if (this.bufferIndex == this.bufferLast)
280-
return null;
281-
282-
synchronized (this.buffer) {
283-
int length = this.bufferLast - this.bufferIndex;
284-
byte outgoing[] = new byte[length];
285-
System.arraycopy(this.buffer, this.bufferIndex, outgoing, 0, length);
286-
287-
this.bufferIndex = 0; // rewind
288-
this.bufferLast = 0;
289-
return outgoing;
290-
}
291-
}
292-
293-
/**
294-
* Grab whatever is in the serial buffer, and stuff it into a byte buffer
295-
* passed in by the user. This is more memory/time efficient than
296-
* readBytes() returning a byte[] array.
297-
*
298-
* Returns an int for how many bytes were read. If more bytes are available
299-
* than can fit into the byte array, only those that will fit are read.
300-
*/
301-
public int readBytes(byte outgoing[]) {
302-
if (this.bufferIndex == this.bufferLast)
303-
return 0;
304-
305-
synchronized (this.buffer) {
306-
int length = this.bufferLast - this.bufferIndex;
307-
if (length > outgoing.length)
308-
length = outgoing.length;
309-
System.arraycopy(this.buffer, this.bufferIndex, outgoing, 0, length);
310-
311-
this.bufferIndex += length;
312-
if (this.bufferIndex == this.bufferLast) {
313-
this.bufferIndex = 0; // rewind
314-
this.bufferLast = 0;
315-
}
316-
return length;
317-
}
318-
}
319-
320-
/**
321-
* Reads from the serial port into a buffer of bytes up to and including a
322-
* particular character. If the character isn't in the serial buffer, then
323-
* 'null' is returned.
324-
*/
325-
public byte[] readBytesUntil(int interesting) {
326-
if (this.bufferIndex == this.bufferLast)
327-
return null;
328-
byte what = (byte) interesting;
329-
330-
synchronized (this.buffer) {
331-
int found = -1;
332-
for (int k = this.bufferIndex; k < this.bufferLast; k++) {
333-
if (this.buffer[k] == what) {
334-
found = k;
335-
break;
336-
}
337-
}
338-
if (found == -1)
339-
return null;
340-
341-
int length = found - this.bufferIndex + 1;
342-
byte outgoing[] = new byte[length];
343-
System.arraycopy(this.buffer, this.bufferIndex, outgoing, 0, length);
344-
345-
this.bufferIndex = 0; // rewind
346-
this.bufferLast = 0;
347-
return outgoing;
348-
}
349-
}
350-
351-
/**
352-
* Reads from the serial port into a buffer of bytes until a particular
353-
* character. If the character isn't in the serial buffer, then 'null' is
354-
* returned.
355-
*
356-
* If outgoing[] is not big enough, then -1 is returned, and an error
357-
* message is printed on the console. If nothing is in the buffer, zero is
358-
* returned. If 'interesting' byte is not in the buffer, then 0 is returned.
359-
*/
360-
public int readBytesUntil(int interesting, byte outgoing[]) {
361-
if (this.bufferIndex == this.bufferLast)
362-
return 0;
363-
byte what = (byte) interesting;
364-
365-
synchronized (this.buffer) {
366-
int found = -1;
367-
for (int k = this.bufferIndex; k < this.bufferLast; k++) {
368-
if (this.buffer[k] == what) {
369-
found = k;
370-
break;
371-
}
372-
}
373-
if (found == -1)
374-
return 0;
375-
376-
int length = found - this.bufferIndex + 1;
377-
if (length > outgoing.length) {
378-
Common.log(new Status(IStatus.WARNING, Const.CORE_PLUGIN_ID,
379-
"readBytesUntil() byte buffer is too small for the " + length //$NON-NLS-1$
380-
+ " bytes up to and including char " + interesting, //$NON-NLS-1$
381-
null));
382-
return -1;
383-
}
384-
// byte outgoing[] = new byte[length];
385-
System.arraycopy(this.buffer, this.bufferIndex, outgoing, 0, length);
386-
387-
this.bufferIndex += length;
388-
if (this.bufferIndex == this.bufferLast) {
389-
this.bufferIndex = 0; // rewind
390-
this.bufferLast = 0;
391-
}
392-
return length;
393-
}
394-
}
395-
396-
/**
397-
* Returns the next byte in the buffer as a char. Returns -1, or 0xffff, if
398-
* nothing is there.
399-
*/
400-
public char readChar() {
401-
if (this.bufferIndex == this.bufferLast)
402-
return (char) (-1);
403-
return (char) read();
404-
}
405-
406-
/**
407-
* Return whatever has been read from the serial port so far as a String. It
408-
* assumes that the incoming characters are ASCII.
409-
*
410-
* If you want to move Unicode data, you can first convert the String to a
411-
* byte stream in the representation of your choice (i.e. UTF8 or two-byte
412-
* Unicode data), and send it as a byte array.
413-
*/
414-
public String readString() {
415-
if (this.bufferIndex == this.bufferLast)
416-
return null;
417-
return new String(readBytes());
418-
}
419-
420-
/**
421-
* Combination of readBytesUntil and readString. See caveats in each
422-
* function. Returns null if it still hasn't found what you're looking for.
423-
*
424-
* If you want to move Unicode data, you can first convert the String to a
425-
* byte stream in the representation of your choice (i.e. UTF8 or two-byte
426-
* Unicode data), and send it as a byte array.
427-
*/
428-
public String readStringUntil(int interesting) {
429-
byte b[] = readBytesUntil(interesting);
430-
if (b == null)
431-
return null;
432-
return new String(b);
433-
}
434-
435233
public void registerService() {
436234
this.fServiceRegistration = FrameworkUtil.getBundle(getClass()).getBundleContext().registerService(Serial.class,
437235
this, null);
@@ -458,36 +256,12 @@ synchronized public void serialEvent(SerialPortEvent serialEvent) {
458256
int bytesCount = serialEvent.getEventValue();
459257

460258
if (IsConnected() && bytesCount > 0) {
461-
synchronized (this.buffer) {
462-
try {
463-
byte[] readBytes = this.port.readBytes(bytesCount);
464-
465-
// Check there is enough buffer to store new bytes
466-
if (readBytes.length > this.buffer.length) {
467-
int newLength = this.buffer.length;
468-
while (readBytes.length > newLength) {
469-
newLength *= 2;
470-
}
471-
472-
byte temp[] = new byte[newLength];
473-
System.arraycopy(this.buffer, 0, temp, 0, this.buffer.length);
474-
this.buffer = temp;
475-
}
476-
477-
// Append read bytes to buffer tail
478-
System.arraycopy(readBytes, 0, this.buffer, this.bufferLast, readBytes.length);
479-
if (this.monitor == true) {
480-
System.out.print(new String(readBytes));
481-
}
482-
this.bufferLast += readBytes.length;
483-
484-
notifyConsumersOfData(readBytes());
485-
} catch (SerialPortException e) {
486-
errorMessage("serialEvent", e); //$NON-NLS-1$
487-
}
259+
try {
260+
notifyConsumersOfData(this.port.readBytes(bytesCount));
261+
} catch (SerialPortException e) {
262+
errorMessage("serialEvent", e); //$NON-NLS-1$
488263
}
489264
}
490-
491265
break;
492266
case SerialPortEvent.BREAK:
493267
errorMessage("Break detected", new Exception()); //$NON-NLS-1$

0 commit comments

Comments
 (0)