Pages: [1]   Go Down
Author Topic: Reading from serial and converting ISSUE?  (Read 298 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   ****UPDATE****
  this has been solved LOOK at PaulS response.



Hello fellow programmers, I am a n00b and think I'm doing something wrong. smiley-eek-blue  So I am issuing this challenge: I challenge you to create a program for an arduino (if that wasn't already clear) that reads a string from serial like "14,90", "2,20", etc and split and convert it into an int array. All help is very appreciated.  I thank you very much in advance. You may ask questions in a quote to this thread or in a post if you wish.


P.S. Don't tell me not to do a challenge and just ask for help.
« Last Edit: January 12, 2012, 07:34:35 am by albrufsky » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Most people aren't just going to do your work for you.  From my experience you need to put forth some effort, show and explain what you've done, and then explain at what step you become unsure.

Just some advice if you actually expect useful responses.
Logged

Offline Offline
Edison Member
*
Karma: 35
Posts: 1424
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yup, what MapleYamCakes said.

Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh definately I totally understand its just that I followed many guides and tutorials on doing this and not  one has worked for me so I was wondering if there was anything that worked for someone else.
I guess I was trying to think a little more creatively than logically.
I am sorry for the misunderstanding.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well post what you have, and what you're having trouble with, and I'm sure people will be glad to help!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for helping me out there. I will post that up when I'm near my computer next!



P.S. I'm sending this from a phone.
Logged

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

I've posted code to do this many times. Just in case you've not seen it, the code to receive the data looks like:
Code:
#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';
  }
}
Send "<14,90>", and inData will contain "14,90". The strtok() and atoi() functions can then parse the tokens ("14" and "90" and convert them to ints (14 and 90). Exactly how to use them is your part of our homework.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you so much paulS!!!!!! Also, I think it strange that I was looking around the forum quite a while and still did not find this code.
Logged

Pages: [1]   Go Up
Jump to: