Pages: [1] 2 3   Go Down
Author Topic: Character limit?  (Read 1791 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


The ring buffer is 63 bytes with Arduino 1.0.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8625
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1704
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pete
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8625
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley )

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:

// ****************************************************************
// 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 smiley-wink
 
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8625
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';
      printFlag = true;    // set a flag
      break;

Code:
  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:
      iData[index] = iTemp;
      index++;
      iData[index] = '\0';

can be

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


______
Rob
« Last Edit: April 25, 2012, 09:01:02 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3091
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink

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?
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

???

Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8625
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Graynomad's proposal implies that ReadData() is called continuously,
Yeah I didn't spot the delay(1000).

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 130
Posts: 8625
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

iData[index] = 0;
iTemp = 0;

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've changed the function to this,
Code:
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.
Logged

Pages: [1] 2 3   Go Up
Jump to: