cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
2,176 Views
Registered: ‎04-23-2018

How to read Float data from a file in SD card

Jump to solution

Hi,

I am using SDK 2017.2 for making a multicore application in zync mpsoc. I have a test data file in which every data is floating point value. I am running my code on FREE RTOS . I enabled xilffs bsp library and i am using 'f_read' function to read the data. Problem that i am facing is ,if data value is 234.567, 'fread ' will read '2','3','4','.','5','6','7' as individual characters. I want to read 234.567 as a single data and i want to store data into a floating point variable. Can anyone help me with this. 

 

In simple words i want to replicate 'fscanf' function ?

 

I am attaching my input file also.

 

Thanks and Regards,

Sarath

0 Kudos
1 Solution

Accepted Solutions
Highlighted
2,284 Views
Registered: ‎04-23-2018

Hi All,

I have posted above question some days back. I found answer for this. i am giving the same program below .

Notes: file which you want to read should be a bin file , you have write float data into a bin file instead of text file.

           increase stack/heap size depending upon your buffer size.

 

program is tested on : Zync Ultrascale MPSOC (ZCU102).

 

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "ff.h"

#define TOTAL_SIZE (512*256*16*8)

typedef struct complex {

float real;
float imag;
}cmplx;

int main()
{
FATFS def_drive;
FRESULT rc;
FIL fp1;
UINT bytes_read;
UINT bytesToBeRd = sizeof(cmplx) * TOTAL_SIZE;
//cmplx data_test[10];
cmplx data[TOTAL_SIZE];
init_platform();

print("Hello World\n\r");

if((rc = f_mount(&def_drive,"0:",0)) != FR_OK)
{
print("ERROR : f_mount failed\n\r");
return 0;
}

if((rc = f_open(&fp1,"0:elevOut.bin",FA_OPEN_EXISTING | FA_READ)) != FR_OK)
{
print("ERROR : f_open failed\n\r");
return 0;
}

if((rc = f_read(&fp1,&data[0],bytesToBeRd,&bytes_read)) != FR_OK)
{
print("ERROR : f_read failed\n\r");
return 0;
}

if((rc = f_close(&fp1)) != FR_OK)
{
print("ERROR : f_close failed\n\r");
return 0;
}
cleanup_platform();
return 0;
}

 

regards,

sarath

View solution in original post

4 Replies
Contributor
Contributor
2,150 Views
Registered: ‎04-04-2018

 > i want to replicate 'fscanf' function ?

 

It's only 5 KiB ... why not just read the whole file into memory? ... then just use sscanf to your hearts content?

 

0 Kudos
Highlighted
2,124 Views
Registered: ‎04-23-2018

Thank you for ur reply. 

 

Can u tell me where is this sscanf function defined and how to use it?

 

i have files with bigger size also at that time how can i read the whole file ?

 

Regards,

sarath 

0 Kudos
Highlighted
2,285 Views
Registered: ‎04-23-2018

Hi All,

I have posted above question some days back. I found answer for this. i am giving the same program below .

Notes: file which you want to read should be a bin file , you have write float data into a bin file instead of text file.

           increase stack/heap size depending upon your buffer size.

 

program is tested on : Zync Ultrascale MPSOC (ZCU102).

 

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "ff.h"

#define TOTAL_SIZE (512*256*16*8)

typedef struct complex {

float real;
float imag;
}cmplx;

int main()
{
FATFS def_drive;
FRESULT rc;
FIL fp1;
UINT bytes_read;
UINT bytesToBeRd = sizeof(cmplx) * TOTAL_SIZE;
//cmplx data_test[10];
cmplx data[TOTAL_SIZE];
init_platform();

print("Hello World\n\r");

if((rc = f_mount(&def_drive,"0:",0)) != FR_OK)
{
print("ERROR : f_mount failed\n\r");
return 0;
}

if((rc = f_open(&fp1,"0:elevOut.bin",FA_OPEN_EXISTING | FA_READ)) != FR_OK)
{
print("ERROR : f_open failed\n\r");
return 0;
}

if((rc = f_read(&fp1,&data[0],bytesToBeRd,&bytes_read)) != FR_OK)
{
print("ERROR : f_read failed\n\r");
return 0;
}

if((rc = f_close(&fp1)) != FR_OK)
{
print("ERROR : f_close failed\n\r");
return 0;
}
cleanup_platform();
return 0;
}

 

regards,

sarath

View solution in original post

Highlighted
Adventurer
Adventurer
1,000 Views
Registered: ‎03-13-2019

Hi sarath,

I want to read text file which is off size 19MB and store it into DDR memory.

How can I do that operation? Application should be linux or baremetal?

Your input is highly appreciated

 

Thanks and regards.

0 Kudos