Skip to content

Commit 6826d4e

Browse files
committed
Example of reading csv from PC (using python script)
1 parent f2227b2 commit 6826d4e

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [What is this CSV parser](#what-is-this-csv-parser)
44
* [Installation](#installation)
55
* [Usage](#usage)
6+
* [Examples](#examples)
67
* [Things to consider](#things-to-consider)
78
* [Specifying value types](#specifying-value-types)
89
* [How to store unsigned types](#how-to-store-unsigned-types)
@@ -135,6 +136,16 @@ for (int i = 0; i < strlen(csv_str); i++) {
135136
```
136137
137138
139+
## Examples
140+
Examples directory contains examples showing:
141+
* [basic usage](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/basic_usage)
142+
* [how to handle unsigned types](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/unsigned_values)
143+
* [how to supply csv by incomplete parts](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/supplying_csv_by_incomplete_parts)
144+
* [how to read csv file from a PC (using provided python script)](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/reading_from_computer_python)
145+
* [how to read csv file from SD card](https://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/reading_from_sd_card)
146+
147+
148+
138149
## Things to consider
139150
If CSV file doesn't contain header line, then it must be specified as 3rd argument of the constructor (see [this example](#headerless-files))
140151
If CSV file is separated by other character instead of comma, then it must be specified as 4th argument of the constructor (see [this example](#custom-delimiter))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Month,Average,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015
2+
May,0.1,0,0,1,1,0,0,0,2,0,0,0
3+
Jun,0.5,2,1,1,0,0,1,1,2,2,0,1
4+
Jul,0.7,5,1,1,2,0,1,3,0,2,2,1
5+
Aug,2.3,6,3,2,4,4,4,7,8,2,2,3
6+
Sep,3.5,6,4,7,4,2,8,5,2,5,2,5
7+
Oct,2.0,8,0,1,3,2,5,1,5,2,3,0
8+
Nov,0.5,3,0,0,1,1,0,1,0,1,0,1
9+
Dec,0.0,1,0,1,0,0,0,0,0,0,0,1
10+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Usage example for: https://github.com/michalmonday/CSV-Parser-for-Arduino */
2+
3+
#include <CSV_Parser.h>
4+
5+
/*
6+
Python script used to send the file through serial (from PC to arduino) can be found at:
7+
https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/reading_from_computer_python/arduino_serial.py
8+
9+
The csv file I used for this particular example can be found at:
10+
https://github.com/michalmonday/CSV-Parser-for-Arduino/blob/master/examples/reading_from_computer_python/hurricanes.csv
11+
12+
This example shows how Arduino can receive and parse csv file from a computer (sent using a python script).
13+
It first sends the file from PC to Arduino, Arduino parses it, for verification purposes it sends values
14+
from 2 columns ("Month", "Average") back to PC (python scripts reads them and prints them in console window).
15+
In this particular example the csv file I used was containing information about hurricanes. The csv file contained:
16+
17+
Month,Average,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015
18+
May,0.1,0,0,1,1,0,0,0,2,0,0,0
19+
Jun,0.5,2,1,1,0,0,1,1,2,2,0,1
20+
Jul,0.7,5,1,1,2,0,1,3,0,2,2,1
21+
Aug,2.3,6,3,2,4,4,4,7,8,2,2,3
22+
Sep,3.5,6,4,7,4,2,8,5,2,5,2,5
23+
Oct,2.0,8,0,1,3,2,5,1,5,2,3,0
24+
Nov,0.5,3,0,0,1,1,0,1,0,1,0,1
25+
Dec,0.0,1,0,1,0,0,0,0,0,0,0,1
26+
27+
After uploading this code to arduino and running the python script (command: python --csv hurricanes.csv),
28+
the python script outputs the following:
29+
30+
All ports:
31+
1. device=COM1 name=COM1 description=Communications Port (COM1) manufacturer=(Standard port types)
32+
2. device=COM4 name=COM4 description=Arduino Leonardo (COM4) manufacturer=Arduino LLC (www.arduino.cc)
33+
34+
A single arduino port was found and will be used
35+
COM4 - Arduino Leonardo (COM4)
36+
Received:
37+
0. month = May
38+
0. average = 0.10
39+
1. month = Jun
40+
1. average = 0.50
41+
2. month = Jul
42+
2. average = 0.70
43+
3. month = Aug
44+
3. average = 2.30
45+
4. month = Sep
46+
4. average = 3.50
47+
5. month = Oct
48+
5. average = 2.00
49+
6. month = Nov
50+
6. average = 0.50
51+
7. month = Dec
52+
7. average = 0.00
53+
54+
Received:
55+
end
56+
Closing port
57+
58+
59+
Note: I suggest to close Serial Monitor window (if it's open) before running the python script,
60+
otherwise the port may be busy.
61+
*/
62+
63+
void setup() {
64+
Serial.begin(115200);
65+
CSV_Parser cp(/*format*/ "sfccccccccccc");
66+
67+
// wait for python script to start sending the csv file
68+
while(!Serial.available()) {}
69+
70+
// receive the file and pass it to CSV_Parser object
71+
while(Serial.available()) {
72+
cp << Serial.read();
73+
}
74+
75+
cp.parseLeftover(); // just in case if the csv file doesn't end with "\n"
76+
77+
// get "Month" and "Average" columns
78+
char **months = (char**)cp["Month"];
79+
float *averages = (float*)cp["Average"];
80+
81+
// Send "Month" and "Average" columns back to the PC
82+
// (python script will read and print them in console window)
83+
for(int row = 0; row < cp.getRowsCount(); row++) {
84+
Serial.print(row, DEC);
85+
Serial.print(". month = ");
86+
Serial.println(months[row]);
87+
Serial.print(row, DEC);
88+
Serial.print(". average = ");
89+
Serial.println(String(averages[row]));
90+
}
91+
Serial.println();
92+
93+
// the python script is set to terminate after receiving "end" message
94+
delay(500);
95+
Serial.print("end");
96+
}
97+
98+
void loop() {
99+
100+
}

0 commit comments

Comments
 (0)