繁体   English   中英

通过 NRF52840 读取 CSV 文件

[英]Reading a CSV file via NRF52840

 00> <info> app: Reading: data.csv... 00> 00> <info> app: data.csv sucessfully opened! 00> 00> <info> app: File size: 37876 bytes 00> 00> <info> app: File successfully read! 00> 00> <info> app: 0 bytes read

我正在尝试读取可以写入 Nordic NRF52840 的 CSV 文件。 文件类型为 CSV。 文件本身只是一个 ID 值,旁边有一些传感器/数据值。

我也希望能够读取该文件。 最好根据 ID 值读取一行。 但是我在读取数据时遇到了问题。 在我的终端中,我可以看到该文件存在,并且它具有从我的读取函数中找到的文件大小。 但是,当我尝试读取文件时。 它带来了 0 个字节的读取。

下面是我阅读 CSV 的代码,任何提示都会非常感谢。

 void SD_CARD_Read() { uint16_t size; UINT bytesRead;//From sd card driver library while (fno.fname[0]); ff_result = f_open(&file, FILE_NAME, FA_READ | FA_WRITE | FA_OPEN_APPEND); if(ff_result != FR_OK)//Not passing if the file is missing { if (ff_result != FR_OK) { NRF_LOG_INFO("Unable to open or create file: " FILE_NAME "."); SD_CARD_PRESENT = 0; return; } } else//File was openned fine { NRF_LOG_RAW_INFO(""); NRF_LOG_INFO("Reading: " FILE_NAME "..."); NRF_LOG_INFO(FILE_NAME" sucessfully opened!"); size = f_size(&file); char * data = NULL; data = malloc(size); /* allocate memory to store image data */ NRF_LOG_INFO("File size: %d bytes", size); ff_result = f_read(&file, data, (UINT) size, &bytesRead); if (ff_result == FR_OK){ NRF_LOG_INFO("File successfully read!"); NRF_LOG_INFO("%d bytes read", bytesRead); for (int i=0; i < bytesRead; i++) { NRF_LOG_INFO("data[%d]: 0x%x", i, data[i]); } } free(data); // free allocated memory when you don't need it } (void) f_close(&file); return; }

这是我的终端的输出。 如您所见,它标识了一个名为 data.csv 的文件及其大小,但不读取任何数据。

 00> <info> app: Reading: data.csv... 00> 00> <info> app: data.csv sucessfully opened! 00> 00> <info> app: File size: 37876 bytes 00> 00> <info> app: File successfully read! 00> 00> <info> app: 0 bytes read

根据我的理解,代码 f_read 将 bytesRead 设置为 0。我正在使用 FA_OPEN_APPEND 打开文件。 下面是传递给 read 函数的 sdk 参数:

 FRESULT f_read ( FIL* fp, /* Pointer to the file object */ void* buff, /* Pointer to data buffer */ UINT btr, /* Number of bytes to read */ UINT* br /* Pointer to number of bytes read */ )

这个答案是猜测,因为我不知道有关 SD 卡库的任何详细信息。

也许库没有单独的指针来读取和写入(附加到)文件。 如果FA_OPEN_APPEND将位置设置为文件末尾,则f_read不会从该位置获取任何数据。

尝试使用f_open没有FA_OPEN_APPEND ,甚至没有FA_WRITE

ff_result = f_open(&file, FILE_NAME, FA_READ);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM