I am trying to get python on my computer to talk to arduino. I have an SD card connected to the arduino (it is functional) and I am trying to get python to read all of the numbered files on that card. Right now the arduino is programmed to wait until a connected button is pushed ( so you can start the python program) then send if the card is functional (which it is) and how many files are currently on that card. This read program works jointly with another program, which writes the data it gets from a sensor to the card, making new files to record to every time it is turned on and off. It also writes down how many files are on the card to a file named “varifile.txt”. Back to the read program, the program reads this number from varifile and sends that to the python program. Then the arduino goes into the void loop, repeatedly waiting for a number to get sent to it from the python program, then reading off the file named that number through the serial port to the python program ( python is using the pyserial module) python adds each line to a string, then puts each file’s data into a dictionary under the respective number. the issue that keeps occurring is in the part where python sends the arduino the number of the file it wants to read. the arduino recieves and reads the correct file sent from python when those file numbers are between 1 and 9.
however, when you try to send a two digit number, the arduino receives and tries to read file 4900.txt. whats even odder is that when the arduino is sent 11, it receives 4901(and 12 to 4902,13 to 4903,14 to 4904, etc.). The same thing happens both when python sends the number and when the number is sent over the arduino serial monitor. That leads me to believe that the code it uses to receive the message is messed up(elegant phrasing, I know.).
That line is :
while(Serial.available()>0){
fntr += Serial.read(); //fntr stands for file number to read
But that is such a typical line, I really don’t get what is wrong there.
So that concludes my lengthy explanation, here is all the code I have accumulated(it’s a little messy):
on the arduino:
/*
SD card file dump
This example shows how to read a file from the SD card using the
SD library and send it over the serial port.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 22 December 2010
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#include <SPI.h>
#include <SD.h>
String varifile;
const int chipSelect = 10;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(8,OUTPUT);
pinMode(9,INPUT);
while(digitalRead(9)==LOW);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File dataFile = SD.open("varifile.txt");
if (dataFile) {
while (dataFile.available()) {
char weep = dataFile.read();
varifile += weep;
}
dataFile.close();
}else{
Serial.println("it's your favorite error!!!! fun!!!!oh, right....");
}
Serial.println(varifile);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
}
void loop()
{
String fntr = "";
while(Serial.available()==0);
delay(25);
while(Serial.available()>0){
fntr += Serial.read();
}
int t = fntr.toInt()-'0';
if(fntr == "format"){
for(int i=0;t>i;i++){
SD.remove(i + ".txt");
}
SD.remove("varifile.txt");
return;
}
else{
File dataFile = SD.open(String(t) + ".txt");
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
Serial.write(dataFile.read());
}
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening "+String(t)+".txt");
}
}
Serial.println("$");
}
and, although I think it has nothing to do with the message error, the python code:
import serial
import time
import struct
ser = serial.Serial('com17',115200)
while ser.inWaiting()== 0:
time.sleep(0.1)
time.sleep(1)
print ser.readline()
print "beginning read of all files. hopefully it works."
toint = ser.readline()
INTEGERS = int(toint)
INTEGERS = INTEGERS + 1
PROGRESS = 80/INTEGERS
print INTEGERS
atwr = {}
for x in range(1,INTEGERS):
print "#" * PROGRESS*x
ser.write(struct.pack('>B', x))
while ser.inWaiting()== 0:
time.sleep(0.1)
read = " "
wwr = []
while(read[0] != "$"):
read = ser.readline()
if(read[0]=="e"):
print "error at "+str(x)+".txt. this means that either it doesn't exsist, or something bad has happened."
else:
wwr.append(read)
atwr[x] = wwr
print atwr
print "read done."
ser.close
(you’ll notice I put in a progress bar of hashtags)
Please bear in mind that everything other than this serial issue with the number works perfectly
any help you can give is greatly appreciated!
Thanks,
The hermanoid,age 12.