ESP32 _ESP32_SDMode (4Bit) OpenFile return

For general Flowcode discussion that does not belong in the other sections.
stefan.erni
Valued Contributor
Posts: 758
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

A question about the file open. In the version Before, if the file does not exist on the SD card, it does not return 0.
But now if the file does not exist, it creates the file.
This is not practical. I use this to determine if a file already exists.
Is there a way to test if a file already exists?

regards

Stefan

Open file:
Snag_233b449.png
Snag_233b449.png (52.2 KiB) Viewed 915 times

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

How can I prevent existing files from being overwritten or data attached?
File open ? it always returns 0 if it has created a new one or if it has opened an existing one.

regards

Stefan

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by BenR »

Hello Stefan,

If you look in here C:\ProgramData\MatrixTSL\FlowcodeV10\CAL\ESP\ESP_CAL_File.c

Around line 193 we have this which opens the file in a mode that allows read and append write access. Unfortunatley this also seems to be creating the file if it doesn't already exist.

Code: Select all

	MX_FILE_FP_X[MX_FILE_IDX_X] = fopen (FCL_FILEPATH, "a+");

I can try to first open the file in read mode to check if the file is there, if it is then I can close the file again and then re-open in a+ mode.

Code: Select all

	//Open file for in read mode - ensure we don't create new files
	MX_FILE_FP_X[MX_FILE_IDX_X] = fopen (FCL_FILEPATH, "r");

	//if file not found and creation error
	if (MX_FILE_FP_X[MX_FILE_IDX_X] == NULL)
	{
        //ESP_LOGE(TAG, "Failed to open file for reading");
		return 1;
	}

	//Close file reference
	fclose(MX_FILE_FP_X[MX_FILE_IDX_X]);

	//Open file for append and reading
	MX_FILE_FP_X[MX_FILE_IDX_X] = fopen (FCL_FILEPATH, "a+");

Do you want to try replacing the file with this one and see if this resolves the problem for you.
ESP_CAL_File.c
(11.94 KiB) Downloaded 8 times

The file needs to live here, just copy and paste the address into the file explorer address bar.

Code: Select all

C:\ProgramData\MatrixTSL\FlowcodeV10\CAL\ESP\
If it does solve the problem for you then I'll push it out to the library updates.

It looks like CreateFile also uses OpenFile CAL C function so I've added a new CreateFile C function and updated the component to point to this. I'll roll this out with the library update if open is now working as expected.

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

I will gladly try this out. Thank you
I would like to do the following. When I start the recorder I read the card if record1.wav is present, then record2.wav and so on. Until, for example, record7.wav is not present (return a 1 instead of 0). So I know that my next file should be record7.wav and then nextnext record8.wav and so on. This works well with the "old SD-card component" Perhaps there is a better method .

after a restart returns a number for the next filename:
Snag_4492b271.png
Snag_4492b271.png (95.84 KiB) Viewed 299 times
Attachments
file_last_file_found.fcm
(3.15 KiB) Downloaded 10 times

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by BenR »

Ok fingers crossed. I had other things to push and so I've pushed the changes including the create file to the library updates so hopefully it will all work for you correctly now.

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

It's working perfect!

It is so easy to create an excel file with Flowcode. With the help of my macro "file_last_file_found.fcm" I can, for example, make two recordings and then switch off the recorder completely. When I switch the recorder on again, this macro finds the number for the next file.
I my application IMU1_3.csv
Snag_184522.png
Snag_184522.png (66.01 KiB) Viewed 280 times

Now there is only one small wish left.
Sometimes I have to create a binary file instead of an Excel file.
I can save a byte array, but that is a bit complicated because I have an integer array.
Would it be possible to add this also with ESP32_SDMode (4Bit) like the old SD-component?

AppendINTArrayToFile:
Attachments
Snag_2acbfe.png
Snag_2acbfe.png (32.13 KiB) Viewed 280 times

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by BenR »

Excellent glad it's working correctly for you now, I've now added the INT, Long and Float array read/write functions so hopefully that will help.

Please let me know if you have any issues.

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

If i append an array with number 0-19 in a loop, it's working fine.
But if i change value 10 and 11 to a special value like "\r\n" and "\n\r" in hex value
its not append the rest of the array anymore.

Set the array:
Snag_53ff7a.png
Snag_53ff7a.png (9.58 KiB) Viewed 244 times
result is perfect:
Snag_54988e.png
Snag_54988e.png (91.72 KiB) Viewed 244 times
Set the array with special char:
Snag_54d671.png
Snag_54d671.png (12.54 KiB) Viewed 244 times
result is not complet after the 0x0A0D:
Snag_55a4ce.png
Snag_55a4ce.png (112.61 KiB) Viewed 244 times

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by BenR »

Hi Stefan,

Right ok that is odd, looks to be something deep within the fputc function which is returning an error when it gets those characters. It writes both bytes in the INT before checking the error return. I can't look at the library itself to see what it's doing because it's precompiled.

I've had a go at simple ignoring the error in all of the AppendArray functions and hopefully the latest version will work ok for you now. Could be the error is just indicating an end of line character has been received.

stefan.erni
Valued Contributor
Posts: 758
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 171 times

Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return

Post by stefan.erni »

Hi Ben

It's working perfect! Thank you.

I checked it and created the array a little more elegantly.
Snag_c1bac9.png
Snag_c1bac9.png (75.98 KiB) Viewed 219 times
Snag_c1ea94.png
Snag_c1ea94.png (14.56 KiB) Viewed 219 times

Post Reply