Pages: [1]   Go Down
Author Topic: function-definition is not allowed..?  (Read 3556 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 107
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My program gives me this error..
In function 'void loop()':
error: a function-definition is not allowed here before '{' token

the line identifier is at the very last line in my code

what am I doing wrong?  see sample code
Code:
//serial handler
//dnear1
//june 12 2008
  char RX[13];
  char nextbyte;
  boolean lostbyte;

void setup() {
  Serial.begin(9600);
  lostbyte=false;
}

void loop() {
  if(Serial.available() > 0) {
    char temp;
    char numbytes;
  {//my protocol is going to be 20 bb cc (xx xx xx) cs
  //where bb is the number of bytes to be sent,
  //cc is s command
  //xx is any data being sent and
  //cs is the checksum
  do
  {
    if (lostbyte=true)//this may not be the best way to handle it..
    {
      temp=nextbyte;
      lostbyte=false;
      //numbytes=RX[13];
    }
    else
    {
    temp= Serial.read();
    };
  numbytes=RX[13];
  if(numbytes = 0)//is this first byte received in command?
  {
    if(temp = 20)//if so, is it a valid start byte?
    {
      RX[0]=temp;//store it as first byte
      RX[13]=1;//and increment valid byte counter
    };
  };
  if(numbytes = 1)//is this second byte received in command?
  {
    if(temp > 0)//if so, it is number of data bytes in preceding packet
    //it should be greater than zero
    {
      if(temp <=10)//it also should be less than 10.. I can always increase later if I need
      //larger packet to hold something
      {
    RX[1]=temp;//store it in second byte
    RX[13]++;//and increment valid byte counter
      }
      else//if data bytes value is not valid
      {
        RX[13]=0;//then incoming data packet is invalid, clear out and wait
      };
    }
    else
    {
     RX[13]=0;//data bytes value must be greater than zero... it isn't so clear out and wait
    };
  };
  if(numbytes = 2)//is this third byte received in command?
  {//this byte will always be data of some sort IE a command byte
    RX[2]=temp;//store it in third byte
    RX[13]++;//and increment valid byte counter
  };
  if(numbytes = 3)//is this fourth byte received in command?
  {//this byte will either be a command, data or checksum
    RX[3]= temp;//store it in fourth byte
    RX[13]++;//and increment valid byte counter
  };
  if(numbytes > 3)//is this fifth or consecutive byte in command?
  {
    if (RX[1]>(numbytes-3))//check the length of the packet
    {//if we expect more bytes than we currently have..
      RX[numbytes]=temp;//store it in nth byte;
      RX[13]++;//and increment valid byte counter
    }
    else
    {//if we are here then we have more bytes than packet calls for... save it for later and exit loop
      nextbyte=temp;//I need to take care of this byte somehow it may be important to next command
      lostbyte=true;//this is how I think I will handle it..
      break;
    };
  };
  } while (Serial.available() > 0);
  //now we need to check and/or process incoming commands if a complete command is found.
  //process incoming data

}
« Last Edit: June 12, 2008, 07:11:38 am by dnear1 » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code is difficult to debug because loop is too big to see the wood from the trees. One problem was that  the compiler was getting confused because there were some brackets in the wrong place .
I suggest you move the functionality to check the incoming data into a separate function as per the example below.

The code used a single equals sign to test for equality, in C you use a double equal sign.

if(a=b)  will always return true, it sets a equal to b
if(a== b)  is what you want.


Code:
//serial handler
//dnear1
//june 12 2008
char RX[13];
char nextbyte;
boolean lostbyte;

void setup() {
  Serial.begin(9600);
  lostbyte=false;
}

void checkBytes(int numbytes) {
 int temp;
   // code to check bytes needs to be added here
   // note that this could return a boolean value if you need to break out of your do loop as a result of calculations here
   // remember that a check for equality is: if(a==b)   not: if(a=b);
}


void loop() {
  if(Serial.available() > 0) {
    char temp;
    char numbytes;
    //my protocol is going to be 20 bb cc (xx xx xx) cs  <- you had a brace here !!
    //where bb is the number of bytes to be sent,
    //cc is s command
    //xx is any data being sent and
    //cs is the checksum
    do
    {
      if (lostbyte==true)//this may not be the best way to handle it..  <- this was: lostbyte=true!!
      {
        temp=nextbyte;
        lostbyte=false;
        //numbytes=RX[13];
      }
      else
      {
        temp= Serial.read();
      };
      numbytes=RX[13];
      checkBytes(numbytes);
    }
    while (Serial.available() > 0);
    //now we need to check and/or process incoming commands if a complete command is found.
    //process incoming data
  } // <- this was missing !!!
}
« Last Edit: June 12, 2008, 07:48:10 am by mem » Logged

Grenoble/Lyon - France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 363
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd say it's this line
  {//my protocol is going to be 20 bb cc (xx xx xx) cs

but not sure
Logged


0
Offline Offline
Full Member
***
Karma: 0
Posts: 107
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That line was merely comment.

Thanks for the hint about if statement needs == not =.  hard to retrain myself after years of VB use..

It seems I was missing a } bracket.. after I reorganized and noticed one missing the problem disappeared..

Code:
//test run serial handler
  char RX[13];
  char nextbyte;
  boolean lostbyte;

void setup() {
  Serial.begin(9600);
  lostbyte=false;
}
void checkrx(){
  //my protocol is going to be 20 bb cc (xx xx xx) cs
  //where bb is the number of bytes to be sent,
  //cc is s command
  //xx is any data being sent and
  //cs is the checksum
    char temp;
    char numbytes;
    do
  {
    if (lostbyte==true)//this may not be the best way to handle it..
    {
      temp=nextbyte;
      lostbyte=false;
      //numbytes=RX[13];
    }
    else
    {
    temp= Serial.read();
    };
  numbytes=RX[13];
  if(numbytes == 0)//is this first byte received in command?
  {
    if(temp == 20)//if so, is it a valid start byte?
    {
      RX[0]=temp;//store it as first byte
      RX[13]=1;//and increment valid byte counter
    };
  };
  if(numbytes == 1)//is this second byte received in command?
  {
    if(temp > 0)//if so, it is number of data bytes in preceding packet
    //it should be greater than zero
    {
      if(temp <=10)//it also should be less than 10.. I can always increase later if I need
      //larger packet to hold something
      {
    RX[1]=temp;//store it in second byte
    RX[13]++;//and increment valid byte counter
      }
      else//if data bytes value is not valid
      {
        RX[13]=0;//then incoming data packet is invalid, clear out and wait
      };
    }
    else
    {
     RX[13]=0;//data bytes value must be greater than zero... it isn't so clear out and wait
    };
  };
  if(numbytes == 2)//is this third byte received in command?
  {//this byte will always be data of some sort IE a command byte
    RX[2]=temp;//store it in third byte
    RX[13]++;//and increment valid byte counter
  };
  if(numbytes == 3)//is this fourth byte received in command?
  {//this byte will either be a command, data or checksum
    RX[3]= temp;//store it in fourth byte
    RX[13]++;//and increment valid byte counter
  };
  if(numbytes > 3)//is this fifth or consecutive byte in command?
  {
    if (RX[1]>(numbytes-3))//check the length of the packet
    {//if we expect more bytes than we currently have..
      RX[numbytes]=temp;//store it in nth byte;
      RX[13]++;//and increment valid byte counter
    }
    else
    {//if we are here then we have more bytes than packet calls for... save it for later and exit loop
      nextbyte=temp;//I need to take care of this byte somehow it may be important to next command
      lostbyte=true;//this is how I think I will handle it..
      break;
    };
  };
  } while (Serial.available() > 0);
 
}
void loop() {
  if(Serial.available() > 0)
  {
  checkrx;
  };
  //now we need to check and/or process incoming commands if a complete command is found.
  //process incoming data
}


« Last Edit: June 12, 2008, 05:28:47 pm by dnear1 » Logged

Grenoble/Lyon - France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 363
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

yeah, that's a comment, but with a bracket before the comment starter ( // ) and no statement, so the bracket was a pb, i think
Logged


Pages: [1]   Go Up
Jump to: