Serial.available in Arduino 1.0 vs Arduino 0023

I just recently opened up an old sketch that I used to control some outdoor led lighting I installed. New computer, fresh download of Adruino 1.0.

This is my first run in with Arduino 1.0 so it took me a long time to troubleshoot the sketch that was based around Serial.available() > 96 to control 32 rgb leds. It took my a while to strip everything away, download an older ide, and test in the serial monitor.

I'm trying to figure out what exactly the difference is between the two ide's for the following code and use case.

void setup(){                
  Serial.begin(57600);
}
void loop(){
  Serial.println(Serial.available()); 
}

Arduino 0023 > serial monitor > enter and send "1" before Serial.available max out at 127

Arduino 1.0 > serial monitor > enter and send "1" before Serial.available max out at 63

I didn't see anything in the release notes about a specific change in Serial.available, only that the communication is now asynchronous. I feel like I must be missing something obvious. How do I read 96 bytes out of the buffer in Arduino 1.0?

Thanks

Just to be clear, I'm entering "1" over and over until I find the limit, not just entering "1" once.

I think I saw that now serial output is buffered too, the input buffer is smaller that it was pre 1.0.

The answer to your question is, process the data as it arrives, don't wait until it's all there.

Thanks, I was wondering if it was something like that. Looks like I'll be switching to a serialEvent scheme. I'm wondering what the speed implications will be.

I'll now be doing more executions per Serial.read() then when I was reading 96 in a loop.

int values[96];
int valueIndex = 0;
void setup(){                
  Serial.begin(57600);
}
void loop(){
  //Serial.println(Serial.available()); 
}
void serialEvent(){
  Serial.println(valueIndex);
  values[valueIndex++] = Serial.read();
  if(valueIndex > 95) valueIndex = 0;
}
int values[96];

What values are you sending that need to be ints, rather than bytes?

  values[valueIndex++] = Serial.read();

Oh, I see. None.

Arduino 0023 > serial monitor > enter and send "1" before Serial.available max out at 127

Arduino 1.0 > serial monitor > enter and send "1" before Serial.available max out at 63

Serial data is buffered. The size of the buffer is different, since outgoing serial data is now buffered, too. To minimize the impact, the incoming and outgoing buffers occupy the same space that the incoming buffer used to.

The key to processing serial data is to not wait for all the data to be buffered before you start reading it.

Read and store in another location as soon as the data arrives.

Keep in mind that serial data delivery is not guaranteed. You should not expect a packet to be "the next 96 bytes". You WILL get out of sync if you make that assumption.

int values[96];
int valueIndex = 0;
void setup(){                
  Serial.begin(57600);
}
void loop(){
  //Serial.println(Serial.available()); 
}
void serialEvent(){
  Serial.println(valueIndex);
  values[valueIndex++] = Serial.read();
  if(valueIndex > 95) valueIndex = 0;
}
  1. Is this supposed to run and do other than nothing?
  2. Why do you do a Serial.read without checking Serial.available()? Are you looking for NULLs?
  3. Why bother with values[] as ints even if you actually called serialEvent()?

PaulS:
Serial data is buffered. The size of the buffer is different, since outgoing serial data is now buffered, too.

Haven't made the switch yet, kinda hanging on for a patch. But it's a powerful incentive knowing that short serial output shouldn't take as long with 1.0.

@GoForSmoke - serialEvent() is new for 1.0. If you create a serialEvent() function, it gets called when data is available. serialEvent() - Arduino Reference

Looks like they bulked up main.cpp to add that:

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USB.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}

	return 0;
}

Why stop at serialEvent? Why not wireEvent? And spiEvent? And timerEvent? Think of the possibilities!

RiceCrispyAdams:
Arduino 0023 > serial monitor > enter and send "1" before Serial.available max out at 127

Arduino 1.0 > serial monitor > enter and send "1" before Serial.available max out at 63

In old IDE:

#if (RAMEND < 1000)
  #define RX_BUFFER_SIZE 32
#else
  #define RX_BUFFER_SIZE 128
#endif

In new IDE:

#if (RAMEND < 1000)
  #define SERIAL_BUFFER_SIZE 16
#else
  #define SERIAL_BUFFER_SIZE 64
#endif

There's your discrepancy.

I would use a state machine or buffer data up internally.

But it's a powerful incentive knowing that short serial output shouldn't take as long with 1.0.

I don't know about that. Sending at 9600 baud will send 960 characters per second, regardless of whether they added buffering or not.

Sending at 9600 baud will send 960 characters per second, regardless of whether they added buffering or not.

And the Arduino supports rates up to 12 times that fast.

That is true but running 0022 I understand that when I do Serial.print(some_string), my code will wait until all those characters have been taken before my next instruction executes. Without a buffer, that means each being sent off at the Serial baud rate before the next is taken. With a buffer and a string no longer than the buffer, the characters will be taken at fill the buffer speed which I believe should be magnitudes faster than 9600 baud.

I should be more clear. It shouldn't take as long at the send end where IMO speed is more critical.