Go Down

Topic: function-definition is not allowed..? (Read 5394 times) previous topic - next topic

dnear1

Jun 12, 2008, 02:10 pm Last Edit: Jun 12, 2008, 02:11 pm by dnear1 Reason: 1
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: [Select]
//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

}

mem

#1
Jun 12, 2008, 02:42 pm Last Edit: Jun 12, 2008, 02:48 pm by mem Reason: 1
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: [Select]

//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 !!!
}


melka

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

but not sure
http://melka.one.free.fr/blog/
http://www.flickr.com/photos/melkaone/

dnear1

#3
Jun 13, 2008, 12:27 am Last Edit: Jun 13, 2008, 12:28 am by dnear1 Reason: 1
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: [Select]
//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
}



melka

yeah, that's a comment, but with a bracket before the comment starter ( // ) and no statement, so the bracket was a pb, i think
http://melka.one.free.fr/blog/
http://www.flickr.com/photos/melkaone/

Go Up