Reading multiple lines from csv file

jami
2025-07-18
2025-07-23
  • jami - 2025-07-18

    Hello, i am trying to read multiple lines from csv file with caa file library and oscat. I have wrote 7 lines in the csv with separation '$R$L'. In my "extracting values" part I check line feeds and chars. After that I convert my buffer to string with oscat but I'm only able to read the first line from the csv. No matter if I even change start position where I start converting the buffer, I only get the first line.

    Here's my code for the reading and extracting value parts:

            4: (*Reading the file*)
    
                fileread.hFile := filehandle;
                fileread.pBuffer := ADR(buffer);
                filesize1:=SIZEOF (buffer);
                fileread.szbuffer:=filesize1;
                fileread.udiTimeOut := 100000;
                fileread(xExecute := TRUE);
                IF fileRead.xDone THEN
                    iFilesize:=TO_INT(fileread.szSize);
                    writestate:=3;
                    fileRead.xExecute := FALSE;
                END_IF
    
            5: (*Extracting values*)
    
                //here i check the number of line feeds and chars. It works
    
                WHILE i < ifilesize DO
                    c:=buffer[i];
                IF c= 10 THEN
                    IF lineindex<=99 THEN
                        lineIndex := lineIndex + 1;
                    END_IF
                ELSIF c <> 13 THEN 
                    IF charIndex <= 1000 THEN
                        charIndex := charIndex + 1;
                    END_IF
                END_IF
                i := i + 1;
                END_WHILE
    
                // Here i convert the buffer to string and transfer it to filelines:ARRAY[0..99] of string[254].
    
                trig(CLK:=BUTTON);
                    IF trig.Q  THEN
                        fileLines[i2]:=oscat_basic.BUFFER_TO_STRING(PT:=ADR(buffer), Size:=TO_UINT(fileread.szBuffer),                           start:=TO_UINT(bufferStart), stop:=TO_UINT(filesize1));
    
                        i2:=i2+1;
                        bufferstart:=bufferstart+80;
                    END_IF
    

    If anyone has idea how to read multiple lines, it would be nice. Even if you have example codes that work, that would help a lot.

     
  • TimvH

    TimvH - 2025-07-23

    Not sure, but maybe before you enter step 5, set i, lineindex and charIndex to 0.
    And the buffer variable should have a size bigger than 1 line.

    Additionally I think you should set bufferstart to 0 before you enter step 5.
    And in the OSCAT function you always point to the buffer address, but shouldn't this be (buffer + bufferstart)?

     

Log in to post a comment.