EmbeddedRelated.com
Forums

Writing decimal number in a text file in SD card using FatFs library

Started by vtrivedi 5 years ago8 replieslatest reply 5 years ago940 views
Hi Team,

I am working on an application where I have to write the data captured from a pulser to SD card.

The data format includes header (26 bytes) followed by data itself.

I am using the FatFs library to write it.

The data that I am getting is uint8_t type.

The format of the header is as below:

Version: 4 digits 4 Bytes
Payload Size: 0-65535 2 Bytes
HW Version: 4 digits 4 Bytes
Time Code: 4 digits 4 Bytes
Vector ID: 0-65535 2 Bytes
Up Vector Length: 0-65535 2 Bytes
Up Sample Freq: 0-16777215 3 Bytes
Dn Vector Length: 0-65535 2 Bytes
Dn Sample Freq: 0-16777215 3 Bytes
Up Vector Data 4096 bytes Up Vector Length Bytes
Dn Vector Data 6656 Down Vector Length Bytes

The overall file size needs to be around 10 KB based on above table.

However, the size spikes up to around 14 - 15 KB.

Providing example as below:

Version: 1,9,1,0 4 Bytes
Payload Size: 10754 2 Bytes
HW Version: 2,2,0,0 4 Bytes
Time Code: 0x5C40E21C 4 Bytes
Vector ID: 1 2 Bytes
Up Vector Length: 4097 2 Bytes
Up Sample Freq: 8333333 3 Bytes
Dn Vector Length: 6657 2 Bytes
Dn Sample Freq: 8333333 3 Bytes
Up Vector Data Data Up Vector Length Bytes
Dn Vector Data Data Down Vector Length Bytes

If I use the standard f_printf or f_write option both of them writes either the value as individual bytes for a given value as if it is written as ascii code or does not write as decimal but ascii value.

For example 10754 is either written as 1 0 7 5 4 as if they were individual numbers instead of 2A02 (hex equivalent of 10754). In the buffer of the file it is writing the number 10754 on each index rather than having 2A followed by 02 as they are 8 bit value.

Kindly help
Kindly advise.
[ - ]
Reply by MarkDSylvaJanuary 21, 2019
I think it would be much easier for someone to help if you posted some code.    My guess is that you are using the incorrect format specifiers in your code but without actually seeing it, it's not easy to help!
[ - ]
Reply by vtrivediJanuary 21, 2019

Hi Mark,

Thank you for the reply.

I am attaching the piece of code that I am trying to use to write the data to the SD card file.temp.c

Also I am attaching the file that I get by using this code.V0000002.TXT

If you see the header part itself what I am expecting in the file is as below:

Byte # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Data 1 9 1 0 2A 2 2 2 0 0 1 5C 40 E2 1C 1 10 1 7F 28 15 1A 1 7F 28 15

But what I get is as below:

Byte # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Data 1 9 1 0 1 0 7 5 4 2 2 0 0 5 C 4 0 E 2 1 C 1 4 0 9 7 8 3 3 3 3 3 3 6 6 5 7 8 3 3 3 3 3 3

This are FatFs Generic FAT Filesystem Module functions and it is third party library.

http://elm-chan.org/fsw/ff/00index_e.html

[ - ]
Reply by SolderdotJanuary 21, 2019

If I get it right you wont to write the data binary, not human readable ASCII text. You can do that this way:

FILE *write_ptr;
write_ptr = fopen("test.bin","wb");  // w for write, b for binary
fwrite(buffer,sizeof(buffer),1,write_ptr); // write 10 bytes from our buffer

Printf will always transform binary data into human readable ASCII text.

[ - ]
Reply by vtrivediJanuary 21, 2019

thank you solderdot,

I will try with the suggestion that you provided.

[ - ]
Reply by SolderdotJanuary 21, 2019

Please note that my code snippet is correct for standarc-C libraries. Maybe your 3rd party lib works differently. However, search your lib for services allowing you to write *binary* data.

From your tables you attached to other posts it is clearly visible that this is what you want, but what you get is ASCII-hex format, i.e. instead of one byte having the value 0x2A you get a string of 2 bytes "2A".

[ - ]
Reply by vtrivediJanuary 21, 2019

Yes you are correct Solderdot in saying "instead of one byte having value 0x2A I get string of 2 bytes "2A".

The 3rd party FatFs library is the one that is used by msp432 to write on the SD card.

Let me search for the services allowing to write "binary" data.

[ - ]
Reply by mr_banditJanuary 21, 2019

This should work for most architectures. You also did not specify the MPU chip. Some older ones require a uint16_t as a long.

uint16_t val = 0x2A02;

printf( "val = %04x\n", val );

Try first with printf(). Then make sure your fd is good, and change to fprintf(). I am suspicious about your f_printf() - is that the actual call, or is it a typo on your part? If f_printf(), you may be using a non-standard library.

[ - ]
Reply by vtrivediJanuary 21, 2019

Hi bandit,

Thank you for the reply.

I am using MSP432P401R chip from Texas instrument.

f_printf is FatFs library function and it is third party library for Generic FAT FileSystem Module.

http://elm-chan.org/fsw/ff/00index_e.html

I will try with what you have mentioned.

With my way what I get is as below:

Byte # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
Data 1 9 1 0 1 0 7 5 4 2 2 0 0 5 C 4 0 E 2 1 C 1 4 0 9 7 8 3 3 3 3 3 3 6 6 5 7 8 3 3 3 3 3 3

But what I am expecting to get is as below:

Byte # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Data 1 9 1 0 2A 2 2 2 0 0 1 5C 40 E2 1C 1 10 1 7F 28 15 1A 1 7F 28 15

Attaching the file that I got using the way I wrote.

Hope this clarifies my problemV0000002.TXT