3

Currently i am working on STM32F479 Eval board. I have to collect data from sensor nodes and store it in a text file using FatFs. My sensor values are in Hexadecimal values. I am using STM32F4Cube HAL Library which includes FatFs. I am using f_write() function to pass these hex values to a text file. But it is storing as garbage values which is not as expected. When i use f_printf() it is working perfectly as expected (Hex values gets stored). But when reading again it shows some other values which is not the stored values. I have two questions.

  1. What is the perfect way to store Hex values in a text file and read the same from a text file using FatFs?

  2. Is there any other way like converting these hex values to char before passing it into a file and again reconverting after reading from a file?

My code:

uint8_t Buff[]; // Buffer to read data from Sensors 

uint32_t byteswritten;

f_write(&MyFile,Buff,sizeof(Buff),(void*)byteswritten); //But writing some other values

for(i=0;i<sizeof(Buff);i++)
{
    f_printf(&MyFile,Buff[i]); //Writing properly But reading throws unexpected values
}
Bence Kaulics
  • 6,353
  • 12
  • 33
  • 60
Jahir Hussain
  • 31
  • 1
  • 5
  • 1
    Are you reading the file, tokenizing it, and running it through strtol()? Or are you just pulling the ASCII value that you stuffed into the file? – Daniel Jul 13 '16 at 05:53
  • 1
    I don't know what you mean by 'hex'. Do you mean the file looks something like "0d67f2a8569bc780", or "0d 67 f2 a8 56 9b c7 80", or "0x0d 0x67 0xf2 0xa8 0x56 0x9b 0xc7 0x80", or "0dh 67h f2h a8h 56h 9bh c7h 80h"? Can you show us a little of that that "f_printf" output looks like? How is Buff[] getting formatted as hex? It appears to me like you are just writing the sensor data directly without any formatting. Is the data coming from the sensors formatted? – Mark Jul 13 '16 at 06:04
  • I am getting data from our own chip (SoC) using SPI. SPI is configured as Receive only Slave 8 bits. So my data looks like "AA FF 80 12 A5 55". Yes, i am just pulling the ASCII value which are stuffed. – Jahir Hussain Jul 14 '16 at 06:10

1 Answers1

6

This line makes a pointer from a random value on the stack, asking for a (Hard-) fault:

f_write(&MyFile,Buff,sizeof(Buff),(void*)byteswritten); 

Correct code would look like this:

UINT byteswritten;
f_write(&MyFile,Buff,sizeof(Buff),&byteswritten);

Note that f_write() writes data in binary to the file, not in hexadecimal.

That f_printf() line should give you a compiler warning like "make pointer from an integer without a cast", because it expects a string (pointer to char) as second argument.

Corrected line for hexadecimal output:

f_printf(&MyFile," %X",Buff[i]);
Turbo J
  • 9,969
  • 1
  • 20
  • 28
  • Thank you Turbo. Say my data looks like "AA FF AF FA 54 45" which is in Buff array(uint8_t). I have to store it into a text file as same as it is (Hex) and read back it into another array. How can this be implemented using FatFs? – Jahir Hussain Jul 14 '16 at 06:15
  • Try looking at how basic file operations like `fread()` and `fwrite()` work on a PC. – Turbo J Jul 14 '16 at 13:13