I have a simple question regarding writing to a file.
The example in Codesys is
2: ( write text in the file )
filewrite.hFile:=hFile;
filewrite.pBuffer:=ADR(filestring);
filesize1:=SIZEOF(filestring);
filewrite.szSize:=filesize1;
filewrite.udiTimeOut:=100000; ( 100ms Timeout )
filewrite(xExecute:=TRUE);
IF filewrite.xDone THEN
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF filewrite.xError THEN
( error handling)
;
END_IF
why isn't the write reinitialized each time during the program loop? It seems that you would never get to set the done flag.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes I realized that the FB responds to the rising edge of the execute flag. It seems the code before the write call would be executed needlessly every program loop.
would it be better to include a "IF filewrite.xExecute = FALSE" before defining the size, buffer address etc.?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
File operations are async. Meaning that they may last way more than one cycle till done.
Just pass the correct parameters when your xexecute is rising from 0 to 1.
Then you should not stop it till the done( may be error) flag. Otherwise you could have inconsistent file writings.
On the exemple provided, the xexecute flag is not reseted because when it's done or when the file is not open for a write, the write function is not called.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a simple question regarding writing to a file.
The example in Codesys is
2: ( write text in the file )
filewrite.hFile:=hFile;
filewrite.pBuffer:=ADR(filestring);
filesize1:=SIZEOF(filestring);
filewrite.szSize:=filesize1;
filewrite.udiTimeOut:=100000; ( 100ms Timeout )
filewrite(xExecute:=TRUE);
IF filewrite.xDone THEN
uiDemoStatus:=uiDemoStatus+1;
END_IF
IF filewrite.xError THEN
( error handling)
;
END_IF
why isn't the write reinitialized each time during the program loop? It seems that you would never get to set the done flag.
You're right. Somewhere should also be a fileWrite(xExecute := FALSE);
Otherwise it will only write once.
Yes I realized that the FB responds to the rising edge of the execute flag. It seems the code before the write call would be executed needlessly every program loop.
would it be better to include a "IF filewrite.xExecute = FALSE" before defining the size, buffer address etc.?
File operations are async. Meaning that they may last way more than one cycle till done.
Just pass the correct parameters when your xexecute is rising from 0 to 1.
Then you should not stop it till the done( may be error) flag. Otherwise you could have inconsistent file writings.
On the exemple provided, the xexecute flag is not reseted because when it's done or when the file is not open for a write, the write function is not called.