PDA

View Full Version : Problem writing to SD card



Hugh
07-11-2009, 11:49 PM
Folks,

I'm trying to write to an SD card using the excellent fsrw object, but am having some problems.




OBJ
sdfat : "fsrw"
lcd : "LCD"

VAR

Byte mode, index, fast, tRef, recData[100] , AnsaA [25], tbuf[20], r, sta
long txDisp, units , AnsaB, Ansa, stack1[20], stack2[20], stack3[20]



pub main

waitcnt(80_000_000 + cnt)
sdfat.mount(00)

AnsaA:=string("Test") ' What I am trying to write to the SD card





lcd.cls
if r ==0
lcd.str(string("Mounted."))
waitcnt(240_000_000 + cnt)
sdfat.opendir
repeat while 0 == sdfat.nextfile(@tbuf)
sta := cnt
else
lcd.str(string("Failed!"))

sdfat.popen(string("Data1.csv"), "a")

repeat 500
ct :=0
' repeat 20
' sdfat.pputc(AnsaA[ct++]) This didn't work

sdfat.pwrite(@AnsaA,25)
sdfat.pputc(13) 'Carriage Return

sdfat.pclose




What I get is a file with 500 "œ" characters, each with 25 spaces before them. I'm obviously making a fundamental mistake here, but can't see the wood for the trees.

Any suggestions / pointers would be gratefully received - I have been through the 'insert SD card - turn on Prop - upload code - wait for it to mount / write - turn off Prop - remove SD card - insert SD card in PC - open file - close file - alt-tab to Prop tool - change code' loop so many times now!

Many thanks,

Hugh

P.S. Its an OBD code reader for my Saab that shows live data on an LCD display - I want to log one or two parameters to SD every couple of seconds.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Hugh - the thinking woman's Geoffrey Pyke.

MagIO2
07-11-2009, 11:58 PM
1. AnsaA is already a pointer, so don't use the @ operator in the pwrite
2. The string is only 4 bytes long, so don't tell pwrite to write 25 bytes.

pwrite(AnsaA, 4)

PS:
Ah ... I see .. you have a big missunderstanding in the string-instruction. string won't copy the string into your array. string stores the string you pass as a parameter somewhere in a dat-section and returns a pointer to that place.

By the way .. you could also write:
AnsaA:=string( "Test", 13)

Then you don't need the putc.

Post Edited (MagIO2) : 7/11/2009 4:13:23 PM GMT