Commenting out debugging causes code to not work..

I know this is probably something easy, but I’ve been beating my head against the LCD for a couple of hours and some fresh eyes might help.

I’m writing a function in a C library to receive a serial message. I’ve been debugging it (not the best coder ever…) so I added in the commented out line just below the first curly bracket for debug. Everything worked just fine.

Now when I comment it out and rerun the code, nothing works. Same exact code, same inputs.

Anybody got any ideas?

Thanks!

void rSerial::receiveTemp(tempMeasurement *t)
{
   //if (msgIn()) Serial.print("MsgIn"); //THE LINE IN QUESTION//
   while (msgIn())
   {
      if (Serial.read() == HEADER)
      {           
           if (msgIn()) (*t).msgType = Serial.read();
           int i = 0;
           while (msgIn() && i < 12)
            {
              (*t).msg[i] = Serial.read();
              Serial.print('r');
              Serial.print(i);
              Serial.print(' ');
              i++;
           }
           if (msgIn() && Serial.read() == TRAILER) (*t).valid = true;
           else (*t).valid = false;
        }
      continue;
   }
}
boolean rSerial::msgIn()
{
   if (Serial.available()) return true;
   else return false;
}

Maybe you msgIn() affects the recieved message, in such a way that calling it twice will work, but not calling it once?

Though it would've been more typical the other way around.

[edit]I did not think of actually scrolling down to the msgIn, I see it calls the available() and it should work.

What happens exactly? [when you say nothing happens] maybe the bug's not in the commented-out line, but in one of the other lines?[/edit]

I included MsgIn at the bottom of the code.......seemed pretty innocent to me.

Could serial.available() really cause that?

What is it that you want your code to do?

It will clear the buffer, if no a HEADER is read. Is this intentional?

[edit]And the 'continue;' won't do anything. Just cause another iteration of the while loop.[/edit]

It is intentional.

I’m integrating into an existing serial network, and I only want it to receive messages that are preceded by a HEADER byte.

The idea is that:
read the header bit, then receive the message and put it into the data structure reference that was passed in. IF the trailer bit is correct, then I have initial verification that be data may be correct.

Right now I can send repetitive headers to the arduino and the debug code in the loop won’t print.
[edit]
Continue Removed… no change
Totally removed debugging code, code not working now (when I readd the line in it works)[/edit]

I think you should remove the if (msgIn())*s just assume the message *is availabe.

[edit] Testing something... [/edit]

I think this does kind-of what you want?:

const char HEADER = ‘h’;
const char TRAILER = ‘t’;

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

void loop(){
receiveTemp();
}

void receiveTemp()
{
//if (msgIn()) Serial.print(“MsgIn”); //THE LINE IN QUESTION//
if (packetIn())
{
while(msgIn()){
if (Serial.read() == HEADER)
{
if (msgIn()){ Serial.print("Type: "); Serial.println( Serial.read() ); }
byte i = 0;
while (msgIn() && i < 12)
{
Serial.println( Serial.read() );
i++;
}
if (msgIn() && Serial.read() == TRAILER){ Serial.println(“Trailer true”); }
else { Serial.println(“Msg fail”); }
}
}
}
}

boolean msgIn()
{
if (Serial.available()) return true;
else return false;
}

boolean packetIn()
{
if (Serial.available()>=15) return true;
else return false;
}

dawerschk--

What is happening is that printing the debug message gives you just enough delay to allow the inbound message to fully arrive. When you delete the debug stuff, you enter the loop before the inbound message completely arrives. All the calls the msgIn() now fail, because the message is not there yet.

What I'd do is gate this function with something like:

if (Serial.available() >= MESSAGE_SIZE)
{
  ... // process message here
}

This way, you can be sure that an entire message has arrived and you don't have to do all those checks to MsgIn().

Mikal

Mikal,

Thank you so much!!!!

The arduino sure is a speedy little guy.

What I was kind of hoping to do was to create a sliding window.....

so I can get junk over the serial....but as soon as that HEADER comes along the arduino knows to look for a message.

Bottom line is that I'll make sure I've got enough bytes to play with!

Thanks again!

It should be noted that alphabeta was on the same track, and really went above and beyond by completely rewriting my code.

Thanks again to all that helped!

dawerschk