Convert incomming serial data to string for storage in variable in Arduino?

Good Day All,

I'm trying to put data being recieved from a 9DOF sensor (http://www.sparkfun.com/products/9623) thru a serial connection to a Arduino Mega 2560 into a variable so I can later write it to an SD Card.

The following code is contained in the void loop() {

  if(Serial1.available() > 0)
    Serial.print(Serial1.read(), BYTE);

How would I break up Serial.print(Serial1.read(), BYTE); so I can assign the data in (Serial1.read(), BYTE) to a variable?

Looking for something on the lines of

  if(Serial1.available() > 0)
    strData = (Serial1.read(), BYTE);
    Serial.print(strData)

But this does not reproduce the same results as

  if(Serial1.available() > 0)
    Serial.print(Serial1.read(), BYTE);

Any help with this would be appreciated.

Thanks Don Anderson

if(Serial1.available() > 0) {
    strData = Serial1.read();
    Serial.print(strData, BYTE);
}

Rob

I have another question related to this? Ok, that makes sense and I need to declare the variable.
I’m having an issue with getting the data to print out correctly with the code at the bottom.
Any ideas as to what I’m doing wrong?

Data printed out should look like this:

Initializing SD card…card initialized.
$,1,-64,207,380,380,376,-356,42,-742,#
$,2,-59,203,380,380,376,-337,91,-741,#
$,3,-61,208,380,380,376,-337,91,-741,#
$,2,-61,206,380,380,375,-337,91,-741,#
$,2,-60,205,380,381,376,-337,91,-741,#
$,2,-60,209,380,380,375,-337,91,-741,#

The above prints to the screen ok with the following code:

void loop() {
  if(Serial1.available() > 0) {
    //Serial.print(Serial1.read(), BYTE);
    intData = Serial1.read();
    Serial.print(intData, BYTE);
}

But When I reference intData later int the code to write to the sd card or print to the screen I get this with the following code which is wrong. You can see things start to repeat and blank lines are added. What can I do to get it to print correctly?

Initializing SD card…card initialized.

$,2,-31,215,379,380,375,-184,208,-673,#
$,3,-30,215,380,380,376,-199,199,-670,#
$,3,-32,213,380,381,376,-199,199,-670,#
$,2,-30,216,380,381,375,-199,199,-670,#
$,1,-29,216,380,380,376,-199,199,-670,#
$,2,-31,214,380,380,376,-199,199,-670,#
$,4,-31,213,380,380,375,-199,199,-670,#
$,2,-29,216,3880,3881,3776,-11877,2003,-66788,##

$,1,–300,2114,3880,3880,3776,-11877,2003,-66788,##

$$,2,–299,2113,3880,3881,3776,-1187,2003,-66788,##

$$,3,–300,2116,3880,3880,3776,-1887,2003,-66788,##

$$,3,–300,2113,3880,3881,3776,–1887,2003,-66788,##

$$,2,–311,2116,3880,3880,3776,–1887,2003,-66788,##

$$,2,–277,2118,3880,3880,3775,–1776,2004,-66766,##

$$,2,–300,2114,3880,3881,3776,–1776,2004,-66766,##

$$,2,–299,2115,3880,3880,3776,–1776,2004,-66766,##

The following code generates the results above:

/* 
   ***** Begin SD Card Code
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 51
 ** MISO - pin 50
 ** CLK - pin 52
 ** CS - pin 53
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 
 This example code is in the public domain.
  ***** End SD Card Code 
 */

// ***** BeginSD Card Code 
#include <SD.h>

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;
int intData = '0';
// ***** End SD Card Code 



/*
 ***** Begin 9DOF Code
Read 9DOF Razor IMU data via Seeeduino Mega board (Arduino Mega clone).

1. Connect the jumper wires between the two boards
   according to the following table:

9DOF Razor IMU | Seeeduino Mega
---------------|--------------------
TX0............|RX1..(pin 18).......
RX1............|TX1..(pin 19).......
3.3V...........|3V3.................
GND............|GND.................
---------------|--------------------

2. Connect the USB Cable between the PC and the Seeeduino Mega board.
3. Select the "Board" item from the "Tools" top menu and check the radio
   button Arduino Mega (ATmega1280). 
4. Upload this Sketch to the Seeeduino Mega board
5. Open Serial Monitor and set the Baud rate to 57600.
6. Send the character '0' to view the 9DOF Razor IMU Menu.

9DOF IMU Test Firmware v15
==========================
[1]Accelerometer: ADXL345
[2]Magnetometer: HMC5843
[3]Gyroscope: LPR530 and LY530
[4]Raw
[5]Self Test

 ***** End 9DOF Code
 */



void setup() {
  // ***** Begin 9DOF Code
  Serial.begin(57600);    // Used for both SD Card & 9DOF to print
  Serial1.begin(38400);  // Baud rate fixed.... (9DOF only)
  Serial1.print(4);      // Begin with Raw data (9DOF only)
  // ***** End 9DOF Code
  
  
  
  // ***** Begin SD Card Code 
   Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  // ***** End SD Card Code 
}





void loop() {
 
  // ***** Begin 9DOF Code
  if(Serial.available() > 0) {
    int incomingByte = Serial.read();
    /*
       DEC - BYTE
         0 - 48
         1 - 49
         2 - 50
         3 - 51
         4 - 52
         5 - 53
    */
    if(incomingByte >= 48 && incomingByte <= 53)
        Serial1.print(incomingByte, BYTE);
  }
  
  if(Serial1.available() > 0) {
    //Serial.print(Serial1.read(), BYTE);
    intData = Serial1.read();
    //Serial.print(intData, BYTE);
  }
   // ***** End 9DOF Code
   
   
   
// ***** Begin SD Card Code
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
//  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
//  if (dataFile) {
//    dataFile.println(intData);
//    dataFile.close();
    //print to the serial port too:
    Serial.print(intData, BYTE);
//  }  
   //if the file isn't open, pop up an error:
//  else {
//    Serial.println("error opening datalog.txt");
//  }   
 //***** End SD Card Code
   
}
    /*
       DEC - BYTE
         0 - 48
         1 - 49
         2 - 50
         3 - 51
         4 - 52
         5 - 53
    */
    if(incomingByte >= 48 && incomingByte <= 53)

You have some problem with this?

    if(incomingByte >= '0' && incomingByte <= '5')

The HardwareSerial::read() function returns an int. The low order byte contains the data, if there was data to read. The reason that the function needs to return an int, when the data size being read is byte, is because every value that fits in a byte is valid data to return, leaving no possibility to return an invalid value as an error condition.

If you always call HardwareSerial::available() before calling HardwareSerial::read(), the read function will never return an error condition, so the returned vale can be stored in a byte or char variable. Doing so will eliminate the need to add “,BYTE” to the HardwareSerial::print() function call. The HardwareSerial::print() function is overloaded. Printing an int with the BYTE argument may be causing the compiler to pick the wrong overload of the print function, causing the “corrupt” output that you see.