1

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 */
)
Thomas Morris
  • 794
  • 5
  • 26
  • 1
    Please [edit] your question and add more details. Please copy&paste the program output. Are you saying that `f_size` returns the expected size but `f_read` puts 0 into `bytesRead`? I don't know any details about the SD card library, but I guess if you use `f_open` with `FA_OPEN_APPEND`, the current position for reading/writing the file is at the end of the file and you cannot read any data from this position. I suggest to omit `FA_OPEN_APPEND` and maybe also `FA_WRITE`. Or maybe there is something like `lseek` or `fseek` which could be used to move the pointer to the beginning of the file. – Bodo Mar 02 '20 at 15:21
  • I have added more detail to my post to include the terminal output, and the sdk information about how to use the function. Unfortunately I can not find any examples online of how to use the read function for this api. The function is supposed to read the number of bytes it found via f_size. I know the function runs ok as no errors occur. – Thomas Morris Mar 02 '20 at 15:33
  • 1
    Why do you use `FA_OPEN_APPEND` and `FA_WRITE` if you only want to read the file? – Bodo Mar 02 '20 at 15:44
  • @Bodo thats a good point. I just changed it to FA_READ only. I was looking in the example which opens for reading and it used FA_OPEN_APPEND and FA_WRITE and FA_READ. So I presumed it would work. But, I removed the others and just used FA_READ and, it now reads the bytes. Thanks, feel free to suggest it as an answer and, ill accept it. Thanks – Thomas Morris Mar 02 '20 at 15:59

1 Answers1

2

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);
Bodo
  • 9,287
  • 1
  • 13
  • 29