Go Down

Topic: Character limit? (Read 2008 times) previous topic - next topic

Jassper

Is there a limit to how many charzcters can come into the serial port on a Arduino?

Coding Badly


The ring buffer is 63 bytes with Arduino 1.0.

Graynomad

And as long as you keep reading from the buffer and handling the characters without the need to store more than maybe a 1000 or so at a time there is no limit.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Jassper

Im loosing data after 60 or so inxoming characters. I stor the data in an array aa it comes in. So its seems to be dropping anything  over the limit.

el_supremo

What baud rate are you using?
It might be best to post your code.

Pete

Graynomad

Quote
60 or so incoming characters

Probably not a coincidence.

Post you code and use the code formatting tag (the # button above the text entry box).

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Jassper

Yep, was going to post my code but was on my Android at the time. (btw, I'm looking for a Arduino file viewer/editor for Android :) )

Anyways, I'm sure there is a better way to read incoming serial than I have, but after some testing everything works until I reach 64 characters.

I stripped everything out just to make sure it wasn't anything else i was doing.
Code: [Select]


// ****************************************************************
// File Name:
// Version:
// Created By:
// Comments:
// ****************************************************************

// ********************** Version x.xx ****************************
// 03.11.12

// ********************** Consants ********************************
#define EOP        ';'
#define SERSIZ     25

// ********************** Included Libraries **********************
//#include <SoftwareSerial.h>

// ********************** Defaults ********************************
// ********************** Analog Input Pins ***********************
// ********************** PWM Output Pins *************************
// ********************** Digital Input Pins **********************
// ********************** Digital Output Pins *********************
// ********************** Output Pins *****************************
// ********************** Sensor Pins *****************************

// ********************** Variables *******************************
int i;
int c;
int Percent[4];
int iData[SERSIZ];
int iTemp;

byte index = 0;

// ********************** Other Information ***********************

// ********************** Declorations ****************************
//SoftwareSerial LCDserial(7,8);

// ********************** SET UP **********************************
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  ///////////////////////// TO READ SERIAL ////////////////////////
  ReadData();
   
  for(i=0; i < SERSIZ; i++)
    {
    Serial.println(iData[i]);
    }
  delay(1000);
   
}

// *********************** FUNCTIONS *******************************
// Read Incoming Data from Tank monitor
void ReadData()
{
  while(Serial.available() > 0)
    {
    //char aChar = Serial.read();
    c = Serial.read();
    if(c > 47 && c < 58)
      {
      iTemp = iTemp * 10 + (c - 48);
      }
    else if(c == ',') // move to next index
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      iTemp = '\0';
      }
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      break;
      }
    }
  index=0;
  c = 0;
  iTemp = '\0';
}
// End ReadData()



If I send this, 48 characters,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4;

I get back,
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
0 // 25th character is null

But if I send this, 64 characters
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,3,3,3,3,3,3,3,3;

I get this
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
13
3
3
3
3
3
3
3
0
0

The 24th character becomes null and it gets worse the more over characters I send.

But I'm completely open to it being me ;)

Graynomad

#7
Apr 26, 2012, 03:53 am Last Edit: Apr 26, 2012, 04:01 am by Graynomad Reason: 1
It looks to me that your code will read the chars in the buffer very quickly, after that you will only get a single char at a time because your ReadData() function does nothing for 99.9% of the time because the Arduino is waiting for characters.

  while(Serial.available() > 0)

If there is no chars you return and print something, probably a \0 which you don't see.

After a mS or so a character comes in, you print it then go back to printing \0s because the next char hasn't arrived yet.

Exactly when in the data stream do you want to print? If it's at EOP then

Code: [Select]
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      printFlag = true;    // set a flag
      break;


Code: [Select]
  ReadData();
  if (printFlag ) {
     for(i=0; i < SERSIZ; i++)
       {
       Serial.println(iData[i]);
       }
   }
   printFlag = false;


also there are quite a few things that code be tidied up, for example this

Code: [Select]
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';


can be

Code: [Select]
      iData[index++] = iTemp;
      iData[index] = '\0';



______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

michael_x

My idea:

You send those 64 characters while loop() hangs in the delay(1000) function. Thus the last one gets lost.

delay() is bad, at least in your case  ;)

Graynomad's proposal implies that ReadData() is called continuously, and signals when a complete block with ";" has arrived.

Jassper


My idea:

You send those 64 characters while loop() hangs in the delay(1000) function. Thus the last one gets lost.

delay() is bad, at least in your case  ;)

Graynomad's proposal implies that ReadData() is called continuously, and signals when a complete block with ";" has arrived.


The Delay makes no difference, I thought that too removed it, no different.

Graynomad,
After tiring your suggestions, I get nothing but 0's no matter what I do.
I don't need this to print at all, I was just using that to make sure that the incoming string was getting stored correctly.

Thanks for the tidy tip, I assume then that this
iData[index++] = iTemp;
      iData[index] = '\0';

Adds one to index after it assigns iTemp?

Jassper

I'm doing something really wrong here.

If I add a Serial.println("Serial Available"); within the loop, and send 2,2,2; I will get 6 "Serial Available" and then 2 2 2 and the rest 0's. If I remove that Serial.print line I get nothing but 0's.

???


Graynomad

Quote
Graynomad's proposal implies that ReadData() is called continuously,

Yeah I didn't spot the delay(1000).

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

Your sticking '\0' into ints, probably OK but just use 0

iData[index] = 0;
iTemp = 0;

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Jassper


Your sticking '\0' into ints, probably OK but just use 0

iData[index] = 0;
iTemp = 0;

______
Rob


Yep tried that too. No different.
I think it is some kind of timing issue or something. If I remove the delay, and only print when EOP is reached, I get all 0's. If I add a deay - either in the main loop or in the function, I will get some results - although not accurate results. If I print every loop I will get results (again not accurate) with or without the delay.

I'm going to try making it read the serail until the EOP is reached instead and see how that works out.

Jassper

I've changed the function to this,
Code: [Select]

void ReadData()
{
  index = 0;
  iTemp = 0;
  c = 0;
  while(c != EOP)
    {
    c = Serial.read();
    if(c == 0)
      {
      Serial.println("Null found.. Ending ReadData()");
      break;
      }
    if(c > 47 && c < 58)
      {
      iTemp = iTemp * 10 + (c - 48);
      }
    else if(c == ',') // move to next index
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      iTemp = 0;
      }
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      PrintFlag = true;
      break;
      }
    }
}

But how do I exit the while loop if the EOP isn't reached? The if(c == 0) line doesn't work and if I check for Serial.available() it will exit as soon as it is called.

Go Up