Page 1 of 2
ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Thu Jan 25, 2024 8:57 pm
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 (52.2 KiB) Viewed 1087 times
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 11, 2024 2:07 pm
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
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 11, 2024 2:37 pm
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.
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.
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 11, 2024 3:10 pm
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 (95.84 KiB) Viewed 471 times
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 11, 2024 4:17 pm
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.
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Wed Mar 13, 2024 8:32 am
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 (66.01 KiB) Viewed 452 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:
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Fri Mar 15, 2024 10:31 am
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.
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Fri Mar 15, 2024 2:04 pm
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 (9.58 KiB) Viewed 416 times
result is perfect:
- Snag_54988e.png (91.72 KiB) Viewed 416 times
Set the array with special char:
- Snag_54d671.png (12.54 KiB) Viewed 416 times
result is not complet after the 0x0A0D:
- Snag_55a4ce.png (112.61 KiB) Viewed 416 times
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 18, 2024 10:01 am
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.
Re: ESP32 _ESP32_SDMode (4Bit) OpenFile return
Posted: Mon Mar 18, 2024 10:49 am
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 (75.98 KiB) Viewed 391 times
- Snag_c1ea94.png (14.56 KiB) Viewed 391 times