Reading Text Files

Hi all,

I've searched google and can't find any examples of Arduino reading a text file.

is it possible to get arduino to read lines from a text file. The text file only contains 3 lines of strings, and each line is only about 9 characters in length

e.g. 356:21:45

Thanks,
Ribuck.

From what medium do you want to read them?

Is it a SDcard, a file on the computer which is send via serial, or maybe a file on a webserver?

With kind regards,

Atmoz

Hi Atmoz,

It's a text file on a pc.

Rich.

First you have to send the text file to your Arduino via the serial port.
You know how to do this?

You can send files via "Hyperterminal" or you can write a small application yourself. For example in Borland Delphi.

Then your Arduino has to check the incoming string and take it apart.
But if you write your own application for sending text files, maybe you could better take the file apart in the program running at your computer and send 3 different strings via serial.

In the Arduino you receive them in 3 different variables, and you got what you want!!

Succes with it :slight_smile:

Atmoz

Hi Atmoz,

How would arduino determin which string it was recieving ? When i send the strings whould i just use a different character to terminate each string ?

And then tell Arduino to look for each serial string terminating with that Character ?

Do you have any examples or Arduino reading Serial to different string values ?

If you want to send a text file with

356:21:45

in it, then you're program on the computer can split this into

356

21

45

Before sending it to the Arduino you can add for example

! for the first string
@ for the second

for the third

So the computer sends

!356

then

@21

and then

#45

The Arduino looks what the first character is, and then put it into the right variable :slight_smile:

something like this:

void ReadSerial(){

   if (Serial.available() == 2) {

   incomingByte = Serial.read();
   incomingByte2 = Serial.read();
 
     if (incomingByte == '!'){
     a = incomingByte2
     }

etc....

Still having a few problems controlling the data flow through the serial port.

I wrote an external program to send the data to the serial port every second and also write it to a file.

The file output looks like this : (note values are updated every second)

01/11/2009 19:56:50!
12:44:12#

But when watching the data through the serial port the data starts out ok then just looks to get messed up.

01/11/2009 19:55:28

12:44:12

01/11/2009 19:55:29

12:44:13

01/11/2009 19:55:30

12:44:14

01/11/2009 19:55:31

00:44:15

01/11/200:01/11/2000
/11/200
01/11/20001/11/200001
11/20001/11/20001/11/
00901/11/20001/11/200
01/11/20001/11/200901
11/200001/11/20001/11
200

Here is my code

Basically my external program will send 3 values, which i can contol the time interval and terminating character. I would like to be able to display these 3 different values on 3 different positions on an LCD screen.

I just don't know how to control the data properly and read it into 3 different variables so that it can then be displayed properly

const int MAX_LEN = 21; // the max digits 
char  strValue[MAX_LEN + 1]; // add one for the terminating null

int index = 0;


void loop()
{
  delay (100);
  if( Serial.available())
  {
    char ch = Serial.read();
    if(index < MAX_LEN && ch != 13) // 13 is ascii value of carriage return
    {
      strValue[index++] = ch; // add the ASCII character to the string;
    }
    else
    {
      // here when buffer full or on the first non digit
        strValue[index] = 0;        // terminate the string with a 0
        //char value = atof(strValue);  // use atof to convert the string to a float
      index = 0;      // reset the index for the next string
  
lcd.setCursor(0,0);
Serial.println(strValue);
lcd.print(strValue);
lcd.setCursor(0,0);
lcd.print(" ");



    }
  }

Thanks for the Help,
Rich.

if(index < MAX_LEN && ch != 33) // 13 is ascii value of carriage return

33 decimal (0x21) is the ASCII code for exclamation mark "!"
Much easier to write the character value you want between single quotes:

 if(index < MAX_LEN && ch != '\r')

Please use the "Code" (#) button when posting code.

Hi Awol,

dont know if oyu noticed but i had updated the code, after i noticed the type, i was actually uaing character 13 for a carrige return.

The problem i have is how the heck do i read the the second lot of characters being streamed through the serial port into a second variable and then print at a different position on the lcd.

Rich.

Anyone got any idea's as i'm really struggling here.

Rich.