Serial & arrays?

Hello all,

I am attempting to figure out how to send some data from the computer to the arduino.

The data would consist of 3 parts (channel, binary state, event code) ie. (11, 0, 1) (channel 11, state off, event code 1)

I am looked on the forum, and the web. I see this is certianly a way to handle this within the arduino. But it seems that their are many different ways to do it, and I am very unsure of which one, is correct, or standard, etc.
Perhaps someone has an easy library which makes this a cakewalk?

So,
What is the standard method to take some serial data and put it into a small array.
on the flip side of this, how would one go about formatting the data I wish to send, in the arduino IDE?

Any help is apprediated.
Thank You

What is the standard method to take some serial data and put it into a small array.

I use code like this, to deal with data coming in like "<11, 0, 1>":

#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';
  }
}

Where it says "Process the packet", strtok() and atoi() might be useful.

on the flip side of this, how would one go about formatting the data I wish to send, in the arduino IDE?

The IDE deos not send data to the Arduino. Perhaps you mean the Serial Monitor?

Thank you for your reply,

Your code is nice and clear, easy to follow, and informative.
And yes I did mean the serial monitor, thanks for the correction.

Let me ask you this.
Because I am not clear on how the data types play with each other.

How will the array being a char (I see why you need it to be a char (< & >)) , affect my use of the incoming data if I intend to use as an int.
Which I have pre-existing code making use of such values as int's.

or will it not matter?

Serial only carries "char" sized entities.
Ifv you want to store these in an "int", then that's up to you, but you'll be wasting half your memory.

AWOL,

Oh I see, Well Its not like I am lacking storage space (@17%). So I guess thats ok.

So I attempted to implement the code

The only change I made was to add the following code in the last section

 if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    
[glow=yellow,2,300]    
    currentState[inData[0]] = inData[1];
    eventState = inData[2];
[/glow]

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

Which mearly writes the incoming data to the approperiate channel in the array

Where the inData[0] should be the channel 1-8 in this case
inData[1] & inData[2] should be either 0 or 1
that portion of code is exactly the same as when i did something similar with reading an I2C data array.

Except in this case it did not actually update my currentState[] array. when I send the <1,1,0> command with serial monitor. (channel 1, state ON, event code 0)

What could be the problem?

Are you sending binary values, or ASCII decimal digits?

That could be your problem.

Perhaps you missed this bit:

Where it says "Process the packet", strtok() and atoi() might be useful.

Alright, I have looked into the atoi() and strtok() functions and if i am understanding what it does... I modified my code to the below

if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    
    currentState[atoi(inData[0])] = atoi(inData[1]);
    eventState = atoi(inData[2]);

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

And it has an "invalid conversion from char to const char" error

In researching that error, so i added some of this stuff into the code
http://forum.arduino.cc/index.php/topic,43990.0.html
Which has all sorts of fun things I really dont understand at all, but I tried the below.

Changing

char inChar = Serial.read();

to

char*inChar = &Serial.read;

It becomes readily apparent that my knowledge base in this particular area is severly limited, and i am accomplishing nothing by banging my head against the keyboard.

Could someone give an example that relates to my particular situation?

Begining with a serial input of form <###,#,#> or <##,#,#> or <#,#,#>
and ending with ints in the array {int1,int2,int3} or 3 seperate ints int1, int2, int3.

and if i am understanding what it does

Clearly, you aren't.

Begining with a serial input of form <###,#,#> or <##,#,#> or <#,#,#>

You end up with "###,#,#" or "##,#,#" or "#,#,#" in inData. That could change, in the future, to "#,##,####". The number of digits in each token does not matter. What you need to do is call strtok() to get each token, and then convert each token to a value,using atoi().

Just for giggles, lets suppose that inData contains "238,25,479".

char *token1 = strtok(inData, ",");

token1 will point to "238".

int num1 = atoi(token1);

num1 will contain 238.

char token2 = strtok(NULL, ","): // Keep parsing same string

token2 will contain "25".

And, I'm guessing that you can see how to convert token2 to an int and how to get token3 and convert it to an int.

Thank you all for the help, I got it working using your suggestions and comments.

Cheers