Pages: [1]   Go Down
Author Topic: sending csv to serial port  (Read 964 times)
0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

With my mobile phone I can call people and talk to them -  how smart can you get ?

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I will let the users IT guys work it out then
Before you get that far, ditch the String class. Period.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

With my mobile phone I can call people and talk to them -  how smart can you get ?

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
« Last Edit: September 10, 2012, 05:00:05 am by Boffin1 » Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Offline Offline
Full Member
***
Karma: 1
Posts: 107
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: