cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
xilinxjtg
Contributor
Contributor
378 Views
Registered: ‎10-05-2020

Not able to save data in decimal form

Jump to solution

hello

I have attached my code below and the data saved in the file in ascii form which should be in decimal form

#include <stdio.h>
#include <stdlib.h>
#include "xsdps.h"
#include "platform.h"
#include "xil_printf.h"
#include "ff.h"
#define DATA_SIZE 1000

int main()
{

static FATFS FS_instance;
const char *path = "0:/";
static FIL file1;
FRESULT result;
static char fileName[32]= "FIL.txt";
u8 data[9] = {1,2,3,4,5,6,7,8,9};
unsigned int bytesWritten ;
volatile u32 FileSize= 10;
init_platform();

result= f_mount(&FS_instance,path,0);
if(result!=0)
{
printf("error mount %d\n",result);

}
printf("SD card mounted successfully %d\n",result);

result=f_open(&file1,fileName,FA_CREATE_ALWAYS|FA_WRITE|FA_READ);
if (result!=0)
{
printf("dummy.txt error %d\n",result);
}

printf("writing to sd card\n");
result = f_lseek(&file1, 0); // Pointer to beginning of file

result=f_write(&file1,(const void*)data,FileSize,&bytesWritten);
xil_printf("bytesWritten=%d\r\n", bytesWritten);
if(result!=0)
{
printf("error:f_write %d\n",result);
}

printf("data_written\n");
result=f_close(&file1);
if(result!=0)
{
printf("error f_close:%d\n",result);
}

result=f_mount(NULL,path,0);
if(result!=0)
{
printf("Error : %d\n",result);
}
printf("SD_card unmounted\n");
cleanup_platform();
return 0;
}

0 Kudos
1 Solution

Accepted Solutions
ericv
Scholar
Scholar
141 Views
Registered: ‎04-13-2015

@xilinxjtg , But why it is saved in ASCII why not as it is 1,2,3,... NO! NO! and NO!

The data is definitely not saved in ASCII, it's saved as 1,2,3,... in binary. The terminal will never display "123456789" with what you are doing.

I know the ASCII value of 1,2,3,... OK, if you say so. Then why didn't you define data[] like this to get "123456789" on the terminal?:

char data[9] = {'1','2','3','4','5','6','7','8','9'};

View solution in original post

0 Kudos
8 Replies
ericv
Scholar
Scholar
317 Views
Registered: ‎04-13-2015

@xilinxjtg , nowhere in your code data[] is converted from binary to ASCII. When data[] is initialized, the compiler puts in the memory the bytes 0x01, 0x02, 0x03,... - that what compilers do, they convert decimal, hex, octal, floating point to binary

Binary is written in the file when you use result=f_write(&file1,(const void*)data,FileSize,&bytesWritten);

f_write isn't printf("%d").

BTW, data[] has 9 bytes, why are you using FileSize set to10; i.e. writing 10 bytes instead of 9?

0 Kudos
xilinxjtg
Contributor
Contributor
293 Views
Registered: ‎10-05-2020

How to save the binary in the file??

 

i didn't get

f_write isn't printf("%d")

0 Kudos
ericv
Scholar
Scholar
285 Views
Registered: ‎04-13-2015

@xilinxjtg :

How to save the binary in the file??

Your code does exactly that - the file contains the binary data 00000001,00000010,00000011, 00000100, ....

i didn't get

f_write isn't printf("%d")

That was a short form to indicate you have to convert the binary data into ASCII if you want to save ASCII decimals in the file. ASCII of decimal 1 is 0x31 (00110001), decimal 2 is 0x32 (00110010), ...

It seems you need to look at this:

https://www.codingeek.com/tutorials/c-programming/example/character-ascii-conversion/

0 Kudos
xilinxjtg
Contributor
Contributor
236 Views
Registered: ‎10-05-2020

Hello Sir,

I want to write the data as it is I am sending to it i.e. 1,2,3....9 numeric value

for now, the data sent is converted into ASCII value which is not correct

 

code:

 

#include <stdio.h>
#include <stdlib.h>
#include "platform.h"
#include "xil_printf.h"
#include "ff.h"
#define DATA_SIZE 1000

int main()
{
init_platform();
static FATFS FS_instance;
const char *path = "0:/";
static FIL file1;
FRESULT result;
static char fileName[24]= "FIL";

result= f_mount(&FS_instance,path,1);
if(result!=0)
{
printf("error mount %d\n",result);

}
printf("SD card mounted successfully %d\n",result);

result=f_open(&file1,"dummy.txt",FA_CREATE_ALWAYS|FA_WRITE|FA_READ);
if (result!=0)
{
printf("dummy5.txt error %d\n",result);
}

printf("writing to sd card\n");

u8 data[9] = {1,2,3,4,5,6,7,8,9};
unsigned int bytesWritten = 0;
// for(int i=0;i<10;i++)
// {
// result= fwrite(&file1,i,1);
result=f_write(&file1,(const void*)data,5,&bytesWritten);

if(result!=0)
{
printf("error:f_write %d\n",result);
}
// }

printf("data_written\n");
result=f_close(&file1);
if(result!=0)
{
printf("error f_close:%d\n",result);
}

result=f_mount(NULL,path,0);
if(result!=0)
{
printf("Error : %d\n",result);
}
printf("SD_card unmounted\n");
cleanup_platform();
return 0;
}

1.JPG
0 Kudos
ericv
Scholar
Scholar
202 Views
Registered: ‎04-13-2015

@xilinxjtg , you are indeed "writing the data as it is"... The terminal is displaying it too. Look at an ASCII table (terminals nowadays always display ASCII , EBCDIC has disappeared). You will see SOH is in binary 00000001 (that's '1' in binary, the byte written, not the ASCII character '1') , STX is 00000010 (that's '2' in binary, the byte written,not the ASCII character '2'), ETX is 00000011 (that's '3' in binary, the byte written, not the ASCII character '3'), etc

The data is not written as ASCII. You say it is, but I don't see in your code where there's a binary to ASCII conversion.  Would you mind telling which lines of code that conversion occurs?

Please read again the link I've provided; else ask a colleague to explain the difference between a '1' byte and the ASCII character '1'. Again, I re-iterate, terminals display ASCII characters.

 

0 Kudos
xilinxjtg
Contributor
Contributor
169 Views
Registered: ‎10-05-2020

Hello

I know the ASCII value of 1,2,3,...

And the data is saved correctly in the .txt file

But why it is saved in ASCII why not as it is 1,2,3,...

I have not done any conversion in the code 

 

Can I know the .h header file for f_printf() in Xilinx SDK to include?

0 Kudos
ericv
Scholar
Scholar
142 Views
Registered: ‎04-13-2015

@xilinxjtg , But why it is saved in ASCII why not as it is 1,2,3,... NO! NO! and NO!

The data is definitely not saved in ASCII, it's saved as 1,2,3,... in binary. The terminal will never display "123456789" with what you are doing.

I know the ASCII value of 1,2,3,... OK, if you say so. Then why didn't you define data[] like this to get "123456789" on the terminal?:

char data[9] = {'1','2','3','4','5','6','7','8','9'};

View solution in original post

0 Kudos
xilinxjtg
Contributor
Contributor
94 Views
Registered: ‎10-05-2020

Hello

It worked 

Thank you!!

Now we want to save the actual received data through DMA in a .txt file

0 Kudos