Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:12:24+00:00 2026-06-11T11:12:24+00:00

I am currently working on an embedded Linux device for data logging. The Linux

  • 0

I am currently working on an embedded Linux device for data logging. The Linux device is plugged into a CANbus and writes the traffic to an SD card.

From time to time the SD card corrupts and is mounted read-only. This behavior needs to be avoided.

The file system is FAT (the SD card should stay be readable by windows systems).

The embedded device can power fail any time, so I need a safe way to write to the SD card from my C program.

As I am not really into C, I rely on a program called “candump” which basically prints the canmessages to stdout in this format:

<0x006> [8] 77 00 00 00 00 00 00 00

My C program basically opens the candump program, reads from stdout, adds a timestamp and removes unnecessary chars:

1345836055.520 6 7700000000000000


while(running)
{
    if (filename != NULL)
    {
        fp_log = fopen(filename, "a");
        if (!fp_log) 
        {
            perror("fopen");
            exit (EXIT_FAILURE);
        }  
    }  

    fgets(line, sizeof(line)-1, fp);

    /* reset the row_values so they are always correctly initialized */
    row_identifier = 0;

    if (strchr(line,'<') != NULL)
    {
        /* creating a buffer char to store values for casting char to int*/
        buffer_ident[0] = line[4];
        buffer_ident[1] = line[5];

        /* cast buffer e.g. {'1','0','\0'} to int: 10 */
        row_identifier = strtol(buffer_ident,NULL,10);

        /* heartbeat of the CANBUS PLC */
        if(row_identifier == 80)
        {
            /* return pong on identifier 81 to the PLC */
            //system("cansend can0 -i 81 1 > /dev/null");
        }
        else
        {
            gettimeofday(&tv,NULL);
            fprintf(fp_log,"%d.%03d ", tv.tv_sec, tv.tv_usec/1000);
            fprintf(fp_log,"%d ",row_identifier);

            /* rowlenght > 11 = data part is not empty */
            row_lenght = strlen(line);
            if (row_lenght>11)
            {
                int i=0;
                for (i=11;i<row_lenght;i++)
                    /* remove spaces between the data to save space and copy data into new array */
                    if (isspace(line[i]) == 0)
                        fprintf(fp_log,"%c",line[i]);
            fprintf(fp_log,"\n");   
            }
        }
    }
    fclose(fp_log);

}

The code snippet above works fine, its just that I get SD card corruption.

Solution

I ended up using ext3 as the file system with standard mount options. No problems any more

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:12:26+00:00Added an answer on June 11, 2026 at 11:12 am

    The corruption probably happens because the OS didn’t complete its write operations on the FAT filesystem. As correctly pointed out by J-16 SDiZ you can try to mitigate the problem doing sync from time to time to force the OS to write changes on the filesystem.

    However, you are having this kind of problems because you are not using a journaled filesystem (such as Ext3 or NTFS. Another thing to do, could be to fsck the filesystem every boot and then explicitly forcing a rw remount to keep the mountpoint clean and writeable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on an embedded device project where I'm running into performance problems.
I'm working on an embedded project that currently uses C in Linux and uClibc.
Currently I am working with a embedded system that has the Linux OS. I
I am currently working on a project which will run on an embedded device.
I currently working with embedded linux with bash shell on it. Write now I
Currently working with converting SQLException error messages into messages that are more useful for
Currently working with the following package structure: /package __init__.py final.py /write __init__.py write.py /data
Currently working for the first time with JSON and with little experience of jQuery.
I'm working on an embedded Linux project. Our build process makes an image that
I am working on an user space app for an embedded Linux project using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.