FSRW Reading Problems?
John A. Zoidberg
Posts: 514
Hello there,
I've just followed the Gadget Gangster's tutorials on the SD cards, and I've successfully created a file with a short text inside.
When I tried reading another file, it does the job, but then the terminal spraying out other gibberish and continued doing so.
Here is the code:
The test2.txt contains the string "abcdefghijklmnopqrstuvwxyz".
When I put "sdfat.pread(buffer,25)", it went until "abc...xy" only, without the z.
When it's "sdfat.pread(buffer,26)", it went "abc...xyz" and other accompanying gibberish.
Any ideas how to solve this?
I've just followed the Gadget Gangster's tutorials on the SD cards, and I've successfully created a file with a short text inside.
When I tried reading another file, it does the job, but then the terminal spraying out other gibberish and continued doing so.
Here is the code:
... (other code) ... sdfat.popen(string("TEST2.TXT"), "r") waitcnt(200_000 + cnt) sdfat.pread(buffer,26) pst.str(buffer) ... (other code) ...
The test2.txt contains the string "abcdefghijklmnopqrstuvwxyz".
When I put "sdfat.pread(buffer,25)", it went until "abc...xy" only, without the z.
When it's "sdfat.pread(buffer,26)", it went "abc...xyz" and other accompanying gibberish.
Any ideas how to solve this?
Comments
The way to fix this is either to zero the buffer before you read into it (so there are zero bytes left after any data read in) or to check the actual number of characters read with .pread and use that to put a zero byte at the end of the data. Make sure that the buffer is large enough for what you read plus one extra byte for the terminator. Also remember that .pread will also return -1 (if I remember correctly) if it's at the end of the file.
A third option would be to read the characters one at a time with .pchar and output them using .out.
I'm not sure whether the .pread would find an EOF inside, and that explains why the other gibberish came right on the last element of the buffer.
I've also zeroed the contents of the buffer - didn't work though.
I'll try the "return -1" option if it's available.
I checked the FSRW code. The -1 is returned by .pgetc at the end of the file. .pread returns a negative number on an error. Attempting to read at the end of the file is an error. I think .pread will read what's available and return the amount read. On the next read, when there's nothing left to read in the file, it will return the negative number.
Zeroing the buffer before reading should work. Remember that, for reading up to 26 bytes, you need a 27 byte buffer to allow for a zero byte at the end.
Also, I noticed that my 10MHz crystal @ PLL 8x = 80MHz wouldn't mount the SD card at all, until I had to use the default PLL 1x. Any ideas why it happened?
Or I should need 5MHz crystals for all the Props? Thanks.
You're using a breadboard. That's probably the problem. Breadboards have significant capacitance and that can load the crystal. Sometimes with breadboards and high frequency oscillators, you have to actually solder the crystal to the IC pins rather than using the breadboard. You may do better with a 5MHz crystal and the breadboard.
No wonder all the Prop tutorials w/ breadboard recommends the 5MHz crystals.
I'll stock on the 5MHz crystals by today too.