我目前正在开发一个用于数据记录的嵌入式Linux设备。Linux设备插入CANbus并将通信量写入SD卡。
SD卡不时损坏并安装只读。必须避免这种行为。
文件系统很胖( SD卡应该保持windows系统的可读性)。
嵌入式设备可以随时停电,所以我需要一种安全的方式从我的C程序写到SD卡。
由于我对C不太感兴趣,所以我依赖于一个名为"candump“的程序,该程序基本上以这种格式将canmessages打印到stdout:
<0x006> [8] 77 00 00 00 00 00 00 00我的C程序基本上打开了烛光程序,从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作为具有标准挂载选项的文件系统。不再有问题了
发布于 2012-09-17 12:03:35
发生损坏的原因可能是操作系统没有完成其在FAT文件系统上的写入操作。正如J-16 SDiZ正确指出的那样,您可以尝试缓解这个问题,不时执行sync来强制操作系统在文件系统上写入更改。
但是,由于没有使用日志文件系统(如Ext3或NTFS ),因此出现了此类问题。另一件要做的事情是,每次启动时都要对文件系统进行fsck,然后显式地强制rw重新装入,以保持挂载点的干净和可写性。
发布于 2012-09-17 11:57:25
即使你不移除权力,腐败也会发生吗?上面的代码是用户级的,只执行简单的FILE *操作;它不应该损坏设备。
如果是的话,那么要么设备驱动程序本身就有问题,要么正在发生其他事情。
你能检查一下是否有电源问题,例如可能导致重置吗?
https://stackoverflow.com/questions/12458495
复制相似问题