splitting ASCII string to variables


i receiving ascii string arduimu on serial array, , using atoi function convert integer.
i edited output format !ang:-11.35,-8.51,14.78 (message preamble,roll, pitch,yaw). edited following code former post in forum .the code seems work fine if string data set "string" in initialized array but  fails if read stuff char array.
code: [select]
void setup() {
  // initialize both serial ports:
  serial.begin(38400);
  serial1.begin(38400);
}

void loop() {
  char arr[30];
  // read port 1, send port 0:
  if (serial1.available()) {
    char arr[30] ={ serial1.read()};
     serial.write(arr);
  }
   
 
    char str[30];
    float roll, pitch, yaw;
    int errors = 0;
   
    strcpy(str, arr);

    // first throwaway unless want strcmp "!ang" or such thing
    char *chpt = strtok(str, ":");
    if (chpt == null) {
        serial.println("first strok returns null");
        ++errors;
    }

    if (errors == 0) {
        chpt = strtok(null, ",");
        if (chpt == null) {
            serial.println("second strok returns null");
            ++errors;
        }
        else {
            roll = atof(chpt);
        }
    }

    if (errors == 0) {
        chpt = strtok(null, ",");
        if (chpt == null) {
            serial.println("third  strok returns null");
            ++errors;
        }
        else {
            pitch = atof(chpt);
        }
    }

    if (errors == 0) {
        chpt = strtok(null, ",\r\n");
        if (chpt == null) {
            serial.println("fourth strok returns null");
            ++errors;
            // input error: handle it.
        }
        yaw = atof(chpt);
    }
    if (errors == 0) {
        serial.print("(");
        serial.print(roll);
        serial.print(", ");
        serial.print(pitch);
        serial.print(", ");
        serial.print(yaw);
        serial.println(")");
    }
}


output:
!first strok returns null
afirst strok returns null
nfirst strok returns null
gfirst strok returns null
:first strok returns null
1first strok returns null
.first strok returns null
0first strok returns null
8first strok returns null
,first strok returns null
-first strok returns null
0first strok returns null
.first strok returns null
1first strok returns null
3first strok returns null
,first strok returns null
3first strok returns null
5first strok returns null
.first strok returns null
8first strok returns null
4first strok returns null

as can see serial data received printed vertically first letter of each output line.
however when run first serial.read , serial.write commands alone putting else comment. output follows

!ang:1.01,0.09,34.84
!ang:1.02,0.17,34.84
!ang:1.37,0.25,34.85
!ang:1.08,0.47,34.85
!ang:1.49,0.36,34.85
!ang:1.13,0.08,34.85
!ang:1.38,0.14,34.85
!ang:1.00,0.17,34.84
!ang:1.34,0.20,34.83
!ang:1.35,0.15,34.86
!ang:1.27,0.30,34.85
!ang:1.30,0.46,34.85
!ang:1.43,0.04,34.87
!ang:1.13,0.30,34.87

help me please cant understand why code doesn't work. code works if value of string arr[30] given as
char arr[30] = {
    "!ang:-11.35,-8.51,14.78"
};
and not received in serial. =(
is there easy way variables string received

code: [select]
 char arr[30];
 // read port 1, send port 0:
 if (serial1.available()) {
   char arr[30] ={ serial1.read()};

this little snippet of code declares 2 arrays of same name. not want do.

tell me, hard understand serial1.read() returns 1 character?

code: [select]
   strcpy(str, arr);
why? there no reason copy array anywhere. arr copying, anyway?

code: [select]
   char *chpt = strtok(str, ":");
   if (chpt == null) {
       serial.println("first strok returns null");

would idea if message printed matched function call. there no call strok.

the str functions, strcpy(), strtok(), etc. expect strings arguments. providing not strings.

a string null terminated array of chars. have array of chars not null terminated. therefore, not string. therefore, can not pass strcpy() or strtok().

if have control on sender, make shape , sent delimited data. "!ang:1.01,0.09,34.84" should "<!ang:1.01,0.09,34.84>". can use code this:
code: [select]

#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 serial data available, fast 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';
      }
    }
  }

  // here either because pending serial
  // data has been read or because end of
  // packet marker arrived. it?
  if(started && ended)
  {
    // end of packet marker arrived. process packet

    // reset next packet
    started = false;
    ended = false;
    index = 0;
    indata[index] = '\0';
  }
}

to receive serial data. says "process packet" put code call strtok() null terminated string, indata.


Arduino Forum > Using Arduino > Programming Questions > splitting ASCII string to variables


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial