My Ticker won't work with an fprintf.

25 Nov 2009

I've cut my non working program down to a bare minimum.

I have a function that open a file on an SD Card, appends to it and closes it.

If I cut the code out of the function and put it in the main loop, it works,

If I make it a function and call that from the main loop, it works.

If I attach it to a ticker, and ask it to tick every 2 secs for instance.

It ticks once, then my mbed hangs.. (not even the LED1 in my main loop which should flash, flashes)

It doesn't do the strobing lights of death either, it just stops.

any ideas ?

I've published the code here..

http://mbed.org/users/DaveStyles/programs/16mhst

any help gratefully received as always.

PS. please don't laugh at the way I'm writing to the file, I'll tidy when I get a mo.. and at least that works as I want it to. (it writes time elapsed from booting mbed up)

 

25 Nov 2009 . Edited: 25 Nov 2009

Hi David,

Just a note, I didn't have an SD Card, so just changed SDFileSystem sd(...) to LocalFileSystem sd("sd") to be able to test it.

I think there may be a problem here:

    char  strh[4];
    char  strm[2];
    char  strs[2];

    sprintf(strh, "%04.4i",hours);
    sprintf(strm, "%02.2i",mins);
    sprintf(strs, "%02.2i",secs);

The arrays need to be big enough to store the strings, and in C, that is always num chars + 1, as strings are stored "null terminated"; the string ends when there is a 0x00 (or '\0') byte. If they are too small, you potentially trash memory.

Simon

25 Nov 2009 . Edited: 25 Nov 2009

Try adding an if ( lp == NULL ) printf("fopen failed!") after fopen(), I have a feeling that it's failing and the next calls are crashing. Although I would expect that to trigger blinkenlights...

Edit: oh, Simon might be onto something here. BTW, why don't you use a single format string for fprintf like you've done with printf above?

25 Nov 2009
Simon Ford wrote:

Hi David,

Just a note, I didn't have an SD Card, so just changed SDFileSystem sd(...) to LocalFileSystem sd("sd") to be able to test it.

I think there may be a problem here:

    char  strh[4];
    char  strm[2];
    char  strs[2];

    sprintf(strh, "%04.4i",hours);
    sprintf(strm, "%02.2i",mins);
    sprintf(strs, "%02.2i",secs);

The arrays need to be big enough to store the strings, and in C, that is always num chars + 1, as strings are stored "null terminated"; the string ends when there is a 0x00 (or '\0') byte. If they are too small, you potentially trash memory.

Simon

ta for looking.. just trying it ! (went to get a cup of tea)

25 Nov 2009
Simon Ford wrote:

Hi David,

Just a note, I didn't have an SD Card, so just changed SDFileSystem sd(...) to LocalFileSystem sd("sd") to be able to test it.

I think there may be a problem here:

    char  strh[4];
    char  strm[2];
    char  strs[2];

    sprintf(strh, "%04.4i",hours);
    sprintf(strm, "%02.2i",mins);
    sprintf(strs, "%02.2i",secs);

The arrays need to be big enough to store the strings, and in C, that is always num chars + 1, as strings are stored "null terminated"; the string ends when there is a 0x00 (or '\0') byte. If they are too small, you potentially trash memory.

Simon

you sir are a GENIUS !

many thanks..

its happily ticking away and writing onto the SDCard.

I'm just not used to having to actually think about things like sizes of strings or how they are stored. Life we lead on the other side eh ? I had some contractors working for me recently that couldn't believe we still use Cobol ! Mind you, I also had a grad working for me recently that asked what a DOS Prompt is when I said to "just drop to DOS and do a net use"

25 Nov 2009
Igor Skochinsky wrote:

 BTW, why don't you use a single format string for fprintf like you've done with printf above?

I'm working on it.. I canlt see whats wrong with a good old    A$ = B$ + C$.  printf(A$)    ;-)

This C stuff is all a bit double dutch to me, but I'm learning !