Announcement

REXYGEN forum has been moved. This one is closed.
Head over to the new REXYGEN Community Forum at https://forum.rexygen.com.

Looking forward to meeting you there!
 

#1 2018-10-18 13:22:40

rexpeter
Member
Registered: 2016-09-06
Posts: 67

REXLANG for Logging File (CSV) with header

Hi,

I have created a subsystem based on the "logging" example "0203-05 Logging to Text File".
LoggingToFile.png

I have created an Auto File Name and data string input and Filename Prefix input.
The Auto File Name is simply adding a Date (and eventually Time) stamp to the file name.
As the prefix of the file name remains the same, only the date (DD) or the hour (HH) is changing, a new file will automatically been created.

Section where the filename is generated:

		Filename = name + "_" + long2str(year) + s_month + s_day + ".csv";		

The data will be stored as CSV file format.
Note: Below you will find a correction to the earlier posted example where it was still using ";" instead of "," as I initially was using "txt" format and forgot to change the separator to "," .

			logline = long2str(year) + "," + s_month + "," + s_day + "," + s_hour + ":" + s_minute + ":" + s_second + "," + DataStringInput + ",";

That all works great, but the only thing I cannot figure out is HOW to add a "header line" on a new generated file?

HeaderString = "YYYY" + "," + "MM" + "," + "DD" + "," + "HH" + ":" + "MM" + ":" + "SS" + "," + "DataStringInput" + ","

As my C- code skills are very limited, I simply don't know where to put this.
Of course I tried all kind of things already, but it was ending up in getting the "header line" above every sample line, or every time the system was stopped (unplugging the Raspberry and plug it in again) when the same file name was still applicable. That is not what I want.
I just want that header line appearing when the new file has been generated and before any data string has been add to it.

In order to get even some more flexibility, it would be nice to use a "Header string input" on another input of the REXlang block that will be applied as "header string" for the file generated. I have created that already in the "LoggingToFile_task.mdl" file, but he question still is WHERE to put that code in the C-file of the REXlang block???

Who can help me with that?

I have attached my mdl files and the C-file.

logging_rexlang_20181023-0824.c
LoggingToFile_task.mdl
LoggingToFile_exec.mdl

Looking forward for some help on this.

Best regards,
Peter

Last edited by rexpeter (2018-10-23 09:27:42)

Offline

#2 2018-10-24 11:53:22

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: REXLANG for Logging File (CSV) with header

Hi Peter,

here are my suggestions:

1) The C-file should be present in the project folder. And its name should be specified in REXLANG:srcname parameter. Make sure to change it.

2) It's not a good idea in every period open the file, browse it to the very end and afterwards close it. These commands can be very time consuming and it can affect the realtime REXYGEN behaviour. I suggest to check the changes in AutoFileName string and perform the OpenFile command only on change. And just after you open the file you can write the HeaderString into it - therefore it will write the header line into every newly created log file.

3) Now you have to solve the duplicity of header lines when you powercycle the RPi This is a bit tricky since the filename can change. I suggest to use the init procedure in order to find the end of file (like it is in the example "0203-05 Logging to Text File") and just if the file is empty - add the header string.

Hope that my suggestions will help you to solve your task. Let us know about your progress.

Cheers, Tomas

Offline

#3 2018-10-24 20:55:00

rexpeter
Member
Registered: 2016-09-06
Posts: 67

Re: REXLANG for Logging File (CSV) with header

Hi Tomas,

Thanks for the feedback on this.

1) I just noticed that the C-file is now having a different name because there was already one with the same name.... But for sure the name is exactly as used int he REXLANG: srcname parameter mentioned.

2) I will have a look at your proposal to leave the file open. However I specifically changed that part only to avoid "open files" or maybe "corrupt files" when the RPi suddenly would recycle power.
Up to now with about 5 processes that each log to a file every 30 seconds, is working pretty well without any issue on my RPi 3 (2018).

3) That still brings me to the same point, I don't know in which way to change the C-code of the REXLANG block that it will determine the file does already exist and therefore does NOT need a header, or the file does NOT exist and therefore a header line will be added.
I guess I have to try and error a bit more to figure this out myself in that case...

Best regards,
Peter

Offline

#4 2018-10-24 20:55:24

rexpeter
Member
Registered: 2016-09-06
Posts: 67

Re: REXLANG for Logging File (CSV) with header

Hi Tomas,

Thanks for the feedback on this.

1) I just noticed that the C-file is now having a different name because there was already one with the same name.... But for sure the name is exactly as used int he REXLANG: srcname parameter mentioned.

2) I will have a look at your proposal to leave the file open. However I specifically changed that part only to avoid "open files" or maybe "corrupt files" when the RPi suddenly would recycle power.
Up to now with about 5 processes that each log to a file every 30 seconds, is working pretty well without any issue on my RPi 3 (2018).

3) That still brings me to the same point, I don't know in which way to change the C-code of the REXLANG block that it will determine the file does already exist and therefore does NOT need a header, or the file does NOT exist and therefore a header line will be added.
I guess I have to try and error a bit more to figure this out myself in that case...

Best regards,
Peter

Offline

Board footer

Powered by FluxBB