Moving Arrays to Variables?

I'm trying to use CmdMessenger to transfer data over the serial bus between 2 Arduino's, one of which is a Energy Monitor, the other is a ethernet enabled board for uploading data to Pachube. The data will flow just one way, from the Energy Monitor to the Ethernet board, and will take to form of 4,3212,4334,1212,1248; (the leading '4' is a command informing that data is forthcoming. The ',' are data separators, and the ';' is a terminator. The sketch is here http://bit.ly/nSVp0p and the processing section is highlighted.

I can get the sketch to run, but what I want to do is to attach the received values to variables in the Ethernet board, so mainPower has the value 3212, solarPower has the value of 4334, etc. but so have not managed to do so. Any help would be appreciated.

If it helps, the sketch's author has said 'The key to understanding this is to see that the function call cmdMessenger.available() points to the next field in the message. And this will loop around in the while() loop for each successive field. As delimited by your chosen field separator character (and ASCII comma ',' by default).'

pauldreed:     : char buf[350] = { '\0' };      :    char buf[350] = { '\0' };       :      char decode_buf[350] = { '\0' };       :

That's a good deal of memory tied up in those automatics.

gardner:

pauldreed: ```      : char buf[350] = { '\0' };       :     char buf[350] = { '\0' };        :       char decode_buf[350] = { '\0' };        : /code] [/quote]

That's a good deal of memory tied up in those automatics.


As I am using simple ASCI data, I will delete out this section, just keep the highlighted section.

I have spent the last few hours trying to work this out, and have come up with this;

void bens_msg()
{
  cmdMessenger.sendCmd(kACK,"command recieved");
  mcount = 0;
  while ( cmdMessenger.available() )
  {
    char buf[350] = { '\0' };
    cmdMessenger.copyString(buf, 350);
    delay (1000); //p

    mcount = mcount+1 ;     tvalue = atoi(buf);     if (mcount == 1) {       kw = (tvalue); }       else if (mcount == 2) {         v = (tvalue); }         else if (mcount == 3) {           a = (tvalue); }         if(buf[0])       cmdMessenger.sendCmd(kACK, buf);   } }



It seems to work, but is it the right way to deal with this issue?

I'm not sure if theres a better way but I would always just use the indexOf function and a few other String functions to parse the data and I usually just put it into an array not a bunch of independent variables. This will work fine as long as you send the same amount of data. Check out the String reference page. Once you have the various indexes of your reference points (i.e. 4 , and ;) then you can parse it pretty easy by using the between indexes and assigning them to a variable or array. I hope this makes sense

It looks like cmdMessenger.available() is designed to step through the comma-separated payload contents using strtok(). Although it does use an uninitialized local 'temppointer' the second time through. So what you're going to do is to take each one in turn, convert it to a number, and then put it where you want it.

Based on assumptions, here's one potential solution:

void bens_msg()
{
  int seq;
  long value;

  // Message data is any ASCII bytes (0-255 value). But can't contain the field
  // separator, command separator chars you decide (eg ',' and ';')
  cmdMessenger.sendCmd(kACK,"bens cmd recieved");


  for (seq = 0; cmdMessenger.available(); seq ++ )
  {
    char buf[64] = { '\0' };
    cmdMessenger.copyString(buf, 350);
    if(buf[0])
      cmdMessenger.sendCmd(kACK, buf);

    value = strtol(buf, NULL, 10);
    switch (seq) {
         case 1: // the first value is the foobar variable
               foobar = value;
               break

               :

         case 3: // the third value is the frobnitz variable
               frobnits = value;
               break

    }
  }
}

gardner: It looks like cmdMessenger.available() is designed to step through the comma-separated payload contents using strtok(). Although it does use an uninitialized local 'temppointer' the second time through. So what you're going to do is to take each one in turn, convert it to a number, and then put it where you want it.

Based on assumptions, here's one potential solution:

void bens_msg()
{
  int seq;
  long value;

 // Message data is any ASCII bytes (0-255 value). But can't contain the field  // separator, command separator chars you decide (eg ',' and ';')  cmdMessenger.sendCmd(kACK,"bens cmd recieved");

 for (seq = 0; cmdMessenger.available(); seq ++ )  {    char buf[64] = { '\0' };    cmdMessenger.copyString(buf, 350);    if(buf[0])      cmdMessenger.sendCmd(kACK, buf);

   value = strtol(buf, NULL, 10);    switch (seq) {         case 1: // the first value is the foobar variable               foobar = value;               break

              :

        case 3: // the third value is the frobnitz variable               frobnits = value;               break

   }  } }

Although it compiles, I can't get this to load the variables. Does it work for you?

hey, hope you'll get this / it'll be helpful to someone:

void bens_msg()
{
  int seq;
  long value;

  // Message data is any ASCII bytes (0-255 value). But can't contain the field
  // separator, command separator chars you decide (eg ',' and ';')
  cmdMessenger.sendCmd(kACK,"bens cmd recieved");


  for (seq = 0; cmdMessenger.available(); seq ++ )
  {
    char buf[64] = { '\0' };
    cmdMessenger.copyString(buf, 350);
    if(buf[0])
      cmdMessenger.sendCmd(kACK, buf);

    value = strtol(buf, NULL, 10);
    switch (seq) {

         case 0:
               red = value;
               break;

         case 1:
               green = value;
               break;
         case 2:
               blue = value;
               break;

    }
  }
}