我目前正在开发用于数据记录的嵌入式Linux设备.Linux设备插入CANbus并将流量写入SD卡.
SD卡有时会损坏并以只读方式安装.需要避免这种行为.
文件系统是FAT(SD卡应该可以被Windows系统读取).
嵌入式设备可以随时断电,因此我需要一种安全的方法从我的C程序写入SD卡.
由于我不是真的进入C,我依赖一个名为"candump"的程序,它基本上以这种格式将canmessages打印到stdout:
<0x006> [8] 77 00 00 00 00 00 00 00
Run Code Online (Sandbox Code Playgroud)
我的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];
/* …Run Code Online (Sandbox Code Playgroud)