Write a variable to SD >csv file
AGCB
Posts: 330
It's time to ask for help. I've been trying for 3 days to figure out how to write a variable to a SD .csv . I have no problem writing a string of comma separated values and then displaying them on MS Excell. However I would like to substitute variables for the values. I have looked at many threads and objects and believe I have done my home work but still fail to get anything but gibberish on the spreadsheet for the variables.
Here is my code
Thanks
Aaron
Here is my code
CON _clkmode = xtal1 + pll16x '80 MHz system clock _xinfreq = 5_000_000 DO = 0 CLK = 1 DI = 2 CS = 3 OBJ sdfat : "fsrw" pst : "parallax serial terminal" VAR byte hour,minute,day PUB demo | mount pst.start(115200) waitcnt(clkfreq+cnt) day:=1 hour:=33 minute:=12 mount := \sdfat.mount_explicit(DO,CLK,DI,CS) if mount < 0 pst.str(string(13,"failed to mount")) abort pst.str(string(13,"SD card was found and mounted fine",13)) sdfat.popen(string("tryelev.csv"),"w") sdfat.pputs(string("AGG,PJG,JRG,TLJ,Tom", 13,10 )) 'works good up thru here sdfat.pwrite(day,2) sdfat.pputc(",") sdfat.pwrite(hour,2) sdfat.pclose sdfat.unmountOnce I can do this I will substitute variable values from a RTC and TC74
Thanks
Aaron
Comments
Here's one thread with some explanations. Post #3 has an example of creating a numeric string to use as a SD card file name. The same principle applies to strings you want to write to the card.
The "Dec" method of many serial objects are other examples of converting numbers to ASCII characters. Make sure and read about ASCII on Wikipedia too.
The jest is, numbers and characters are stored and transmitted differently. The number zero held in a variable has all it bits set to zero. The character "0" is stored and transmitted as the number 48. While a single byte can contain a number between 0 and 255, the same byte used to store a character can only hold a single digit. If you were to send the number 13 on the serial line to the terminal, if you sent the raw number as a single byte you'd get a carriage return. If you sent "13" as characters "1" and "3" you'd need to send two bytes with the values 49 and 51. 49 and 51 are the ASCII values for "1" and "3". Notice 49 (48 + 1) and 51 (48 + 3) are the ASCII value of zero (48) plus the numeric value of the character.
You need to use these same ASCII characters when you write to the SD card for Excel to be able to read them.
I'll study that out. BTW you have helped me numerous times before and I look for your posts as I read others questions. Thanks for taking the time to help people!
Aaron
ptr is ??
value is ??
length is the total length of output string ??w/ or w/o decimal places
dp is the number of decimal places
Fill me in where I'm missing or just plain wrong. This may lead to other questions.
Thanks
Aaron
It took me a bit to figure this out. There's an example of using the method in post #3 of the thread I linked to above.
"ptr" is the address of the buffer where you want the string stored. In the example I gave this was initially the first byte/character in the buffer "FileName".
This buffer can be a local temporary buffer. "value" is the variable you want converted to ASCII characters., "len" is the number of characters you want to output. "dp" can be used to add zero padding if you use a negative number or it can be used to display decimal points if you use a positive number. I think to get "normal" output, set "len" to the largest possible size the number can be (11 should be the largest you'd need) and set "dp" to zero.
The method returns the address of the next byte/character in the buffer where the characters had just been stored.
In the example (as shown in the above code block), "result" started as the address of "FileName".
The value of "result" will change after the first call to "fdec":
The value of "result" will have been increased by two since two characters were added to the original pointer.
The remaining calls to "fdec" continued to increase the value of "result". Keep in mind the value of "result" is the location in RAM where the next byte/character will be stored.
So now which address location does "result" point to?
To find out, you can add this line to the code in post #3 (make sure it's after the calls to "fdec").
Once you have your number stored as ASCII characters in a buffer, you can use the SD object's "str" method to write the string to the card in a similar way the string had been sent to the terminal.
Do you need help turning the example code into a working program to display the string "FileName" and the string stored in location pointed to by "result"?
I think I'm getting it. I don't have much time left today and will be out of town for a couple days but I will write my code with what I understand and let you know of my success or else additional wonderments as soon as I can and I will post what I have.
Thanks again
Aaron
In my extremely novice mind, I think I would now look at something like the ASCII0 String object to build a string of comma separated values to write to the SD card but I'd like suggestions if theres a better way to do this. Maybe just writing the individual characters to the SD with a comma between each so it can be displayed on an Excell spread sheet.
Several ideas for using this are
Record the times of hot water use in my house, the temperature of the water in the
tank and how long it takes for the water to return to max temp (I heat entirely w/ wood)
Record the amount and length of sunlight in my greenhouse
Record the time of events of my homemade trail/security camera
Thanks again
Aaron
You can add a comma to your "fileName" string by using the "ch" method of "StrFmt".
I'm not sure if you're aware that the various types of numbers, binary, hexadecimal, etc., is just for us human's convenience. The numbers are stored the same inside the Propeller.
The following three blocks of code will produce the same compiled code and run exactly the same.
I do know the base makes no difference to the Propeller, I was just using the RTC output format.
I'm sure that StrFmt is very useful but the total lack of comments makes it difficult for the novice.
My code is liberally commented; hope it helps you out.
Aaron
All works fine except that the write to sd card and that displayed on the PST are in decimal equivalents of the BCD (I think) values output by the RTC.
I have tried multiple ways to convert these so as to get proper output but I have failed in all. I basically know what the problem is but dont know how to fix it. Ive also looked at the code provided in posts above.
At the bottom of my code I have included the PST screen to show what I mean.
Thanks again for your time.
Aaron
JLOCKE It's a DS1307
Aaron
I failed to inform of my RTC chip so the output format and use of (fdec vs hex) was what caused it to not work. As JLocke was alluding to.
As I've seen many times on this forum "we can't help you if you don't tell us exactly what you have". I'm guilty but now happy that I learned something extra. I usually do learn more than bargained for.
Thanks to all who replied
Aaron
Time recorded on SD card Excel file works great after 10:00 AM
But, before that, leading zeros are deleted (I wish they would remain) AND either a 20 or 19 is inserted before the seconds value. It is 20 if the seconds are less than 30 and 19 if the seconds are greater than 30. I've tried and tried multiple ways of correcting this but to no avail. I could live with it if necessary but in the interest of learning which is my only goal anyway, I'd like to figure it out. This would be OK if I were a late riser LOL.
At the bottom of my code I have included the Excel file to show what I mean.
Thanks again for your time.
Aaron
Try to manually format all cells that might be written to, before having them written to.
Otherwise, can you make a macro that will format the cells aftter they are written?
hope this helps
Tom
You can have Excel treat the numbers as text if you surround it in double quotes.
You'll need to use the ASCII value for double quotes (34) since otherwise the Prop Tool will think you're trying to enter a character or string. Just use 34 to place a double quote at the beginning of the field and 34 before adding the comma delimiter.
If you change the name of the file to end with txt instead of csv, you can import it into Excel. While importing you can state what type of format you want the data to take. You might be able to use this import feature (or some other Excel feature) to solve the issue without having to resort to adding double quotes to the file.
It's too nice outside to be in the basement, I'm going fishing. I'll look at this early in the morning.
Thanks again for your knowledge.
Aaron
Aaron