Reading Serial into a string

Im trying to a serial.read() into a char string[20] but im getting errors… because its reading int’s not strings? error im getting is

error: incompatible types in assignment of ‘int’ to ‘char [20]’

this is the bit getting stored in a database…

struct Username {
        uint8_t Usernumber;
        char Firstname[20];
        char Surname[20];
        char Welcome[100];
}
Username;

And this is what i had in mind to read the data from the terminal… i know its wrong… but not sure where to start

Sorry for not posting the whole code… its upto 500lines and 15 files now :frowning: and I’ve barely even started yet

Serial.print("Enter Users first name 20 char max: ");
while(!Serial.available());
Username.Firstname = Serial.read();
Serial.println();

Serial.print("Enter Users Surname 20 char max: ");
while(!serial.available());
Username.Surname = Serial.read();
Serial.println();

Serial.print("Enter a Welcome Message 100 char max: ");
while(!serial.available());
Username.Welcome = Serial.read();
Serial.println();

Serial.read() returns a Char, that is a single Character. A sample of the code that I tend to use is below…

Please bear in mind that this will not compile in itself… It is just a snippit.

Darryl

    static char inData[51]; // Allocate some space for the string
    static char inChar=-1; // Where to store the character read
    static int pos= 0; // Index into array; where to store the character

    while (Serial1.available()){
    if (pos < 45){
        inChar = Serial1.read();
        inData[pos] = inChar;
        pos++;
        inData[pos] = 0;
        if (inChar == 0x0A || inChar == 0x0D){
            // End of line... Process things here...

            pos = 0;
            inData[pos] = 0;
        }
    } else {
        pos = 0;
        inData[pos] = 0;
    }
}

Thats exactly what i was after! Thanks for the fast reply

@vk2tds Im guessing your into radio stuff? Been working on a little side project for my old man, a tuner for a mag loop antenna. basically just a stepper on a VVC at the moment with tune up and tune down buttons. might have a look at putting a spark on it to one day and some kind of antenna analyzer so its self tuning all you have to do is enter a frequency

1 Like

Glad I could help… I was doing some programming on the spark and just needed a little distraction. I am still coming up to speed on the Spark myself… and as the textbook in my bookcase says, I have “C as a second language”. These days I don’t do enough programming to stay up to speed with any language.

And I guess you could call me a radio geek… Amongst other bits and pieces.

Darryl VK2TDS

So that is working perfect!

Next question… reading a number… i thought it would be easier… if i type 2 into the terminal it gives me 50… so its ascii equivalent of the char??

How would i make ‘c’ be the right number… you know what i mean?

Serial.println("Select user from List:");
while(!Serial.available());
int c = Serial.read();
Serial.print("Updating User number:"); Serial.println(c);
if ( c >= 0 && c <= Usernamecount) {
        if (SD.exists(UsernameFile)) {
            dbFile = SD.open(UsernameFile, FILE_WRITE);
            if (dbFile) {  
               db.open(0);
               db.updateRec(c, EDB_REC Username);
               dbFile.close();
            }
            
        }

Normally when you enter numbers they are more than one digit long, so you would need to look at the numbers in a string. There is a function called atoi, and another called atof. That is Ascii to Integer, and Ascii to Float. Google them. You could use sscanf but it will take up more flash, or at least did in my case.

Might be a good idea to find a good resource for ‘C’ string processing. I actually refer back to the manual that came with Microsoft Quick C back in 1990 occasionally, as the manual was excellent whilst Quick C was horrid!

Darryl

weirdly, atof() is ascii to double and atoff() is to float

1 Like

Awesome… I’ll give it a go I normally do lots of searching on this forum and arduino forum… but it’s hard when you don’t understand what your searching for. I came up with some going the other way but not the way I needed.

Thanks again

Yup! I’ve been through that same frustration. However the docs are now improved somewhat: What I have been led to believe is a fairly complete and accurate list of functions available to you is newly referenced at http://docs.spark.io/firmware/#other-functions and at http://docs.spark.io/firmware/#other-functions-math where you will find links to the Newlib docs at https://sourceware.org/newlib/libc.html and https://sourceware.org/newlib/libm.html - apparently Newlib is one of the libs automatically linked to.

3 Likes

I’ll have a look at those tomorrow when my eyes aren’t so square!

For now I have made it work with a very simple atoi thingy… This fingerprint and rfid project is really coming together now… All I need to do is put everything together… And maybe a lot of optimisation as my code is terrible but it works. Then maybe look at security and see if I can make it better in any way.