arduino recieves 4900 when sent 10

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.

It would help a lot if you would post the serial logs on both sides. Seeing the code and being able to understand it is one thing. But not knowing what the data is that it operates on still makes it hard to find a logical fault in your programs. So, show the contents of the varfile.txt file, preferably in hexadecimal formatting. And the logs of what it sent between the two programs.

This line is probably the cause of why you get 4900 when sent 10 and 4901 when set 11.

at the beginning of the arduino loop: at least it is the cause of the last digit.

int t = fntr.toInt()-'0';

If the character ‘1’ is sent and this gets put into fntr then it’s turned into the integer number 49 (decimal) and is subtracted with the number 48 (which represents character ‘0’).

But to me it is not clear in which order the numbers are sent. It’s unclear if those 2 digit numbers are supposed to mean the number ‘10’, or an individual number ‘1’ followed by an individual number ‘0’. I hope the logs clear up why the first ‘1’ digit is converted to 49 and the second digit as above.