Unable to Split Serial Data to Character Arrays - Please help


hi,

i trying split serial data different character arrays , print, no success.


following serial data:


code: [select]

@$25.99*$9.99*omega t-shirt (xxl)*omega101*#


character '@' starting character.
character '#' ending character.

character '*' separator. trying split above in 4 character arrays. following program.

code: [select]


int seperatorcount = 1;

char receiveddata;

char r_price[10];
char o_price[10];
char product_name[40];
char sku[15];

void setup()

{
  serial.begin(9600);
  delay(500);
}

void letsdisplay()
{
  serial.println(r_price);
  serial.println(o_price);
  serial.println(product_name);
  serial.print('(');
  serial.print(sku);
  serial.println(')');
}


void loop()

{

  int i,j,k,l = 0;

  while (serial.available() > 0)
  {
    receiveddata = serial.read();
    if (receiveddata == '@')
      serial.println("\n___________________________\n transmission begins...");
    else if (receiveddata == '#')
      letsdisplay();
    else
    {
      switch (seperatorcount)
      {
        case 1:
          if (receiveddata == '*')
            seperatorcount = seperatorcount + 1;
          else
            r_price[i++] = receiveddata;
          break;
        case 2:
          if (receiveddata == '*')
            seperatorcount = seperatorcount + 1;
          else
            o_price[j++] = receiveddata;
          break;
        case 3:
          if (receiveddata == '*')
            seperatorcount = seperatorcount + 1;
          else
            product_name[k++] = receiveddata;
          break;
        case 4:
          if (receiveddata == '*')
            seperatorcount = 1;
          else
            sku[l++] = receiveddata;
          break;
      }
    }


  }

}



the above program prints:

code: [select]



    ( <--empty character )
9
)
(1)




my requirements is:

code: [select]


$25.99
$9.99
omega t-shirt (xxl)
omega101






so, curiosity is:

  • why printing last charter o_price, product_name , sku and, empty character r_price.
  • and why not printing full string.


i tried end character arrays assigning '\0' character, if so, printed empty.

but if printing serially without splitting below, works:

code: [select]


void loop()

{
  while (serial.available() > 0)
    {
    receiveddata = serial.read();
    if((receiveddata == '@') || (receiveddata == '*') || (receiveddata == '#'))
      serial.print("\n");
    else
     serial.print(receiveddata);
    }
}





please me on this.


thanks.

in 'loop()', change this:-
code: [select]
int i,j,k,l = 0;
to this:-
code: [select]
static int i=0, j=0, k=0, l = 0;

when that, output in serial monitor:-
code: [select]
___________________________
 transmission begins...
$25.99
$9.99
omega t-shirt (xxl)
(omega101)


Arduino Forum > Using Arduino > Programming Questions > Unable to Split Serial Data to Character Arrays - Please help


arduino

Comments

Popular posts from this blog

Help with Missing Filename Error?

more info - Joomla! Forum - community, help and support

Using phone battery to power a plane.