Go Down

Topic: Reading from serial and converting ISSUE? (Read 376 times) previous topic - next topic

albrufsky

Jan 12, 2012, 01:04 am Last Edit: Jan 12, 2012, 01:34 pm by albrufsky Reason: 1
  ****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.

MapleYamCakes

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.

el_supremo


albrufsky

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.

MapleYamCakes

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

albrufsky

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.

PaulS

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: [Select]

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

albrufsky

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.

Go Up