Serial Buffer Size

Hi,
hooking up Arduino to a terminal using RS232 Shield, when the terminal sends data over to Arduino, it always stop at 240 plus, seems to me the Serial buffer not enough to receive more than that? despite the reading as follow:

void loop() {
if (Serial.available()) {
int c = Serial.read();
}
}

how to receive data more than the serial buffer size? Thanks,

V.C.

What is your evidence ?

This piece of code should run forever.

venngomez:
void loop() {
if (Serial.available()) {
int c = Serial.read();
}
}
how to receive data more than the serial buffer size? Thanks,

The serial buffer will never fill up with that code...you're constantly emptying it.

What makes you think it stops? That code doesn't do anything with the data so where does the number 240 come from?

Thanks for the replies, reasons for me to suspect are:

  1. I use a PC to get the data from the same terminal, it has more than 700 bytes.
  2. I use following code to check how many read, it just stop somewhere between 240 and 300:
    void loop()
    {

// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
client.flush();
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
client.stop();

if (Serial.available()) {
int c = Serial.read();
cc ++;
ss = ss + "-" + String(c);
if (cc >= 50) {
httpPost(ss);
cc = 0;
ss = "";
}
}
}

void httpPost(String s) {
if (client.connect()) {
String temp = "GET /INFO?name=" + s + " HTTP/1.0";
client.println(temp);
client.println();
}
}

I think you're running out of RAM.

the program is quite simple, should not be using too much RAM, btw, the board is Arduino Duemilanove

It's easy to check the memory, just include:

#include <MemoryFree.h>

and then print it so you can check what's going on.

Serial.println(freeMemory());

When one uses an ethernet shield and the string library, memory usage can get out of hand pretty quickly, so you could be running out on unexpectedly. You should allow a couple of hundred bytes of leftover memory for transient things that happen or you will get random, unexplained failures.

You can get MemoryFree from the playground, I put it in everything I do these days. Saved my projects several times now.

This way, you can cross this item off the list of possible problems.

venngomez:
the program is quite simple, should not be using too much RAM, btw, the board is Arduino Duemilanove

Oh really? Prove me wrong...

BTW: claiming something doesn't work and posting code THAT DOESN'T SHOW the problem right away stinks! Consider yourself slapped around the head with a wet fish - numerous times.

The ATmega328 has a tiny amount of RAM, 2k, you don't want ever to concatenate strings in memory like this. Your computer may has millions of times more RAM than a microcontroller and virtual memory too...

  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
     client.flush();
  }

The code does not do what the comments say. Fix one or the other.

    ss = ss + "-" + String(c);

There is no reason to waste resources creating a String object to pass to the + operator. It is overloaded to accept char, too.

RAM size is an issue in my old code, thanks for all those helping a newbie like me here. to eliminate this, I use a SD card to store the result, following is the new code, but it still gets only part of the data expected from the terminal, probably around 140 or a little more, looks like the serial buffer overflow? will following help:

  1. reduce the terminal speed to 9600?
  2. use Arduino Mega?

#include <SD.h>

File myFile;

int done;

void setup() {

done = 0;
pinMode(13, OUTPUT);
digitalWrite(13, LOW);

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
myFile = SD.open("LOG.TXT", FILE_WRITE);

Serial.begin(38400);
}

void loop()
{
if (Serial.available()) {
int c = Serial.read();
if (c < 32) {
myFile.print(c, HEX);
myFile.print("="); // just for debugging
}
else {
myFile.print((char)c);
myFile.print("-"); // for debugging
}
}

if (!done && millis() > 60000) { // let's close the file after one min
digitalWrite(13, HIGH);
myFile.flush();
myFile.close();
done = 1;

}
}

    digitalWrite(13, HIGH);
     myFile.flush();
      myFile.close();

Doesn't this disable the SD before you finish the file?


Rob

The data sheet for the chip used on your arduino should specify the buffer size of the built in serial port. I think I heard that it is 128 bytes.

zoomkat:
The data sheet for the chip used on your arduino should specify the buffer size of the built in serial port. I think I heard that it is 128 bytes.

Right, surprisingly Mikal Hart's NewSoftSerial works in this case, it has a buffer of 64 bytes. now I got the complete data from the terminal.

 if (!SD.begin(4)) {

Serial.println("initialization failed!");
   return;
 }
 myFile = SD.open("LOG.TXT", FILE_WRITE);

Serial.begin(38400);

So you do Serial.println, and then afterwards you do Serial.begin? Also the return just gets you into loop a little more quickly.

Perhaps:

 Serial.begin(38400);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    exit (1);  // error exit ;)
  }
  myFile = SD.open("LOG.TXT", FILE_WRITE);

you are absolutely right! did not notice that, that's really wrong. actually i can't use Serial.println in that case as it is already connected to the terminal.

Thanks.

Graynomad:

    digitalWrite(13, HIGH);

myFile.flush();
      myFile.close();




Doesn't this disable the SD before you finish the file?

______
Rob

Hi Rob,

Can you give some more tips on this? quite new to this Arduino, I close the file after one min of running the app for debugging purpose, the reading of data from terminal takes less than a second.

V.C.

I've not used SD cards or that library but normally with an SPI device you raise SS (pin 13 in your case) after you have finished with the device.

That code looks like you deselect the card then close the file. If closing the file involves accessing the SD card I don't see how that can work.

OTOH if the .flush() and .close() functions lower pin 13 then it will work, but in that case there's no point you doing anything with that pin, just let the library handle it.


Rob

Hi Rob,

I see, it's a newbie's code:) my first few days in Arduino, i used it to turn a light high to signify the end of a process for debugging as well, not knowing it will cause a problem to SD. after switching my serial lib to NewsoftwareSerial, everything seems running perfectly, I can have two serial devices, one connecting to terminal, another for monitoring, thanks to everybody who answer my questions.

v.c.