Pages: [1]   Go Down
Author Topic: How to add a char to a string?  (Read 5180 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm taking in data from a GPS module, but the data comes in one character at a time.
I want to take each character and add add it to a string, so that the string gets bigger and bigger until I am done collecting the data.

strcat(Latitude, DATAIN) doesn't work for me (if DataIn is the character)

So what can I do?

Just in case this helps:
Code:
#include <string.h>
 #include <ctype.h>
 int ledPin = 13;                  // LED test pin
 int rxPin = 0;                    // RX PIN
 int txPin = 1;                    // TX TX
 int byteGPS=-1;
 char data[300] = "";
 char GPRString[7] = "$GPRMC";
 int count=0;
 int ready=0;
 int counta=0;
 int info[13];
 
 char Latitude[50];
char Storage;
 
 void setup() {
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     data[i]=' ';
   }  
 }
 
 void loop() {
   getGPS();
 }
 
 void getGPS(){
   byteGPS=Serial.read();         // Read a byte of the serial port
   if (byteGPS == -1) {           // See if the port is empty yet
     delay(100);
   } else {
     data[counta]=byteGPS;        // If there is serial port data, it is put in the buffer
     counta++;                      
     if (byteGPS==13){            // If the received byte is = to 13, end of transmission
       count=0;
       ready=0;
       for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
         if (data[i]==GPRString[i-1]){
           ready++;
         }
       }
       if(ready==6){               // If yes, countinue and process the data
         for (int i=0;i<300;i++){
           if (data[i]==','){    // check for the position of the  "," separator
             info[count]=i;
             count++;
           }
           if (data[i]=='*'){    // ... and the "*"
             info[12]=i;
             count++;
           }
         }
         for (int i=0;i<12;i++){
           switch(i){
             case 0 :;break;
             case 1 :;break;
             case 2 :for (int j=info[i];j<(info[i+1]-1);j++){Storage = data[j+1]; strcat(Latitude,Storage);};break;
             case 3 :;break;
             case 4 :;break;
             case 5 :;break;
             case 6 :;break;
             case 7 :;break;
             case 8 :;break;
             case 9 :;break;
             case 10 :;break;
             case 11 :;break;
             case 12 :;break;
           }
         }
       }
       counta=0;                    // Reset the buffer
       for (int i=0;i<300;i++){    //  
         data[i]=' ';            
       }                
     }
   }
 }
Logged

Van Alstyne, TX
Offline Offline
Full Member
***
Karma: 1
Posts: 154
Haven't smoked an Arduino... yet.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First and foremost -- strcat() is for appending two strings, not adding a single character to the end of a string.   It would be best to simply
keep an index to the last character you appended and use that index
to point to the next place your data array can accept another char,
incrementing the index for each addition.

Also, strcat() only works with strings that are NUL terminated which
is something you are not doing.  Probably the easiest way to do this
is to initialize your data array in setup() and at the end of GetGPS()
to NUL's, not spaces.

You can use memset(data, 0, sizeof(data)); to do this for you.

Also, when you declare char data[300] -- there's no need to assign
an initial value of "" since data[] will be filled in by the setup() function.
« Last Edit: February 19, 2009, 02:05:50 am by Fe2o3Fish » Logged

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you really want to use 'strcat':

Code:
char str1[32];
char str2[2];

str2[0] = '!';  // Char to append to string
str2[1] = '\0';

strcpy (str1, "hello");
strcat (str1, str2);
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

C++ can fix this, implementing another interface for the strcat() function itself.

Code:
void strcat(char* original, char appended)
{
    while (*original++)
        ;
    *original++ = appended;
    if (appended)
        *original = '\0';
}

After defining this function in your sketch, feel free to strcat(myStringBuffer, 'A') to add the letter A to the end of myStringBuffer.
« Last Edit: February 19, 2009, 08:08:35 am by halley » Logged

Denver
Offline Offline
God Member
*****
Karma: 19
Posts: 778
Inactive - PM
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Out of my league a bit, but
strcat(myStringBuffer, "A")
is OK, right?


Logged

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll

Bristol, UK
Offline Offline
Edison Member
*
Karma: 0
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, because "A" is a string with one character in it, whereas 'A' is a character constant that is equivalent to the ASCII code for capital A.  Note the use of double and single quotes!  In memory, a constant like "A" will generate two bytes, one containing the ASCII code for A, and the other holding a zero byte to indicate end-of-string.  Hope that makes sense!
Logged

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

Thanks!
I didn't realize that 'A' and "A" are completely different.

Now I can know my speed no matter what!
Logged

Pages: [1]   Go Up
Jump to: