I want to read a text file containing two INT pairs into a two dimension INT array. The text files will be created externally and copied onto the controller when updated. This will be a headless system (no display).
Does anyone have an ST program example of opening and reading a text file? I downloaded an example used in the help file, but it's not very helpful. I have a lot of programming experience with multiple languages but am new to CodeSys and IEC PLCs.
TIA!
Gyz
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2016-02-21
Originally created by: scott_cunningham
You will need to use the library SysFile and the functions: SysFileOpen, SysFileRead and SysFileClose. Here is a sample view of a ReadFile program. I suggest adding an FB_EXIT() method (see online help) to the ReadFile program which calls SysFileClose - that helps avoid file handles being left open when the system is interrupted... Hope this helps!
HINT: Some hardware platforms do not support AM_APPEND modes (typically flash based memory devices that don't have hard drives/CF cards/SD cards).
PROGRAMReadFileVARCONSTANT
  MAX_BUFFER : __XWORD :=999;END_VARVAR_INPUT
  FileName : STRING :='c:/test.txt';END_VARVAR_OUTPUT
  Done : BOOL :=FALSE;
  ErrCode : SysFile.SYS_FILE_STATUS :=SysFile.SYS_FILE_STATUS.FS_NO_FILE;
  Data : ARRAY[0..MAX_BUFFER] OFBYTE;
  BytesRead : __XWORD :=0;END_VARVAR
  pResult : POINTERTOSysFile.RTS_IEC_RESULT;
  FileHandle : SysFile.RTS_IEC_HANDLE;
  pFileData : POINTERTOBYTE :=ADR(Data);END_VAR//trytoopenfileFileHandle :=SysFileOpen(szFile:=FileName, am:=AM_READ, pResult:=pResult);//verifyfoundafileIFFileHandle=SysFile.RTS_INVALID_HANDLETHEN
  //nofilefound
  ErrCode :=SysFile.SYS_FILE_STATUS.FS_NO_FILE;
  Done :=TRUE;ELSE
  //trytoreadfileout
  BytesRead :=SysFileRead(hFile:=FileHandle, pbyBuffer:=pFileData, ulSize:=MAX_BUFFER, pResult:=pResult);
 Â
  //checkifactuallyreadanybytes
  IFBytesRead>0THEN
    ErrCode :=SysFile.SYS_FILE_STATUS.FS_OK;
  ELSE
    ErrCode :=SysFile.SYS_FILE_STATUS.FS_NO_FILE;
  END_IF
  Done :=TRUE;END_IF//closefile!!!!SysFileClose(hFile:=FileHandle);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have a some questions not answered in the help file that I'd like to ask.
Q1: Is the data read from the file altered in any way, such a stripping off line terminations (cr/lf)?
Q2: Do the Open/Read/Close functions block execution until completed (success or fail), or do I have to set up a loop and wait for them to complete?
Thanks! I really appreciate your help.
Gyz
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2016-02-29
Originally created by: scott_cunningham
To answer your questions:
Q1: I think the characters are still there for you to parse. You will need to make a small test - I don't 100% remember.
Q2: The open/read/writes are function calls and so they execute and that's it - no need to "wait". Double check how long the execute time is - depending on size of read and hardware platform....
For embedded systems you typically need to read smaller chunks instead of a large file.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
it seems that the code of scott_cunningham does exactly what I want to do in my application. However, I got error messages in the variable declaration section (e.g. in this row
ErrCode: SysFile.SYS_FILE_STATUS := SysFile.SYS_FILE_STATUS.FS_NO_FILE;):
"Identifier SysFile not defined".
In the program section there the functions from the SysFile are linked to the Sysfile library. I tried to find this library, but it seems to be very well hidden.
My Codesys version is V3.5 and I'm also new in Codesys programming.
Anyone has got a hint for me?
Greetings!
Oliver
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OK I found the errors:
1. the SysFile library was not activated. It took me quite a while to locate it. It is within the Data_Server library.
2. after the activation of the Data_server library in my project, I had to relocate the SysFile variables.
Now there are no errors anymore and I can test the functionality of the code
Thanks so far!
Oliver
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I want to read a text file containing two INT pairs into a two dimension INT array. The text files will be created externally and copied onto the controller when updated. This will be a headless system (no display).
Does anyone have an ST program example of opening and reading a text file? I downloaded an example used in the help file, but it's not very helpful. I have a lot of programming experience with multiple languages but am new to CodeSys and IEC PLCs.
TIA!
Gyz
Originally created by: scott_cunningham
You will need to use the library SysFile and the functions: SysFileOpen, SysFileRead and SysFileClose. Here is a sample view of a ReadFile program. I suggest adding an FB_EXIT() method (see online help) to the ReadFile program which calls SysFileClose - that helps avoid file handles being left open when the system is interrupted... Hope this helps!
HINT: Some hardware platforms do not support AM_APPEND modes (typically flash based memory devices that don't have hard drives/CF cards/SD cards).
That was very helpful - thanks!
I have a some questions not answered in the help file that I'd like to ask.
Q1: Is the data read from the file altered in any way, such a stripping off line terminations (cr/lf)?
Q2: Do the Open/Read/Close functions block execution until completed (success or fail), or do I have to set up a loop and wait for them to complete?
Thanks! I really appreciate your help.
Gyz
Originally created by: scott_cunningham
To answer your questions:
Q1: I think the characters are still there for you to parse. You will need to make a small test - I don't 100% remember.
Q2: The open/read/writes are function calls and so they execute and that's it - no need to "wait". Double check how long the execute time is - depending on size of read and hardware platform....
For embedded systems you typically need to read smaller chunks instead of a large file.
Hello,
it seems that the code of scott_cunningham does exactly what I want to do in my application. However, I got error messages in the variable declaration section (e.g. in this row
ErrCode: SysFile.SYS_FILE_STATUS := SysFile.SYS_FILE_STATUS.FS_NO_FILE;):
"Identifier SysFile not defined".
In the program section there the functions from the SysFile are linked to the Sysfile library. I tried to find this library, but it seems to be very well hidden.
My Codesys version is V3.5 and I'm also new in Codesys programming.
Anyone has got a hint for me?
Greetings!
Oliver
OK I found the errors:
1. the SysFile library was not activated. It took me quite a while to locate it. It is within the Data_Server library.
2. after the activation of the Data_server library in my project, I had to relocate the SysFile variables.
Now there are no errors anymore and I can test the functionality of the code
Thanks so far!
Oliver