A basic serial comms question

Hey all,

I am working on a program for an autopilot that reads data from a sparkfun 9dof razor. The problem I am having is a stupidly simple one I believe, but I cant seem to get things to work right. I need to read in the angles from the serial stream, into a String type variable. So essentially what it boils down to is how do I get the serial data into a character string and then into a String.

Any help would save me alot of headache,

Thanks

In a nutshell you read the characters until you have them all then use one of the atoi/atof() etc functions to convert the string into an int/float/whatever.

This is a very common request, have a search through the forum and you'll probably find a 1000 threads on it.

Meanwhile, what's the exact format of the data coming from the Razor?


Rob

Hey, thanks for the quick response. I have it covered from the point that the data is in a String object. I can parse it and convert it using atoi, gots all of that down. The problem i am having is that the data comes in as a pre arduino -0019 character array string and I am trying to get it into the newer String object so that I can more easily manipulate it. Once it is in the String I have the rest of the program working.

I never use the String class, it's a bit new-fangled for me plus I don't trust it to behave properly re allocating and freeing memory. But maybe that's me just being a control freak :slight_smile:

I would assume though that there is a constructor that takes a NULL-terminated array of char as the argument. So

char oldString[] = "xxx";
String newString = oldstring;

Should work I think.

Did you check the Arduino reference pages?


Rob

thanks rob, thats what I was thinking but im getting garbage out, maybe something else is set up wrong. I just found out about the String class today actually. some of the aspects of it are very well explained and some are not, this was one (at least in my research) that was not. There is a method of taking a String and turning it into a character array but not vice versa.

Dan

The data from the razor appears as !ANG:-xxx.xx,-xxx.xx,-xxx.xxX in a character string. (the uppercase X on the end is my simple end sign) Of course the data could be positive as well but negative I thought of as a catch all scenario. So what I want to do is do a serial read, put that into an array. make that array a String and parse it/convert it etc from there.

Here is what I had coded

char dataset[30]="";
char incoming=-1;
int counta=0;
int count=0;
int roll[5];
int pitch[5];
int yaw[5];
byte data1=-1;
String buff1="";
int commaposition;

void setup()
{
  Serial.begin(57600);
  Serial1.begin(57600);
}



void loop()
{
  count=0;
  if (Serial1.available())
  {
    incoming=Serial1.read();     // Read a byte of the serial port
   
    while (incoming!='X')
    {
       incoming=Serial1.read();     // Read a byte of the serial port
      dataset[count]=incoming;
      count++;
    }
    buff1=dataset;
    Serial.print(buff1);
   buff1="";
   count=0;
  
  }

(Moderator edit: Please use the code (#) icon on the editor's toolbar when posting code. Thanks. AWOL)

Now there are some initialization weirdnesses there like variables that arent used because I was going to build on this but I am getting nothing on the serial monitor through this.

This works

char oldString[] = "xxx";
char *strptr = oldString;
String newString = strptr;

void setup () {
   Serial.begin (115200);
}

void loop () {
    Serial.println (newString);
}

as does this

char oldString[] = "xxx";
String newString = &oldString[0];

void setup () {
   Serial.begin (115200);
}

void loop () {
    Serial.println (newString);
}

Now they are effectively the same thing as before so I thought I'd try

char oldString[] = "xxx";
String newString = oldString;

void setup () {
   Serial.begin (115200);
}

void loop () {
    Serial.println (newString);
}

And that works. So I'd say you had another problem.

There's another thread running at the moment with a similar theme, ie parsing a string

Have a read of that and see if it helps. You have the same issue where you want to chop a comma-delimited string into pieces and assign the pieces to variables.


Rob

You have the classic issue with reading the serial port - you check serial.available and if there is anything there, start reading until you get an 'X'. Problem is, serial comms are very slow, so what you will get is one character followed by a large number of -1s from serial.read while the next character is being transmitted. You almost certainly overflow your dataset array and stamp all over whatever is next to it in memory. Check that serial.available > length of string you're reading or better, check it before you read every character. This issue comes up often - search the forums for serial.available and you'll get a number of similar explanations.

thanks guys, il take a look at whether it is overflowing and see if I cant get somewhere on it.

Thanks
Dan

My Blog will show you everything you need to do.