UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
757 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
865 Views
Registered: ‎04-23-2018

Re: How to read Float data from a file in SD card

Jump to solution

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

0 Kudos
3 Replies
Contributor
Contributor
731 Views
Registered: ‎04-04-2018

Re: How to read Float data from a file in SD card

Jump to solution

 > 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
705 Views
Registered: ‎04-23-2018

Re: How to read Float data from a file in SD card

Jump to solution

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
866 Views
Registered: ‎04-23-2018

Re: How to read Float data from a file in SD card

Jump to solution

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

0 Kudos