Common sample code using strtok() and xbees doesn't compile

This is concerning PaulS’s sample code for sending serial values with xbees, but the code

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

or any sketch using this framework like here or the solution here aren’t working anymore.

The errors from the sample code:

testxbeesend2:9: error: 'byte index' redeclared as different kind of symbol

 byte index;

      ^

In file included from c:\users\user\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\stdlib.h:11:0,

                 from C:\Users\User\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\cores\arduino/Arduino.h:24,

                 from sketch\testxbeesend2.ino.cpp:1:

c:\users\user\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\string.h:55:8: error: previous declaration of 'char* index(const char*, int)'

 char  *_EXFUN(index,(const char *, int));

        ^

C:\Users\User\Documents\Arduino\testxbeesend2\testxbeesend2.ino: In function 'void loop()':

testxbeesend2:25: error: assignment of function 'char* index(const char*, int)'

        index = 0;

              ^

testxbeesend2:25: error: cannot convert 'int' to 'char*(const char*, int)' in assignment

testxbeesend2:26: error: invalid types 'char [80][char*(const char*, int)]' for array subscript

        inData[index] = '\0';

                    ^

testxbeesend2:37: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

       if(index < 79)

                  ^

testxbeesend2:39: error: invalid types 'char [80][char*(const char*, int)]' for array subscript

         inData[index] = inChar;

                     ^

testxbeesend2:40: error: ISO C++ forbids incrementing a pointer of type 'char* (*)(const char*, int)' [-fpermissive]

         index++;

              ^

testxbeesend2:40: error: lvalue required as increment operand

testxbeesend2:41: error: invalid types 'char [80][char*(const char*, int)]' for array subscript

         inData[index] = '\0';

                     ^

testxbeesend2:56: error: assignment of function 'char* index(const char*, int)'

     index = 0;

           ^

testxbeesend2:56: error: cannot convert 'int' to 'char*(const char*, int)' in assignment

testxbeesend2:57: error: invalid types 'char [80][char*(const char*, int)]' for array subscript

     inData[index] = '\0';

                 ^

exit status 1
'byte index' redeclared as different kind of symbol

I’ll admit I’m not a professional so when the examples don’t work I’m totally helpless.

ytmnic:
This is concerning PaulS's sample code for sending serial values with xbees, but the code

That code compiled fine for me.

Also, have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The 3rd example is very similar to the code you posted but it has the code in functions that should make it easier to incorporate into another program.

...R

What IDE are you using? I tried 1.6.11 and 1.8.2. Also, did you include any libraries?

I'll have to run your example on my pc to see if it'll compile as well

I compiled YOUR code with IDE 1.6.3. I did not add anything.

My guess is that you have two nearly similar versions of the program and one of them has a mistake in it. Copy and paste the version from your Original Post.

...R

You'll have to give me some credit, I can copy and paste with the best of them. So when I copy from here, here and
here and none of them compile on either of my computers, I'm in trouble.

But somehow every example from your Serial Input Basics does compile, so I'll count myself as being helped.

Thank you!

ytmnic:
You'll have to give me some credit, I can copy and paste with the best of them

Sorry. It is not always the case with people asking questions here :slight_smile:

...R