Pages: [1]   Go Down
Author Topic: SerialCom  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Im more of a php person...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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: http://arduino.cc/playground/Interfacing/CPPWindows  and just trying various things to send it with the Write function)

Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 7
Im more of a php person...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so...could i do this:

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8525
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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

Code:
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


« Last Edit: August 01, 2011, 07:19:38 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 7
Im more of a php person...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8525
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
Im more of a php person...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49385
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code will read all the serial data, between the { and the }.
Code:
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.
Logged

Pages: [1]   Go Up
Jump to: