Pages: 1 [2] 3   Go Down
Author Topic: Character limit?  (Read 1608 times)
0 Members and 1 Guest are viewing this topic.
nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll have a look, meanwhile can you try this

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


// ********************** Variables *******************************
int Percent[4];
int iData[SERSIZ];
int iTemp = 0;
boolean printData = false;
byte index = 0;

// ********************** SET UP **********************************
void setup()
{
  Serial.begin(9600);
  while(Serial.read() != EOP) {}; // sync with frames
}

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

// *********************** FUNCTIONS *******************************
// Read Incoming Data from Tank monitor
void ReadData() {

if(Serial.available() > 0) {
int c = Serial.read();
switch (c) {
case ',':
iData[index++] = iTemp;
iData[index] = 0;
iTemp = 0;
break;

case EOP:
iData[index++] = iTemp;
iData[index] = 0;
printData = true;
iTemp = 0;
index = 0;
break;

default:
if(c >= '0' && c <= '9') {
iTemp = (iTemp * 10) + (c - '0');  // use () for each part, don't trust operator precedance
}
}
}
}
// End ReadData()

______
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

Ok, I give up. I am totally misunderstanding how the serial thing works.

This works, but could get trapped in a infinite loop if ';' is never reached.
Code:
void ReadData()
{
  index = 0;
  iTemp = 0;
  c = 0;
  while(c != EOP)
    {
    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';
      PrintFlag = true;
      break;
      }
    }
}


If I change it and add a check for c to == -1, it doesn't work.
Code:
void ReadData()
{
  index = 0;
  iTemp = 0;
  c = 0;
  while(c != EOP)
    {
    c = Serial.read();
    if(c == -1)
      {
      Serial.print("end");
      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;
      }
    }
}

c should be equal to what was read with c = Serial.read(), so if I send a 1,2; c should first equal "1", but it does not, in stead I get "end" and the function exits. In fact I will get "end" 3 times, then 25 0's from the array print.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25803
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
his works, but could get trapped in a infinite loop if ';' is never reached.
You could put in a timeout
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

This appears to all be a timing type issue, which makes how the Serial data is handled unreliable. I can get the code working, but I can change the results, or how well it works by adding delay() in different parts of the code, or a simple Serial.print() added here or there will yield different results. It is almost like the data is wiped from the buffer faster than it can be processed or something.

Either I am doing something wrong or there is something wrong with my Arduino. I can't imagine that this issue hasn't happened before because I shouldn't half to adjust how I handle the data based on how many lines of code - thus causing different timings - I have in the program. In otherwords, adding more code shouldn't change my Serial results, but it does.

I'm switching Arduino's and see what results I get.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
adding more code shouldn't change my Serial results, but it does.
We need to see your current code. Serial data arrives in it's own time. You have to remove it from the buffer faster than it is placed in the buffer, or you will begin loosing data. delay()s and Serial.print()s cause it to take longer to read all the data.

If you are sending less than a buffer's worth of data, though, you could take all day to read the data, and not risk loosing data.

That you are having such difficulties means that the problem is in your code. Post it again.
Logged

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

We need to see your current code. Serial data arrives in it's own time. You have to remove it from the buffer faster than it is placed in the buffer, or you will begin loosing data. delay()s and Serial.print()s cause it to take longer to read all the data.

This is what I am currently working with. It is working but I don't feel confidant that it is correct.
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 = 0;

boolean PrintFlag = false;

int index = 0;

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

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

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

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

// *********************** FUNCTIONS *******************************
// Read Incoming Data from Tank monitor
void ReadData()
{
  index = 0;
  iTemp = 0;
  c = 0;
  int count = 0;
 
  while(c != EOP)
    {
    if(count >= 30000)
      {
      Serial.println("end");
      break;
      }
    c = Serial.read();
    if(c > 47 && c < 58)
      {
      iTemp = iTemp * 10 + (c - 48);
      }
    else if(c == ',') // move to next index
      {
      iData[index] = iTemp;
      index++;
      iTemp = 0;
      }
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      PrintFlag = true;
      break;
      }
    count++;
    }
}
// End ReadData()

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Counting the number of times that the while loop has iterated is a lousy indication of time.

Code:
    if(c > 47 && c < 58)
would make a lot more sense as
Code:
    if(c >= '0' && c <= '9')
with no need to consult an ASCII table that the compiler intimately knows.

Code:
      iTemp = iTemp * 10 + (c - 48);
c - '0'...

Code:
    else if(c == EOP)
      {
      iData[index] = iTemp;
      index++;
      PrintFlag = true;
      break;
      }
You should NOT be incrementing index here. You've stored the last valid value.

Code:
  if(PrintFlag)
    {
    for(i=0; i < SERSIZ; i++)
      {
      Serial.println(iData[i]);
      }
    PrintFlag = false; 
    }
You should only be printing index values, here. You know how many you received. After using/printing the values, some initialization would be a good idea. Wiping the values from iData, for instance. iData is a lousy name, by the way. It gives no clue what is in the array.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25803
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
iData is a lousy name, by the way. It gives no clue what is in the array.
...and could get you into trouble with Apple.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

PaulS,
Thanks for the clean up tips, but that has no effect on how the code functions. I can still re-create all my issues.

I'm not understanding something , or the Serial.read() don't work the way it is stated. Take this code for example,
Code:
int i;
int c;
int iTemp = 0;

boolean PrintFlag = false;

int index = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  ///////////////////////// TO READ SERIAL ////////////////////////
  if(Serial.available())
    {
    ReadData();
    }
}

// *********************** FUNCTIONS *******************************
void ReadData()
{
  c = 0;
  
  while(c != ';')
    {
    c = Serial.read();
    if(c == -1)// no data
      {
      Serial.println("end");
      break;
      }
    Serial.println(c);
    }
}
// End ReadData()

If I send, 123 I should get back ,
49
50
51
end

But I don't, I get this instead,
49
end
50
end
51
end

This indicates that every other Serial.read() results in a -1 null character instead of reading the next character.

There is serial available so the function is called, c is loaded with Serial.read() and it is printed as 49 (char '1'), the while loop is no over so c is read again, but this time it is -1 instead of the next character in line to be read ??? There is still data avail so the main loop calls ReadData() again, then 50 is printed, but again the next character read is -1, so on.

Clearly I am missing something.

BTW, what name would you give a bunch of numbers other than "Data"?

EDIT
Further more, if I change it to simply,
void ReadData()
{
  c = 0;
   
  while(c != ';')
    {
    c = Serial.read();
    Serial.println(c);
    }
}
Sending 123 to the port I get

49
-1
-1
-1
-1
-1
-1
50
-1
-1
-1
-1
-1
51
-1
-1
-1
44
-1
-1
-1
-1

Indicating that the while loop is executing much faster than the data is coming in.
« Last Edit: April 26, 2012, 09:27:48 am by Jassper » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25803
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But I don't, I get this instead,
49
end
50
end
51
end
That's precisely what I'd expect you to get.
Serial comms are SLOW, you can whip around your while loop loads of times in the time it takes to receive one character (in fact, it's only your serial prints that are slowing you down)

Quote
This indicates that every other Serial.read() results in a -1 null character instead of reading the next character.
No, Serial.read returns -1 when there is nothing there to read, i.e. nothing has been received. (-1 is most definitely NOT null)


Follow your code through:
In loop, suddenly "available" returns 1 because a character has just been received.
Call ReadData, set c to zero, test to see if c is a semicolon (it isn't), so read the character that just came in.
it isn't -1, so print it.
Go back to the top of the while, see if the character was a semicolon.
It wasn't a semicolon, so read the the next character.
Oh dear. It hasn't arrived yet.
« Last Edit: April 26, 2012, 09:45:54 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Quote
But I don't, I get this instead,
49
end
50
end
51
end
That's precisely whaty I'd expect you o get.
Serial comms are SLOW, you can whip around your while loop loads of times in the time it takes to receive one character (in fact, it's only your serial prints that are slowing you down)

See, I thought that from the very beginning, So I removed the serial printing and made it only print when nothing else was going on just to verify the data. all I got back was 0's as if nothing had been stored. I could only get the data to store if I printed it right away. So I need to verify that there is actually a character present before reading that character.

Logged

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


Follow your code through:
In loop, suddenly "available" returns 1 because a character has just been received.
Call ReadData, set c to zero, test to see if c is a semicolon (it isn't), so read the character that just came in.
it isn't -1, so print it.
Go back to the top of the while, see if the character was a semicolon.
It wasn't a semicolon, so read the the next character.
Oh dear. It hasn't arrived yet.

I am seeing that now, the Serial port is very slow which explains why when I add Delays I can get it to work - but that's not the right way to do it.

I guess I should have just asked the question,
How do I separate a string of data? For example I will be receiving something like this,
345,234,532,231,426,63,312,4,523,543;
How do I tun it into
Data[0] = 345
Data[1] = 234
Data[2] = 532
etc etc
?

If I use while(Serial.available()) the wile loop exits before the next character come in. if I check for a ending character ';' I could get stuck in a loop. I agree that counting the loops is a cheesy way of doing it, guess I will half to compare mils to make sure it's not taking too long.


Oh, by the way everyone, I really appreciate the help and head knocking smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I agree that counting the loops is a cheesy way of doing it, guess I will half to compare mils to make sure it's not taking too long.
It's really very simple:
Code:
void ReadData()
{
   unsigned long startTime = millis();
   while(c != ';' && millis() - startTime < 30000)
   {
      // code you currently have
   }

   // At this point, you either got the ; or waited 30 seconds for it, but it didn't arrive
}
Logged

Spain
Offline Offline
Full Member
***
Karma: 0
Posts: 191
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why don't you just check one character each time?

When you check serial.available, you don't know how many characters are in the buffer, you just know there are one or more characters.

Maybe this simple example can help you:

Code:
#define EOP        ';'
#define SERSIZ     100

int iData[SERSIZ];
int index = 0;
boolean flagPrint = FALSE;

void loop()
{
  int i;
  ///////////////////////// TO READ SERIAL ////////////////////////
  ReadData();

  ///////////////////////// TO CHECK BUFFER ////////////////////////
  if(flagPrint)
  {
    for(i=0;i<index;i++)
      Serial.println(iData[i]);
    index = 0;
    flagPrint = FALSE;
  }
}

// *********************** FUNCTIONS *******************************
void ReadData()
{
  int c = 0;
 
  if(Serial.available())
  {
    c = Serial.read();
    if(c==EOP)  // End of message
      flagPrint = TRUE;
    else
    {
      iData[index++] = c; // Here you can add more actions with the character
    }
  }
}
// End ReadData()

You don't need to add a timer or something like that, because you only read once after serial.available(), if there are more characters in the serial buffer, you will read the next one in the next loop round.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at this: http://jhaskellsblog.blogspot.com/2011/05/serial-comm-fundamentals-on-arduino.html

Then this: http://jhaskellsblog.blogspot.com/2011/06/parsing-quick-guide-to-strtok-there-are.html
Logged


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