Go Down

Topic: Serial1 IN--> Serial0 OUT why is my data a bit different? (Read 739 times) previous topic - next topic

fxmech

Nov 27, 2012, 05:28 pm Last Edit: Nov 27, 2012, 05:37 pm by fxmech Reason: 1
I have an Sensor board that outputs a serial stream (currently at 9600baud).
My arduino ProMicro receives the stream and adds one more piece of data to it and then just sends it out Serial0 (USB).

My data looks good with a simple Sketch but when I try combining data I am getting an abnormality and I cant figure out why.
Hope one of you more experienced with serial communications can help me out...

Here is the data as received by the arduino using this Sketch:

Code: [Select]
void setup()
{
 Serial.begin(9600);
 Serial1.begin(9600);
}

void loop()
{
 if (Serial1.available()){
   char ch=Serial1.read();
   Serial.print(ch);
 }
}



Data:

0,-379,-15969,
#,-7079,-379,-15971,
#,-7080,-376,-15972,
#,-7081,-376,-15972,
#,-7081,-376,-15971,
#,-7080,-376,-15971,
#,-7079,-378,-15972,
#,-7079,-381,-15972,
#,-7081,-380,-15971,
#,-7080,-382,-15972,
#,-7080,-382,-15970,
#,-7080,-383,-15969,
#,-7079,-385,-15969,
....etc


Now with the modified sketch to insert a new piece of data after the "#,"  I get this:

Code: [Select]
char ch;

void setup()  
{
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 Serial1.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }
}

void loop() // run over and over
{
 while(ch!='#'){//look for start of data (#)
   if (Serial1.available()){
     ch=Serial1.read();
   }
 }
 Serial.print(ch);            //print the "#"
 if (Serial1.available()){
   ch=Serial1.read();
   Serial.print(ch);          //print the ","
 }
 Serial.print("DATA");     //space where I want to insert my additional sensor data
 while (!Serial1.available()){
 }
 while (ch >= ',')  {         //If we are not at start of next stream keep printing data (only /n, /l , and # are below "," in ascii)
   if (Serial1.available()){
     Serial.print(ch);
     ch = Serial1.read();  
   }
 }
 Serial.println();       // if ch > ',' then it read a end of line or a "#" so it is done with that line.... Print a line end
}



Data:

#,DATA,-7103,-385,-15978,
#,DATA,-7104,-384,-15978,
#,DATA,-7104,-386,-15975,-378,-15975,
#DATA
#,DATA,-7105,-378,-15975,
#DATA
#,DATA,-7105,-380,-15975,
#DATA
#,DATA,-7104,-382,-15976,
#DATA
#,DATA,-7105,-384,-15974,
#DATA
#,DATA,-7104,-383,-15977,
#DATA
#,DATA,-7104,-382,-15978,
#DATA
#,DATA,-7104,-382,-15977,
#DATA
#,DATA,-7105,-383,-15977,
#DATA
#,DATA,-7106,-382,-15978,
#DATA
#,DATA,-7106,-384,-15978,
#DATA
#,DATA,-7107,-383,-15976,
#DATA
....etc

Im guessing its some sort of buffer issue????  I'm very new to this so any help will be a lifesaver!

PaulS

Code: [Select]
  while(ch!='#'){//look for start of data (#)
  while (ch >= ',')  {  //If we are not at start of next stream keep printing data

Which is it? Does # mark the start of data or does ,?

fxmech

The # sign is the start of the line of data but actual data that I will be extracting is after the first ","
The word "DATA" will be replaced by an integer value, and then there are 3 more pieces of data after it

fxmech

I updated the notes in my original posted CODE to make it more clear....

PaulS

This:
Code: [Select]
  while(ch!='#'){//look for start of data (#)
    if (Serial1.available()){
      ch=Serial1.read();
    }
  }
  Serial.print(ch);            //print the "#"
  if (Serial1.available()){
    ch=Serial1.read();
    Serial.print(ch);          //print the ","
  }
  Serial.print("DATA");     //space where I want to insert my additional sensor data


explains this:
Quote
#,DATA,-7104,-386,-15975,-378,-15975,
#DATA

Sometimes the comma arrives in time. Sometimes it does not. That appears to be key in whether the rest of your code executes correctly (the comma arrived quickly) or not (it didn't).

If you need the comma to arrive, you need to wait for it.

fxmech

That makes sense I figured it was something like that but I honestly have no idea how to correct it :(
Is it  done by adding a delayMicroseconds(#) line or does it have to do with placing Serial.available() commands??

How do I adjust the serial stream.  I don't even know if I have been doing a good job of placing Serial.available() lines so far...
I feel like I just sprinkled them on like toppings on a cupcake ;)

PaulS

Quote
Is it  done by adding a delayMicroseconds(#) line??

No. Absolutely not.

Quote
or does it have to do with placing Serial.available() commands??

A while loop, waiting for another character (possibly requiring that the character be a comma) would be what I would use.

fxmech

OK tried it and made some more changes...  Here is the working version of the sketch!!!
Thanks for the help!

Code: [Select]
char ch;

void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial1.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop() // run over and over
{
  Serial1.find("#,");  //find starting characters of data block (#,)
  Serial.print("#,"); //print "#,"
  Serial.print("D");//space where I want to insert my additional sensor data
  Serial.print(","); //print coma after added data
  ch = Serial1.read();  //read first piece of data
while (ch<44 || ch>57) { //if data is not a number or a minus sign skip it
   if (Serial1.available()){
     ch = Serial1.read();
   }
}
  while (ch>=44 && ch<58)  {  //incoming data is a number, coma, or minus sign keep printing data
    if (Serial1.available()){
           Serial.print(ch);
      ch = Serial1.read();   
    }
  }
  Serial.println(); // end of data Print a line end
}

PaulS

Code: [Select]
while (ch<44 || ch>57) { //if data is not a number or a minus sign skip it
  while (ch>=44 && ch<58)  {  //incoming data is a number, coma, or minus sign keep printing data

A week from now (or later today in my case), will you remember what 44 means? 57? 58?

Code: [Select]
while (ch<',' || ch>'9') { //if data is not a number or a minus sign skip it
  while (ch>=',' && ch<';')  {  //incoming data is a number, coma, or minus sign keep printing data

How about this? Would you recognize these limits a week later?

It's the same code. One is easy to understand; one isn't.

fxmech

Good point!
I actually started doing it that way but was getting error and did not know what was causing them yet so I tried a few things including changing the text to ascii values...  now I know that wasn't the problem so I do plan on doing it that way in the future...  cause your right, I wont remember...  btw off topic do you plan ahead when you write code???  I have been doing some pretty complicated stuff (within the stuff I understand... not serial communications)  I never plan ahead just start writing and figure it out as I go so yes 1 week later if not for comments in my code I would be completely lost.  I was just curious if most non professional programmers work this way or if they start with a flowchart...

PaulS

Quote
btw off topic do you plan ahead when you write code???

Of course. I plan what I want the program to do. I develop a general outline of how to make the program do that. Then I start developing code. Sometimes the planning is pretty obvious. Sometimes not. Sometimes the planning takes a few seconds. Sometimes longer. Sometimes a lot longer.

If you spend even a few minutes writing comments and then add code between the comments to implement what the comments say, your programs will be a lot better.

Go Up