Hey all, I am currently working on a project where I am trying to read data from a file into an array of string and then convert those strings into reals. The program is reading the document but the values are being read into only the first row of the array like this:
you have to split the file content by your self using the line sperator '$R$N'. Here is a very simple example:
PROGRAMPLC_PRGVAR  buffer    :STRING:='6.41052162$R$N11.52562684$R$N';  num    :ARRAY[1..3]OFREAL;  lineStart  :INT;  pos      :INT;  line    :STRING;  numIndex  :INT;END_VARVARCONSTANT  lineSep    :STRING(2):='$R$N';END_VAR
lineStart :=1;numIndex :=1;FORpos :=1TOLEN(buffer)-1DO
  IFbuffer[pos-1] =lineSep[0] ANDbuffer[pos] =lineSep[1] THEN
    line :=MID(buffer, pos-lineStart, lineStart);
    num[numIndex] :=STRING_TO_REAL(line);
    numIndex :=numIndex+1;
    lineStart :=pos+LEN(lineSep);
  END_IFEND_FOR
Tip: Read the file contents into an array of byte and then work with SysMemCmp and SysMemCpy.
I had also to do byte array parsing, and I've some question about the exemple code :
MID, is a string function. Isn't it limitated to 255 bytes long ? (meaning your file won't be fully read if containing more than 255 chars)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
dFx hat geschrieben:
I had also to do byte array parsing, and I've some question about the exemple code :
MID, is a string function. Isn't it limitated to 255 bytes long ? (meaning your file won't be fully read if containing more than 255 chars)
Yes. My example should only clarify the functionality. I would do the following:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hey all, I am currently working on a project where I am trying to read data from a file into an array of string and then convert those strings into reals. The program is reading the document but the values are being read into only the first row of the array like this:
str[1] output is '6.41052162$R$N11.52562684$R$N'
rather than splitting into separate rows. Any guidance would be much appreciated, this is all new to me.
PROGRAM PLC_PRG
VAR
hFile : SysFile.RTS_IEC_HANDLE;
error : POINTER TO SysFile.RTS_IEC_RESULT;
pBuffer : POINTER TO BYTE;
fileSize : __XWORD;
accessM : SysFile.ACCESS_MODE;
fileName : STRING;
readFile : __XWORD;
pArray : ARRAY [0..4] OF POINTER TO STRING;
str : ARRAY [1..3] OF STRING;
// pStr : POINTER TO ARRAY [0..2] OF STRING;
num : ARRAY [1..3] OF REAL;
END_VAR
fileName := 'azi_data.txt';
accessM := SysFile.AM_READ;
fileSize := SysFile.SysFileGetSize(szFileName := fileName , error);
hFile := SysFile.SysFileOpen(fileName,accessM,error);
readFile := SysFile.SysFileRead(hFile,ADR(str),SIZEOF(str),error);
//pStr := ADR(fileSize);
num[1] := STRING_TO_REAL(str[1]);
num[2] := STRING_TO_REAL(str[2]);
Related
Talk.ru: 1
Talk.ru: 2
Hi,
you have to split the file content by your self using the line sperator '$R$N'. Here is a very simple example:
Tip: Read the file contents into an array of byte and then work with SysMemCmp and SysMemCpy.
Related
Talk.ru: 1
I had also to do byte array parsing, and I've some question about the exemple code :
MID, is a string function. Isn't it limitated to 255 bytes long ? (meaning your file won't be fully read if containing more than 255 chars)
Yes. My example should only clarify the functionality. I would do the following:
Thank you very much! I'll see what I can do with the information given.