Write to start of sd card file

For general Flowcode discussion that does not belong in the other sections.
WingNut
Posts: 254
http://meble-kuchenne.info.pl
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Write to start of sd card file

Post by WingNut »

One for Ben again. I've been struggling for quite a while with trying to write a string to the start of an open file on an sd card. The string is the first 9 characters (currently). Attached is an image of the macro that does the writing (with help from Mnfisher) It simulates effectively (writes the first sector with the string at the start). However, it only works in simulation and only if slowed to 5hz. Can you tell me if I'm wasting mine and Martins time? If it cant be done with the existing sd macros I would rather know and use an alternative solution. But this idea is the start of bigger ambitions of writing to specific locations on a file on an sd card.
Attachments
MSaveCfg.JPG
MSaveCfg.JPG (34.97 KiB) Viewed 1901 times

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Write to start of sd card file

Post by mnfisher »

This is the 'offending' program.

It seems to work in SIM ok (at least on my machine) at any speed. Time to pull out a SD card reader and see if I can get it to work in hardware.
sd config.fcfx
(18.75 KiB) Downloaded 53 times
Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Write to start of sd card file

Post by mnfisher »

Not much joy - I can't find an SD card that works and can be formatted as fat16 - I don't know what I do to them - two SD and 2 micro SD fails so far (on Arduino and in Windows)

Some EEPROM might be more reliable?

Martin

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Write to start of sd card file

Post by WingNut »

Martin I cant get yours to get past initializing the sd card tonight and now my own version wont simulate at any speed
Attachments
cfgtest.fcfx
(24.63 KiB) Downloaded 48 times

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Write to start of sd card file

Post by WingNut »

I've tried on another machine and it works fine again at 5hz but not faster. Overwriting the old cfg characters with the new

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Write to start of sd card file

Post by mnfisher »

I've just got it to work on Arduino - using software SPI (as per viewtopic.php?f=3&p=7578#p7578)

It works - with the proviso that having changed the amount of data written - to 10 x 'SOME DATA' - there is a block of crud in the middle of the file - looks like the WriteSector extends the file size to 512 bytes.

It works on first run (create file) but not if the file exists.. Hmm, back to the drawing board..

Changing back to HW and no dice...

Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Write to start of sd card file

Post by mnfisher »

Removing the WriteFileSector from ChangeConfig seems to fix it (software SPI)

Note there is a 10s delay at the start of code (allow me to fire up 'putty')
sd config.fcfx
(21.33 KiB) Downloaded 46 times
Allows change of config and appends data to file correctly (although thinking about it - I haven't checked beyond a sector boundary)

Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Write to start of sd card file

Post by mnfisher »

Still seem to be some issues - if writing more data (change to 100 x lines of data) then it grinds to a halt on the change config.

Looks like the ReadByteFromIndex(0) - attempting to read the first sector of the file is causing the stall?

BenR
Matrix Staff
Posts: 1707
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 435 times
Been thanked: 598 times

Re: Write to start of sd card file

Post by BenR »

Hello

The WriteFileSctor function is intended to write the current buffer and then add a new sector onto the file and that's why the file size is growing and corruption could be creeping in. With a load of null bytes in the file anything you then add to the end of the file likely won't ever be seen in a text editor etc.

I've added a new macro called UpdateFileSector which should allow you to modify existing data without doing extras like editing the size of the file etc.

Please note for this to work you would first have to create a file and append a string to get the file up to the correct size to then be able to overwrite with new data using the new UpdateFileSector macro. Hope this makes sense and helps.

WingNut
Posts: 254
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 33 times
Been thanked: 23 times

Re: Write to start of sd card file

Post by WingNut »

Thanks Ben. I still cant get it to simulate writing the data above 5hz. Not tried on hardware

Post Reply