私は現在、データロギング用の組み込みLinuxデバイスに取り組んでいます。LinuxデバイスはCANbusに接続され、トラフィックをSDカードに書き込みます。
SDカードが破損し、読み取り専用でマウントされることがあります。この動作は避ける必要があります。
ファイルシステムはFATです(SDカードはWindowsシステムで読み取り可能である必要があります)。
組み込みデバイスはいつでも電源障害が発生する可能性があるため、CプログラムからSDカードに安全に書き込む方法が必要です。
私は実際にはCに興味がないので、基本的に次の形式でcanmessagesをstdoutに出力する「candump」と呼ばれるプログラムに依存しています。
<0x006> [8] 77 00 00 00 00 00 00 00
私のCプログラムは基本的に、candumpプログラムを開き、stdoutから読み取り、タイムスタンプを追加し、不要な文字を削除します。
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);
}
上記のコードスニペットは正常に機能しますが、SDカードが破損するだけです。
解決
最終的に、標準のマウントオプションを使用してファイルシステムとしてext3を使用しました。もう問題ありません