My MAIN_PROG code consists of an initialization part, in which some files are read from txt files and stored in arrays, and an operative part in which values are used.
It should run on the RTE module and should receive and send data through my CIFX card on a profiBUS. I've coded and tested it in simulation mode (card is installed at my university laboratory) a Function Block that reads a txt file in which there is a matrix; so if the file is in the form:
1 2 3
4 5 6
7 8 9
it produces a bidimensional array containing those values. The program is executed in a standard cyclic task and it is the only one executing.
Well, in simulation mode everything works; while passing to the real run on RTE, files never get read, so returned arrays are always zero.
By using a step by step debugging, I discovered that the instruction:
always produces the result: RTS_INVALID_HANDLE, even if in simulation mode this doesn't happen.
How can be this problem solved? Here is the code of the function block:
FUNCTION_BLOCKMATRIX_READERVAR_IN_OUT
  M: ARRAY[*,*] OFINT;END_VARVARCONSTANT
  MAX_BUFFER : __XWORD :=64*1024;END_VARVAR_INPUT
  FileName : STRING(255);END_VARVAR_OUTPUT
  nRows : INT:=0;
  nCols: INT:=0;END_VARVAR
  ErrCode : SysFile.SYS_FILE_STATUS :=SysFile.SYS_FILE_STATUS.FS_NO_FILE;
  BytesRead : __XWORD :=0;
  Data : ARRAY[0..MAX_BUFFER] OFBYTE;
  pResult : POINTERTOSysFile.RTS_IEC_RESULT;
  FileHandle : SysFile.RTS_IEC_HANDLE;
  pFileData : POINTERTOBYTE :=ADR(Data);
  actualValue:INT;
  values : ARRAY[1..10] OFINT;
  i: __XWORD;
  r_index: INT;
  c_index:INT;
  char: STRING;
  numEnd: BOOL;
  accString: STRING;
  minus:bool:=false;
  rowEnd: BOOL;END_VARFORi:=1TOMAX_BUFFERBY1DO
  Data[i]:=0;END_FOR//trytoopenfileFileHandle :=SysFileOpen(szFile:=FileName, am:=SysFile.AM_READ, pResult:=pResult);//verifyfoundafileIFFileHandle=SysFile.RTS_INVALID_HANDLETHEN
  //nofilefound
  ErrCode :=SysFile.SYS_FILE_STATUS.FS_NO_FILE;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_IFEND_IF//closefile!!!!SysFileClose(hFile:=FileHandle);r_index :=1;c_index :=1;accString:='';minus:=FALSE;i:=0;WHILEi<=BytesReadDO
  IFnumEndANDBYTE_TO_INT(Data[i])=45THEN
    minus:=TRUE;
    i:=i+1;
  END_IF
  numEnd:=FALSE;
  //newlineorspace
  IFBYTE_TO_INT(Data[i])=10ORBYTE_TO_INT(Data[i])=32THEN
    IFNOT(accString='')THEN
      M[r_index,c_index] :=STRING_TO_INT(accString);
      IFminusTHEN
        M[r_index,c_index] :=-M[r_index,c_index];
      END_IF
      minus:=FALSE;
      numEnd:=TRUE;
      accString:='';
      c_index:=c_index+1;
    END_IF
    IFBYTE_TO_INT(Data[i])=10THEN
      r_index:=r_index+1;
      rowEnd:=TRUE;
      nCols:=c_index-1;
      c_index:=1;
    END_IF
  ELSE
    IFNOT(Data[i]=9)ANDNOT(Data[i]=10)ANDNOT(Data[i]=13)ANDNOT(Data[i]=32)THEN
      accString :=CONCAT(accString,WORD_AS_STRING(Data[i],0));
    END_IF
  END_IF   Â
  i:=i+1;
  IFi>BytesReadTHEN
    IFr_index=1THEN
      nRows:=1;
    ELSE
      nRows:=r_index-1;
    END_IF
    IFnRows=1THEN
      nCols:=c_index-1;
    END_IF Â
    RETURN;
  END_IFEND_WHILE
 Â
Please, since i'm relatively new to Codesys, try to answer in a complete manner. Examples are rare to find and documentation is not sufficient.
Thanks,
Luigi Ferrara.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I had some similars troubles on raspberry. The problem was in files permisions.
Another problem that I had was in C300 from Lenze with rte. The problem was in file path. Use the same file path as you can see in your ide in plc part you can see your plc files.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Secondly, I don't think my problem is about permissions; in fact I am using windows with everything in run as admin. Maybe the second one you cited could be it.
So please, could you tell me more precisely what do you mean by files in the ide's plc? I'd like to have some more indications on the effective steps to do in Codesys.
Thanks,
Luigi Ferrara
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi all.
My MAIN_PROG code consists of an initialization part, in which some files are read from txt files and stored in arrays, and an operative part in which values are used.
It should run on the RTE module and should receive and send data through my CIFX card on a profiBUS. I've coded and tested it in simulation mode (card is installed at my university laboratory) a Function Block that reads a txt file in which there is a matrix; so if the file is in the form:
1 2 3
4 5 6
7 8 9
it produces a bidimensional array containing those values. The program is executed in a standard cyclic task and it is the only one executing.
Well, in simulation mode everything works; while passing to the real run on RTE, files never get read, so returned arrays are always zero.
By using a step by step debugging, I discovered that the instruction:
FileHandle := SysFileOpen(szFile:= FileName, am:=SysFile.AM_READ, pResult:=pResult);
always produces the result: RTS_INVALID_HANDLE, even if in simulation mode this doesn't happen.
How can be this problem solved? Here is the code of the function block:
Please, since i'm relatively new to Codesys, try to answer in a complete manner. Examples are rare to find and documentation is not sufficient.
Thanks,
Luigi Ferrara.
Hi.
Reading seems correct.
I had some similars troubles on raspberry. The problem was in files permisions.
Another problem that I had was in C300 from Lenze with rte. The problem was in file path. Use the same file path as you can see in your ide in plc part you can see your plc files.
Hi!
First of all thanks very much.
Secondly, I don't think my problem is about permissions; in fact I am using windows with everything in run as admin. Maybe the second one you cited could be it.
So please, could you tell me more precisely what do you mean by files in the ide's plc? I'd like to have some more indications on the effective steps to do in Codesys.
Thanks,
Luigi Ferrara
Hi.
In right tree go to device (plc). Wher the communications settings be.
Down to communications settings you can see files.
In files right window refresh. You can see tree folders. Go to your files folder and use the path ad you can see in location.
Sorry, left tree devices
Another thing. In some plcs you only can write on sdcard.
Thank you so much.
I've solved the problem using your tip and enabling all the permissions of the folder; running Codesys and RTE as ADMIN can be helpful.
Thanks again.