Hello All,

I am working with a program the reads data from a file and plots it to a graph in real time. My stream is coming from a microcontroller output and I am building an interface to display the data. I am using stream reader in my routine, but I have a problem.

What I want to do is to get one line, plot it, and the get the next line inside a method. Can you please direct me as to what to use to do this. I am new to C# and I think stream reader is the best way to do this. I just need a way of being able to read new points as they come.

Thanks'

private void timer1_Tick( object sender, EventArgs e )

if ( curve == null )
return;

 IPointListEdit list = curve.Points as IPointListEdit;

 if (list == null)
   return;

double time = (Environment.TickCount - tickStart) / 1000.0;
try
 {
 //PROBLEM (NEED TO READ ONE LINE THIS TIME AND THE NEXT LINE IN THE NEXT CALL
StreamReader sr = new StreamReader("TestFile.txt");
String line;     
line = sr.ReadLine();
 double value = double.Parse(line);
 list.Add(time, value);
    }
 }
 catch (Exception err)
  {
  MessageBox.Show(err.Message);
}
          
//graphing code
}

Declare a global integer variable index=0
in timer tick write this code

/*Your code */
index++;
int i=0;
String line; 

try
 {
 //PROBLEM (NEED TO READ ONE LINE THIS TIME AND THE NEXT LINE IN THE NEXT CALL
StreamReader sr = new StreamReader("TestFile.txt");
while ((line = sr.ReadLine()) != null && i<= index)
{
if(i==index)
{
double value = double.Parse(line);
list.Add(time, value);
}
else i++;
    }
sr.Close();
 }
 catch (Exception err)
  {
  MessageBox.Show(err.Message);
}

Use this Code this will help you

Edited 5 Years Ago by abelLazm: n/a

This will read lines and get new ones that are added to the file as they are added. You'll have to come up with a way to end the loop though :)

FileStream ins = File.Open(@"d:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(ins);
String line;

while (true) {
                
    if (sr.EndOfStream == false) {
        line = sr.ReadLine();
        if (!String.IsNullOrEmpty(line)) {
            Console.WriteLine(line);
        }
    }
}

Depending on the speed of the writes to the file, you may have issues with partial lines. Flush after every write.

Edited 5 Years Ago by Momerath: n/a

This article has been dead for over six months. Start a new discussion instead.