Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

read csv into string array help

burnzy
2018-02-22
2018-02-26
  • burnzy - 2018-02-22

    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

  • mirasoft - 2018-02-25

    Hi,

    you have to split the file content by your self using the line sperator '$R$N'. Here is a very simple example:

    PROGRAM PLC_PRG
    VAR
       buffer      : STRING := '6.41052162$R$N11.52562684$R$N';
       num       : ARRAY [1..3] OF REAL;
       lineStart   : INT;
       pos         : INT;
       line      : STRING;
       numIndex   : INT;
    END_VAR
    VAR CONSTANT
       lineSep      : STRING(2) := '$R$N';
    END_VAR
    
    lineStart := 1;
    numIndex := 1;
    FOR pos := 1 TO LEN(buffer) - 1 DO
       IF buffer[pos - 1] = lineSep[0] AND buffer[pos] = lineSep[1] THEN
          line := MID(buffer, pos - lineStart, lineStart);
          num[numIndex] := STRING_TO_REAL(line);
          numIndex := numIndex + 1;
          lineStart := pos + LEN(lineSep);
       END_IF
    END_FOR
    

    Tip: Read the file contents into an array of byte and then work with SysMemCmp and SysMemCpy.

     

    Related

    Talk.ru: 1

  • dFx

    dFx - 2018-02-26

    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)

     
  • mirasoft - 2018-02-26

    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:

     
  • burnzy - 2018-02-26

    Thank you very much! I'll see what I can do with the information given.

     

Log in to post a comment.