SerialCom

Hoping this is the place to post this wuestion im not positive but Is it possible to pass an array from computer to arduino board with info from a program in a way that it can then use the bits of info to do various things. Basically what im asking is how to send the array (I am currently just using the VisualC++ example from: Arduino Playground - HomePage and just trying various things to send it with the Write function)

Thanks

just send the array as a string with a start and a stop delimiter and a field separator, e.g. the array { 1,2,3,4,5 } can be send as “{1,2,3,4,5}”
{ = start delimiter
, is field delimiter
} is stop delimiter (end of array)

another option is is to send length, field* e.g. “5,1,2,3,4,5” for the same string as above however this latter method is less robust for missing digits.

If the array contains other datatypes similar schemes can be used.

Rob

so...could i do this:

this->serialPort1->Write("{Colours[0],Colours[1],Colours[2]}");

this->serialPort1->Write("{Colours[0],Colours[1],Colours[2]}");

Technically that would sort of work but you’re sending a heck of a lot of characters for no reason and you’re not actually indexing into the array so the numbers are hardcoded. Something more like

this->serialPort1->Write("{",Colours[0],",",Colours[1],",",Colours[2],"}");

would be better, depending on how Write() works that will send the send characters in a manner close to what Rob suggested. Say

{23,34,56}

Then you write some receiving code that waits for a “{” and parses the byte stream until you get a “}”.

How does Write() work. does

this->serialPort1->Write(123);

send 123 as a binary number or ‘1’ ‘2’ ‘3’ as three characters.


Rob

Ok,its not iking that exact thing but ill fiddle with it to see if it helps. When im using Serial.read() will it return a string?

When im using Serial.read() will it return a string?

Serial.read returns a single character, it's up to you to accumulate them into an array and work on that (usually when you have all the characters).

This is covered al least 5 times a week on the forum so have a search around (having said that I can't find one example, try searching for "delimiter").

If you have no luck one of us will have some example code somewhere.


Rob

Ok,its not iking that exact thing but ill fiddle with it to see if it helps.

It doesn't like "it" because the Write() method is expecting one argument, and Nick's example supplied 7 arguments.

this->serialPort1->Write("{");
this->serialPort1->Write(Colours[0]);
this->serialPort1->Write(",");
this->serialPort1->Write(Colours[1]);
this->serialPort1->Write(",");
this->serialPort1->Write(Colours[2]);
this->serialPort1->Write("}");

And, of course, now you see that this could be done in a loop to write out any number of values in an array.

Ok sending is now working how am i recieving now..? Any chang eof an eg?

This code will read all the serial data, between the { and the }.

char inData[64];
byte index = 0;
bool started = false;
bool ended = false;

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == '{') // Did we get a {?
    {
       started = true;
       index = 0;
       inData[index] = '\0';
    }
    else if(inChar == '}')
    {
      ended = true;
      break;
    }
    else
    {
       if(started && index < 64)
       {
         inData[index++] = inChar;
         inData[index] = '\0';
       }
    }
  }

  if(started && ended)
  {
    // Parse the data and use it

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

Where the Parse the data comment is, you can use strtok() to get each token (number as a string) and pass it to atoi() to convert it back to an int, and store it somewhere.