| | nasirgul 04-24-2008, 09:16 AM Hello,
Thank you very much for you peoples efforts in mine previous work.
Please let me know about the NMEA Simulator which I am working on nowadays. I want to develop a simulator which will get data from NMEA.txt file and will show on screen. Mine code is extracting data which I placed in timer and I am facing two problems .
1. when I use this str = sr.ReadToEnd(); it will show all mine txt file but it is not updating continuously.
2. When I use this str = sr.ReadLine();it shows only first line of txt file and it is continuously updating.
All i want to keep on updating continuously mine txt file data which I will use to mine application.
private void timer1_Tick(object sender, EventArgs e)
{
try
{
FileStream gul = new FileStream("C:/GPS.txt.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader nasir = new StreamReader(gul);
string str = nasir.ReadLine();
//str = nasir.ReadLine();
while (str != null)
{
txtResults.Text = str;
str = nasir.ReadLine();
//str = nasir.ReadToEnd();
}
}
Hope to hear from u guys.
Regards.
nasir Wilfried 04-28-2008, 04:02 AM Hi,
Your code example read the file line by line by ReadLine() until the end of file is reached. since you do it in a timer event the next time the same file is completely read, so if it is updated then you also have updated information. Do I understeand you question well? If not please rephrase. nasirgul 04-28-2008, 04:35 AM Hi,
Thanks for yours reply.Well when I use it in public Form1() str = nasir.ReadLine(); and this in timer1_TicktxtResults.Text = str;it only reads first line. With using str = nasir.ReadToEnd(); it show me whole text file. I am not able to understand how to show mine text file line by line with some timer delay as I fixed it to 1.. I am also using array but when I insert in for() loop and want to show as result txtResults.Text = str[i]; it shows only last line.
Please let me know how to do it.
regards.
nasir Wilfried 04-28-2008, 07:27 AM Hi,
I understand what you wants to do now. But there can be a problem. ReadLine() will work, but if another program updates the file it is possible that ReadLine() will forever will return null. Not sure of it, but this can be an untested situation and may change.
You can however close the file, and after a timer expires open it again and read it complete. But file will always get bigger and bigger so your program will slow down in time. Also you have to count the lines to see if a new line is appended.
There is also another problem. If you read the last line in the middle where the other program is updating it then you possible have only part of the line.
Question: is the program that write to the file also from you as author or do you have the source ? Because then easy synchronization can be done. but let me know first. nasirgul 04-28-2008, 07:55 AM Hello,
many thanks for yours reply. Well I got control on it. what I did as in public
protected FileInfo theSourceFile = null;
protected StreamReader reader = null;
protected string text = " "; // assigned to allow first line to be read below
then in timer i fixed this
private void timer1_Tick(object sender, EventArgs e)
{
if (text != null)
{
text = reader.ReadLine();
//Console.WriteLine(text);
txtResults.Text = text;
}
Now it keep on updating mine NMEA text file and when the file ends it restart it again.
Well yours question I am not going to write any file just using text file and mine software will act as simulator which keep on reading from the file to get GPGGA data and extract Lat & Lon and show it on map or you can say act as dummy don t need of any hardware.
Now I will try to extract GPGGA data from the updating file and get link with map.
Nasir nasirgul 04-29-2008, 03:51 AM Hello,
Please let me know how to read text file again, if mine present text file ends up. As for looping the reading of file I tried else, in timer but it is not restarting mine text file again.
How can I get it to read the text file in a continuous loop instead of just ending at the end of the text file?
Hope to hear from u guys.
Regards.
nasir nasirgul 04-30-2008, 04:58 PM Hello,
Any body have an experience of using text file as I am able to read it line by line properly in timer but i need to read it again after it has finished. I use if & else. When mine file has finished it goes to else but I don't know how I can read it back.
Regards.Nasir Wilfried 05-01-2008, 03:51 AM Hi,
Use ReadLine() to read again. You don't tell us what the problem is. nasirgul 05-01-2008, 04:33 AM Hi,
Thanks.
When I use readline(); in timer it keeps on showing me text file line by line by using protected string text = " "; // assigned to allow first line to be read below
it works fine but when my file ends up its show error
An unhandled exception of type 'System.NullReferenceException' occurred in NMEA.exe
Additional information: Object reference not set to an instance of an object.
i tried for readline();again in else but how it will update mine location again as previously timer was doing.
I want mine file to start again after it ends up.
Regards.
Nasir Wilfried 05-01-2008, 06:04 AM Hi,
ReadLine() returns null at end of file. You have to check for null pointers like this:
if (text != null)
// do something with text nasirgul 05-01-2008, 06:45 AM Hi,
Thank u very much once again.I already did this..
if (text != null)
{
//Console.WriteLine(text);
text = reader.ReadLine();
txtResults.Text = text;
}
But still mine file ends up after mine text has finished. If I will use this code in if
text = reader.ReadLine();
if (text != null)
{
//Console.WriteLine(text);
txtResults.Text = text;
}
then mine program shifts to else after mine code end up. So I am not able to get how to restart mine text file again automatically.
Thanks for yours kind help.
Regards.
Nasir Wilfried 05-01-2008, 01:03 PM when the return value is null then you are at the end of the file. in a timer tick you can check at regularly intervals if someone has appended a line to the file. you also can do this with ReadLine, when it return null then nothing is appended, when it returns data then something is appended.
note that 'something' is not necessarily a complete line, it can be part of a line also, so you have to check for that also, and if the line is not complete then wait a moment and read again. | |