Pages: [1]   Go Down
Author Topic: Commenting out debugging causes code to not work..  (Read 1178 times)
0 Members and 1 Guest are viewing this topic.
midwest US
Offline Offline
Newbie
*
Karma: 0
Posts: 17
I void warranties
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Code:
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;
}
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: April 25, 2009, 05:16:13 pm by AlphaBeta » Logged

midwest US
Offline Offline
Newbie
*
Karma: 0
Posts: 17
I void warranties
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Could serial.available() really cause that?
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: April 25, 2009, 05:22:34 pm by AlphaBeta » Logged

midwest US
Offline Offline
Newbie
*
Karma: 0
Posts: 17
I void warranties
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]
« Last Edit: April 25, 2009, 05:38:33 pm by dawerschk » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

[edit]
Testing something...
[/edit]
« Last Edit: April 25, 2009, 05:46:55 pm by AlphaBeta » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote

 
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;
}

 
« Last Edit: April 25, 2009, 06:21:06 pm by AlphaBeta » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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
« Last Edit: April 25, 2009, 06:17:09 pm by mikalhart » Logged

midwest US
Offline Offline
Newbie
*
Karma: 0
Posts: 17
I void warranties
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

midwest US
Offline Offline
Newbie
*
Karma: 0
Posts: 17
I void warranties
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: