Get the last record in a CSV file
Posted
#1
(In Topic #531)
Regular

Boy am I happy I found Gambas3 and this forum :-)
After programming VB6 and VB.net for many years, moved to Python for many of the control and MQTT "internet of things" projects that I have.
I also run Linux, which limited me from good GUI applications. Until now.
Gambas is awesome, light, fast, and perfect for building GUI applications.
I run into some snags with the slight nuance differences in the BASIC syntax but that's not a big issue.
The current project is the building of a GUI dashboard to display various parameters in a greenhouse.
Light, Temperature, pumps on/off.
Currently am struggling with a CSV file and was hoping to find a few members with some experience with what I'm trying to do.
One of the greenhouse sensors publishes data to a .csv file.
The text format is: date, time, temperature (with no header headings).
Like this….
27/12/2020,18:23:33,13.62
27/12/2020,18:23:49,13.62
27/12/2020,18:24:04,13.62
27/12/2020,18:24:20,13.62
I was planning to use the CsvFile library from the internal gb.util
but I can't find any documentation on how to use it.
I have included the cb.lib in my project and coded the Opening of my file like this….
===============================
Public myCSVfile As CsvFile
' Load file
myCSVfile = New CsvFile(Application.Path & "/" & "Greehouse_Sensor_Data.txt")
Do Until myCSVfile.Eof
< SOME CODE HERE>
Loop
The goal of this project is to grab the last line (specifically the last temperature reading) in the csv file.
The file gets updated every 15 seconds, but I just need to grab the last temperature reading every 10 minutes or so in order
to check it against alert/alarm settings.
Simply put:
How do I grab the last data item in a CSV file regardless of the number of records in the file?
It is not important to me whether or not the the CsvFile / gb.util method is used as long as I can read the last data point in the file.
thanks kindly
Posted
Regular

sarpomira said
…How do I grab the last data item in a CSV file regardless of the number of records in the file?
It is not important to me whether or not the the CsvFile / gb.util method is used as long as I can read the last data point in the file…
Take a look at this post, but do come back if you still have a problem: Gambas One - Gambas ONE
Posted
Guru

Have a look at the attached program and see if that helps.
<IMG src="https://www.cogier.com/gambas/LastCSV.png">
</IMG>EDIT
Here are a few comments for the code.
Code (gambas)
- Timer1.Trigger 'The timer is set for 10 mins so this just gets it run once
- With GridView1
- .Clear
- .Columns.Count = 3
- .Rows.Count = 1
- .Columns[0].Title = "Date"
- .Columns[1].Title = "Time"
- .Columns[2].Title = "Temp"
- .Columns.Width = -1 'Auto adjusts the Column widths
Posted
Regular

sarpomira said
…but I just need to grab the last temperature reading every 10 minutes or so in order
to check it against alert/alarm settings…
It also occurs to me that if you only read the last (most recent) record, it might be easier to build the csv file the other way up, i.e. add the most recent record to the top of the file each time.
Posted
Regular

cogier, that example you constructed works great. Wow.
cheers
Posted
Regular

With a few mods, your code works also.
Here's how I did it.
Thanks
Code (gambas)
- 'Requires the addition of the gb.util library
- '************** LOAD FILE *************
- 'read every line into a collection
- 'count the number of records using the Inc-rement method (adds +1 to the iRecords variable)
- Inc iRecords
- 'Get the Field cound of the csv file
- aFields = myCSVfile.Fields
- 'Get the Number of lines (Records)
- iRecords = myCSVfile.line
- '********** DISPLAY DATA ***********
- Me.Text = "Greenhouse Sensor Data: " & "Field count: " & aFields.Count & ", Record count: " & iRecords
- 'Prints the number of records
- txtRecordCount.Text = iRecords
- 'Print a full record (In this case the last one)
- ThisRecord = iRecords
- txtLastElement.text = colRecords[thisRecord]["Temperature"]
1 guest and 0 members have just viewed this.


