File management - Write

Alle Fragen zur Programmierung, die nicht in die speziellen Themen passen
Antworten
Seecosmea
Foriker
Beiträge: 27
Registriert: So 17. Sep 2017, 15:05
Wohnort: Leer (Ostfriesland)
Kontaktdaten:

File management - Write

Beitrag von Seecosmea » So 15. Okt 2017, 13:51

Haveing following problem:
My Gambas program is saving temperatur reading from OneWire temp sensor „28FF0BE7B31605D6“. into the file“/media/pi/TIMO/Speicher_oben.txt“.

Everything is good working up to that point. My intention is that the file is not getting to big and therefore I have the Idea that everytime the program is saving a temperature reading – that in the same moment the program should delete the oldest temperature reading. I would like to have around 500 readings into mention file. The second advanctage of mention procedure would be that gnuplot (graph teminal program) is exact showing the last 4 days of reading. - not more not less, exact what I would like to see.

Pls note that the temp readings inside the file are one below the other due to the code: Write #hFile, Speicher_oben.text & Format((Now), " ddmmyyhhnn") & Chr(10). Mention background is that my GnuPlot graph is understanding the string data in this way only.

I'am somhow a beginner in Gambas and in programming, I appreciate your comments and assistance in English and / or German.

the file is looking like this:

23.8 1110170219
25.8 1110170220
23.8 1110170220
28.8 1110170220
23.8 1110170221
23.8 1110170221
24.1 1110170254
24.1 1110170254
24.1 1110170254
24.1 1110170255
24.1 1110170255
25.3 1110170701
25.3 1110170701
25.3 1110170702
25.2 1110170702
25.3 1110170702
25.3 1110170703
25.4 1110170713
25.4 1110170713
25.4 1110170714
25.4 1110170714
25.4 1110170714
25.1 1110171147
23.9 1110171235
23.9 1110171237

vuott
Foriker
Beiträge: 82
Registriert: Fr 16. Jan 2015, 01:27
Kontaktdaten:

Re: File management - Write

Beitrag von vuott » So 15. Okt 2017, 16:21

As every row is a text-string, you should use "string" functions.

Here a very little and simple example (it erases oldest row, if the rows > 8 ).
I considered that every line in your file consists of 16 ASCII characters. gambas code
Public Sub Main()
  
  Dim s As String
  Dim b As Byte
  
' it creates a new clean text file:
  File.Save("/tmp/a", Null)
  
  Do   ' ...cyclus ad infinitum !

    s = File.Load("/tmp/a")
    Print s
    If Len(s) = 8 * 16 Then s = Right(s, Len(s) - 16)
    File.Save("/tmp/a", s & String(15, Chr(48 + b)) & "\n")   ' 15 characters + &h0A = 16  ASCII characters 
    Inc b
    Sleep 1
  Loop
  
End

Benutzeravatar
4tionov
Site Admin
Beiträge: 234
Registriert: So 18. Mai 2014, 22:40
Kontaktdaten:

Re: File management - Write

Beitrag von 4tionov » So 15. Okt 2017, 18:08

Seecosmea hat geschrieben:
So 15. Okt 2017, 13:51
Everything is good working up to that point. My intention is that the file is not getting to big and therefore I have the Idea that everytime the program is saving a temperature reading – that in the same moment the program should delete the oldest temperature reading.
Gambas is good for all kind of programming, but sometimes I think its better to leave things to other programs, especially when Unix tools do this job good for a very long time. In this case I personally would let logrotate and cron do this job.

Write a file .logrotate with this content:
shell code
compress 

/media/pi/TIMO/Speicher_oben.txt { 
    rotate 2
    daily 
} 
And a cronjob: shell code
57 4 * * *      logrotate -s /media/pi/TIMO/.logrotate_state /media/pi/TIMO/.logrotate
And you're done.

For the commands and possibilities do
shell code
man logrotate
Alles Gute,

4tionov

Seecosmea
Foriker
Beiträge: 27
Registriert: So 17. Sep 2017, 15:05
Wohnort: Leer (Ostfriesland)
Kontaktdaten:

Re: File management - Write

Beitrag von Seecosmea » Di 24. Okt 2017, 15:48

So, habe beide Sachen ausprobiert - Funktioniert beides gut - Vielen Dank :)
Viele Gruesse aus India - Cochin!

Antworten

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 4 Gäste