sending csv to serial port

I have a project that sends csv values from the Arduino chip, to a serial port, for the user to use the data any way they wish.

For my prototype, I send the various header titles, ( seperated by commas )

I hadnt thought about it, but I didnt include a CR LF to start each new line of values.

I am using Megunolink to monitor the data.

It displays the data perfectly, I suppose it matches the number of values to the number of column titles ?

I want to generate a csv output that any application can use, I have no idea what OS the user has.

I had a look at my prototypes output using a general Term program, the data is there but jumbled , it obviously needs a CR/LF to separate the lines of values.

What should I put at the end of each line that any spreadsheet type application will recognise in a CSV file ?

There are a few options when I have searched.

heres a snippet of the code I am sending :-

     {dataString += "normal" ;dataString += ","; }                                  
           dataString += String (buttonnumber) ;                 
                   dataString += ",";dataString += ",";
                   dataString += String (counter) ;dataString += ",";
                    dataString += String (clockhours );dataString += ",";
                    dataString += String (clockmins );dataString += ",";
                                  hardwired.println(dataString);

The 'println()' function puts a newline ('\n') at the end of the line so I think you're covered unless your spreadsheet insists on Windows (CRLF) line endings.

Thanks John

I suppose the Terminal program I tried needs setting up for that..

I will let the users IT guys work it out then, thats what they are there for :slight_smile:

I will let the users IT guys work it out then

Before you get that far, ditch the String class. Period.

Whats the alternative Paul ? , this is all new ground for me..

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 ?

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.

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 !

Boffin1:
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

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:

#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.