8 years, 3 months ago.

unable to read binary file from SD card

Hello,

I 'm trying to read a binary file from SD card and print them to teraterm. I could able to write a file to SD card in binary but I could not read it back. It is a custom board and communication is via serial port UART1. My controller is STM32l053R8. Here is my code

int main() {
    //pc.baud(9600);
      int nr;                    
      //int c;
      int readings[3];
     
        
     FILE *logFile = fopen("/sd/PCE-DL001.bin","rb");
      if (!logFile)  
      pc.puts("FAILED TO OPEN FILE");
     
      else { 
      //while (!feof (logFile))
      led1 = 1;
      while((nr = fgetc(logFile)) != EOF) 
      pc.printf("%d\n\r",nr);
      nr = fread(readings,sizeof(int16_t),3,logFile);
      if (nr != (sizeof(int16_t) * 3)) 
      pc.puts("\r\n UNEXPECTED END OF FILE"); 
      else for (int i = 0; i<3; i++) 
      pc.printf("\r\n %i",readings[i]);
      pc.puts("File complete"); 
      fclose(logFile); 
          }
  } 

I see failed to open on teraterm.

Any suggestions are appreciated. thank you.

Be the first to answer this question.