Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] How can I read 250 bytes from the start of the file ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,

I'm using FATFs,
How can I read 250 bytes from the start of the file ?

f_read?

thanks
 

FRESULT f_read (
FIL* fp, /* [IN] File object */
void* buff, /* [OUT] Buffer to store read data */
UINT btr, /* [IN] Number of bytes to read */
UINT* br /* [OUT] Number of bytes read */
);
Parameters
fp
Pointer to the open file object.
buff
Pointer to the buffer to store read data.
btr
Number of bytes to read in range of UINT type.
br
Pointer to the UINT variable to return number of bytes read. The value is always valid after the function call regardless of the result.


Return Values
FR_OK, FR_DISK_ERR, FR_INT_ERR, FR_NOT_READY, FR_INVALID_OBJECT, FR_TIMEOUT
Description
The file read/write pointer of the file object advances number of bytes read. After the function succeeded, *br should be checked to detect the end of file. In case of *br is less than btr, it means the read/write pointer reached end of the file during read operation.
 

This code working with 19Kbytes text file
but when I change to MP3 file it's reading only 3 bytes and with small file less than 1Kb, it doesn't work,
anyone knows why ?

thanks

if (f_open(&Fil, "test.txt", FA_READ) == FR_OK)
{
f_read(&Fil, line, sizeof line, &bw);

usart_pstr("\nReading file 512 bytes \n");

usart_pstr(line);
f_close(&Fil);
usart_pstr("\nFinished reading 512 bytes\n");
}
 

how do you know it is only reading 3 bytes? it is binary data [and not text data]
 

Do you get any messages - are you checking?
EOF marker in binary file - how are you checking when reading back?
Do you clear buffer before re-reading
What compiler are you using
Read kams spec carefully esp the note at the foot
 

I read from UART ,
when I'm reading test.txt, it's reading exactly 512 bytes,
but with MP3 file,
it's reading only 3 bytes...

ID3 and that's it....why is it ?
 

Binary data can't be printed as a string.

To understand why your code goes wrong, just view the start of a MP3 file with ID3v2 tag in a hex editor.

I presume you are referring to a document describing the structure of ID3v2? It should also answer the question.
 

Yes I'm referring to a document about ID3V2
When I used :
Code:
/*TESTING READING FILE BEGIN*/
        /* Open a text file */
        //fr = f_open(&Fil, "hello.mp3", FA_READ);
        fr = f_open(&Fil, "air2.mp3", FA_READ);

		if (fr) return (int)fr;
        usart_pstr("Reading file \n \n");
        /* Read all lines and display it */
        while (f_gets(line, sizeof line, &Fil))
      
		usart_pstr(line);
         
        /* Close the file */
		
		usart_pstr("\n \n Finished reading file.. \n \n");

        f_close(&Fil);
	/*TESTING READING FILE END*/

I got :
mp3file.jpg

I don't understand why if I read partially, it doesn't give a response as I want ?
but if I read the whole file, it's fine ??

thanks

- - - Updated - - -

Code:
 /*TESTING READING 512 bytes begin*/
  char line[512],size_char; /* Line buffer */
	 if (f_open(&Fil, "air1.mp3", FA_READ) == FR_OK)
	 {
		 f_read(&Fil, line, sizeof line, &bw);
		 usart_pstr("\n\nReading file 512 bytes \n\n");
		 
		 usart_pstr(line);
		 f_close(&Fil);
		 usart_pstr("\n\nFinished reading 512 bytes\n\n");
	 }
	 /*TESTING READING 512 bytes end*/

I got :
mp3file2.jpg


mp3file3.jpg

Why is it ?
thanks
 

did you read carefully [and try] the suggestions given?

It seems you are not grasping a key point; text files and binary files[like mp3] are different animals.

I recommend reading up on binary files.
 
Last edited:

Respectively you need to learn about string handling in C.

Try to understand from the library manual what uart_pstr() does, how a 0 byte works as a string delimiter.
 

I recommend reading up on binary files.
How can I do that ? thanks

uart_pstr :
Code:
void usart_pstr(unsigned char *s) {

	// loop through entire string

	while (*s) {
		usart_transmit(*s);
		s++;
	}
}
 

uart_pstr() is a typical string oriented function, it stopps at the terminating 0.

The first 5 bytes in your file are "I", "D", "3", 0x03, 0x00. 0x00 is understood as delimiter, only 4 characters are printed.

You also used f_gets(), a read function for line formatted text files which waits for a carriage return (0x0d). But carriage return isn't used in ID3 tags, so it's unsuitable.

What you need to do:
- Read the ID3 tag as binary data, e.g. with f_read()
- Decode the tag fields according to the specification
 

Code:
 if (f_open(&Fil, "air1.mp3", FA_READ) == FR_OK)
	 {
		 f_read(&Fil, line, sizeof line, &bw);
		 usart_pstr("\n\nReading file 512 bytes \n\n");
		 
		 usart_pstr(line);
		 f_close(&Fil);
		 usart_pstr("\n\nFinished reading 512 bytes\n\n");
	 }

How can I display it on UART ?
What will I modify here ?
Code:
void usart_pstr(unsigned char *s) {

	// loop through entire string

	while (*s) {
		usart_transmit(*s);
		s++;
	}
}
void usart_transmit( BYTE data )

{
	/* Wait for empty transmit buffer */
	while ( !( UCSR0A & (1<<UDRE0)) )
	;
	/* Put data into buffer, sends the data */
	UDR0 = data;
}
 

What will I modify here ?
- print a fixed number of bytes instead of stopping at 0.
- possibly print bytes as hex value (format %02x) instead of ASCII character
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top