我目前正在使用嵌入式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