Pages: [1]   Go Down
Author Topic: A basic serial comms question  (Read 660 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

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
« Last Edit: July 19, 2011, 11:37:28 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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 smiley

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
 
 
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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.
« Last Edit: July 20, 2011, 01:44:23 am by AWOL » Logged

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

This works

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

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

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

as does this

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

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

http://arduino.cc/forum/index.php?topic=66981.new;topicseen#new

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
 

Logged

Rob Gray aka the GRAYnomad www.robgray.com

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3726
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Thanks
Dan
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

My Blog will show you everything you need to do.
Logged


Pages: [1]   Go Up
Jump to: