我目前正在使用嵌入式Linux设备进行数据记录。 Linux设备插入CANbus并将流量写入SD卡。避免在ansi中损坏SD卡C
SD卡随时会损坏并挂载为只读。这种行为需要避免。
文件系统是FAT(SD卡应该可以让Windows系统保持可读)。
嵌入式设备可能会随时发生故障,所以我需要一种安全的方式从我的C程序写入SD卡。
由于我没有真正进入C,我靠所谓的“candump”基本上打印canmessages这种格式的标准输出方案:
<0x006> [8] 77 00 00 00 00 00 00 00
我的C语言程序基本上打开candump程序,从读取标准输出,增加了一个时间戳,并删除不必要的字符:以上
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作为标准安装选项的文件系统。没有问题
你确定腐败不会发生在其他地方吗? –
这是唯一一个访问SD卡的程序,所以我没有考虑到腐败可能发生在其他地方,说实话我不知道在哪里开始或追踪损坏 – user1320852
/var/log /是一个开始挖掘SD卡问题根源的好地方(某些嵌入式系统默认情况下不会记录任何东西) – drahnr