Q about setPos

Use this section to discuss your embedded Flowcode projects.
WingNut
Posts: 262
http://meble-kuchenne.info.pl
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Q about setPos

Post by WingNut »

I want to write a number to the very first bytes of a file. The size of the number isn't important but won't be more than ten digits.
What is the handle? Is the address 0? and is the 'from' also 0?

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Go on give me a clue

stefan.erni
Valued Contributor
Posts: 893
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 175 times
Been thanked: 206 times

Re: Q about setPos

Post by stefan.erni »

Hi WingNut

a simple way is with append

regards

Stefan
append_2021-12-09_20-15-10.png
append_2021-12-09_20-15-10.png (43.19 KiB) Viewed 5685 times

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Thanks Stefan. I want to add data to specifically the first bytes of the file. Sort of the write equivalent of ReadStringFromFile

BenR
Matrix Staff
Posts: 1901
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 494 times
Been thanked: 668 times

Re: Q about setPos

Post by BenR »

Hello,

I'm assuming you're using the FAT or FILE component and not the Simulation API.

Open file reads the first 512 byte sector of the file into ram.

Read Byte from buffer reads directly from the ram buffer, so you can read bytes 0 to 9 to get the first 10 characters of the file. Stores these into a string variable str[0] to str[9] and add a null termination at str[10].

Code: Select all

OpenFile("File.txt")
idx = 0
while (idx < 10)
{
  str[idx] = ReadByteFromBuffer(idx)
  idx = idx + 1
}
str[idx] = 0
Then convert str to an long.

Code: Select all

long = StringToInt$(Str)
Then do what you need to the long variable, e.g. add one and convert back into an string.

Code: Select all

str = ToString$(long)
You may need to padd the string with leading 0's or spaces to get it back to the right length.

Code: Select all

while (Length$(str) < 10)
{
  tempstr = "0" + str
  str = tempstr
}
Write byte to buffer allows you to write directly to the ram buffer so you can write bytes 0 to 9 from the string to set the first 10 characters of the file.

Code: Select all

idx = 0
while (idx < 10)
{
  WriteByteToBuffer(idx, str[idx])
  idx = idx + 1
}
str[idx] = 0
WriteFileSector()
Write file sector then writes the ram buffer back to the file.

Hope this helps, let us know how you're getting on.

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Thanks Ben. I'll give that a go when I get another 2 issues sorted :o

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

So before I get into saving data to the card. Can anyone help with compiler errors I'm now getting. File attached
Attachments
Flowcode1.msg.txt
(3.15 KiB) Downloaded 222 times

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Update : I took out a goto component and put it back in exactly as it was and now it compiles! ;)

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Hi Ben. So although i know c programming (somewhat) I'm not sure how to use this in flowcode. Is there tutorial available? I've looked and can't find anything with detail. I clearly cant just copy this and put it into a c code macro. Can variables declared in flowcode be accessed directly from a piece of c code or do they need to be declared in the block? Can you explain using your example?

Cheers
N

WingNut
Posts: 262
Joined: Tue Jul 13, 2021 1:53 pm
Has thanked: 35 times
Been thanked: 27 times

Re: Q about setPos

Post by WingNut »

Hi again Ben. The attached is what i understand but i know it doesn't work. Can you give me a pointer?
Attachments
MSaveCfg.PNG
MSaveCfg.PNG (20.13 KiB) Viewed 5362 times

Post Reply