Go Down

Topic: sending csv to serial port (Read 1 time) previous topic - next topic

Boffin1

#5
Sep 10, 2012, 11:54 am Last Edit: Sep 10, 2012, 12:00 pm by Boffin1 Reason: 1
I have just seen the similar thread about strings and sd cards,  I will have a go at sending each data and a comma, if I can

Can I use  (  for some of the column titles to start with )  :-

hardwired.print("   day  ,");
hardwired.print("   month  ,");
hardwired.println("   year  ,");

with the comma in the text, and a ln on the last print ?

or must I send each comma seperately ?
With my mobile phone I can call people and talk to them -  how smart can you get ?

PaulS

Quote
Whats the alternative

Multiple print() and/or println() calls to write each record. There is no advantage in writing the whole record at once. At the end, each character is shifted out individually. You might as well buffer them that way.

Boffin1

OK great, it makes more sense , thanks -    I love learning, just wish I had more time !

I love deadlines, that whooshing sound they make as they fly by !
With my mobile phone I can call people and talk to them -  how smart can you get ?

danb35


Can I use  (  for some of the column titles to start with )  :-

You can use separate statements like that, but you can just as well use
Code: [Select]
hardwired.println("day,month,year");
I haven't seen CSV data padded with spaces before; usually any formatting like that is done in the app that reads the data (the spreadsheet/database).

Another option to build your data strings would be sprintf and a char array, like this:

Code: [Select]

#include <Time.h>

char dataString[11];
time_t t = now();
sprintf(dataString,"%.2d,%.2d,%.4d",day(t),month(t),year(t));
hardwired.println(dataString);


sprintf will put your values ("day(t),month(t),year(t)" here) into a specified format ("%.2d,%.2d,%.4d"--google sprintf for information and examples on formatting), and save that into the designated array (dataString).

For this to work, you have to know the maximum length of one line of data, and set the length of the char array to at least one more than that (to allow for the null that will be added at the end).  However, this will print your data using two lines of code (the sprintf to build your output string, and the println to actually print it), rather than five to separately print the day, month, year, and commas between them.  The code savings increases as you decide to log more things.  Obviously you can use any data you want; I'm just showing date using the Time library, but anything else could be stored instead.

Go Up