简体   繁体   中英

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

I am trying to read a CSV file that I can write to in my Nordic NRF52840. The file type is a CSV. The file itself is just an ID value with some sensor / data values next to it.

I wish to be able to read the file as well. Preferably reading a line based upon the ID value. But I have an issue reading the data at all. Within my terminal I can see the file exists and it has a found file size from my read function. However, when I try to read the file. It comes up with 0 bytes read.

Below is my code for reading the CSV any tips would be great thanks.

 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; }

This is the output of my terminal. As you can see it identifies a file called data.csv and its size but, does not read any data.

 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

From my understanding the code f_read sets bytesRead to 0. I am openning the file with FA_OPEN_APPEND. Below are the sdk parameters to pass into the read function:

 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 */ )

This answer is a guess because I don't know any details about the SD card library.

Maybe the library does not have separate pointers for reading and writing (appending to) the file. If FA_OPEN_APPEND sets the position to the end of the file would expect that a f_read does not get any data from this position.

Try to use f_open without FA_OPEN_APPEND and maybe even without FA_WRITE .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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