reading from a "dynamic" textfile

Raddict

Active member
Joined
May 19, 2005
Messages
28
Location
Rotterdam (Holland)
Programming Experience
3-5
Hi,

I have a textfile where constantly (about every 10 sec.) text is being added (a log from a temperature sensor).

I can read the last added line with a VB.NET program.
But I can't get it to repeat this over and over again, without restarting the program or reopen it with my fileopen dialog.

I use this code to read the file:
VB.NET:
		dlgFile.opendialog
		filename=dlgFile.filename

		Try
			Dim sr As StreamReader = New StreamReader(filename)

			Do
				line= sr.ReadLine
			   
			    wanted_value=line  'only the last line is preserved


			Loop Until regel Is Nothing
			sr.Close()
		Catch ex As Exception

		End Try

apperantly the file is not closed properly, so the progam does not open the updated file but the old one when I run the try to end try part again with my Timer.

Can anyone tell me how to fix this
 
While there are many available solutions for your problem i would suggest using either FileSystemWatcher class contained in System.IO and System.Diagnostics classes (This class can raise events when a file is created, renamed, updated or deleted from the specified folder) or you can use timer control for the purpose. Maybe even building windows service but it's not worthy for such small task. About the timer. If you already know the timing of file updating then you could check timer and re-open file on each 10 seconds althrough i couldn't find much sense why you want to open that file :(

Regards ;)
 
Last edited:
Well the reason that I want to open that file is that I have this Temperature/humidity sensor on a USB 1-wire network.
I also have a piece of third party software which logs the data from the sensors in the followinf format:

VB.NET:
000 T 10-9-2005 23:33:07 026 194 246 246   
000 H 10-9-2005 23:33:13 007 000 131 131   
000 T 10-9-2005 23:33:20 026 192 176 176   
000 H 10-9-2005 23:34:02 006 249 252 252   
000 T 10-9-2005 23:34:09 026 201 161 161   
000 H 10-9-2005 23:34:15 006 253 223 223   
000 T 10-9-2005 23:34:21 026 200 045 045   
000 H 10-9-2005 23:34:27 006 251 186 186   
000 T 10-9-2005 23:34:33 026 209 254 254   
000 H 10-9-2005 23:34:39 006 246 136 136   
000 T 10-9-2005 23:34:45 026 213 221 221   
000 H 10-9-2005 23:34:51 006 239 091 091   
000 T 10-9-2005 23:34:57 026 215 155 155   
000 H 10-9-2005 23:35:03 006 238 215 215   
000 T 10-9-2005 23:35:10 026 210 052 052   
000 H 10-9-2005 23:35:15 006 240 237 237   
000 T 10-9-2005 23:35:21 026 210 052 052   
000 H 10-9-2005 23:35:27 006 240 237 237   
000 T 10-9-2005 23:35:34 026 209 254 254   
000 H 10-9-2005 23:35:40 006 244 206 206

In this case sensor 000 T=temp H=humidity date time MSB LSB CRC CRC and if both CRC's are different a 0 otherwise a space.

I want to be able to use these values for my climate control.

I cannot find how to use the Dallas semiconductors 1-wire protocol on VB.NET so for now I can not directly read the data from the sensor myself.

I want my progam to periodicly read the newest/current temperature/humidity.
My progam reads the logfile, determens the newest entry, this because I only use the last two lines from the logfile.

But with the fileopen dialog it opens the file and read correctly the stream is closed and the timer is activated (interval=15000 ms=15 sec).
In the timer event I have the exact same code without the fileopen dialog but I use the original filename. But it does not have an other output.

For testing purposes I also used: console.writeline(File.GetLastWriteTime(filename))
this didn't reveal that the file has changed, while I know it has.

VB.NET:
	Public filename As String

	Public Function RV(ByVal Sensor_output As String) As String
	  
	Public Function TEMP(ByVal Sensor_output As String) As String
	
	Public Function DewPoint(ByVal RV As String, ByVal Temp As String) As String
	   
	Private Function ToInteger(ByVal x As Long) As String
	
	Private Function ToBinary(ByVal x As Long) As String
	   
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		filename = ""
		dlgOpen.ShowDialog()
		filename = dlgOpen.FileName
		GetData()
		Timer1.Enabled = True
	End Sub

	Private Sub GetData()
		Dim line As String
		Dim RV_sensor As String
		Dim Temp_sensor As String
		Cursor = Cursors.WaitCursor
		Try
			Dim sr As StreamReader = New StreamReader(filename)

			Do
				line = sr.ReadLine
			 If line.Substring(line.Length - 2) = " " Then	'both CRS's are the same so the data is correct
				 If line.Substring(4, 1) = "T" Then 'filter the Temperature data
					 Temp_sensor = line
					Else										 'filter the Humidity data
					 RV_sensor = line
					End If
				End If
			Loop Until line Is Nothing
			sr.Close()									    'close stream
		Catch ex As Exception

		End Try
		lblRV.Text = RV(RV_sensor)
		lblTemp.Text = TEMP(Temp_sensor)
		lblDewPoint.Text = DewPoint(lblRV.Text, lblTemp.Text)
		Cursor = Cursors.Arrow
	End Sub

	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		GetData()
	End Sub
 
Hi,
Why you have used openFileDialog at all? All you need is to read entire content from text file from time to time. means if you go for timer: if timer counted down 10 sec. clear the loaded values and reload with StreamReader.ReadToEnd method then just proceed it again.

Dim strContents As String
Dim objReader As StreamReader
objReader = New StreamReader(Path)
strContents = objReader.ReadToEnd()
objReader.Close()



Regards ;)
 
Thanks a lot Kulrom.

I discovered what went wrong!!

I was playing around with your code and it worked, so why didn't mine?
Then I discovered that if I would place the "sr.close()" and the "dim sr as streamreader" outside the try loop it closes the textfile properly for reopening it.

P.S. I only use the fileopen dialog the first time (form_load) otherwise I have to specify the filelocation in the software.
on the timer event I use the filename variable which is set in form_load.
 
Back
Top