How to extract datas from a file

bmattlet
2011-08-25
2011-08-29
  • bmattlet - 2011-08-25

    Hi,

    I'd like to read a file that'd be on the PLC memory to extract datas from it such as initial values for counters, host name, ip addresses, filenames and stuff like that so I could change propreties without compiling the program on CoDeSys.

    I know that SysFileOpen/Read/Write exist but I don't see any function or programm that reads through the file and copies the datas from it.

    Is it possible ?

    Thank you for any help

     
  • David

    David - 2011-08-26

    Sorry, but I do not find out your exact problem.
    Do you need an example how to read/wrie data from a file?

     
  • ramashu - 2011-08-28

    Hi. I have the some problem (Question).
    I have a file (CVS) in the SD Card and I tried to read it, but I can't.
    Do somebody help me with an example how to use the sysfilelib (sysfileread, sysfilewrite)

    Thanks in advance

     
  • David

    David - 2011-08-29

    Please find a small example to write a csv-File with help of SyslibFile:
    PROGRAM PLC_PRG
    VAR
    hFile:DWORD;
    dwWritten: DWORD;
    szFilePath: STRING:= 'C:\test.csv';
    szSeperator: STRING(1):= ';';
    szCurrent: STRING(2);
    wNewLine: WORD:= 2573; (16#0A0D)
    iCounter1: INT;
    bOnce: BOOL:= TRUE;
    iCounter2: INT;
    END_VAR
    IF bOnce THEN
    hFile:= SysFileOpen(szFilePath, 'w');
    FOR iCounter2:= 1 TO 5 DO
    FOR iCounter1:= 1 TO 5 DO
    szCurrent:= CONCAT(INT_TO_STRING(iCounter2), INT_TO_STRING(iCounter1));
    dwWritten:= SysFileWrite(hFile, ADR(szCurrent), 2);
    IF iCounter1 <> 5 THEN
    dwWritten:= SysFileWrite(hFile, ADR(szSeperator), 1);
    ELSE
    dwWritten:= SysFileWrite(hFile, ADR(wNewLine), SIZEOF(wNewLine));
    END_IF
    END_FOR
    END_FOR
    SysFileClose(hFile);
    bOnce:= FALSE;
    END_IF

    READ can be done like this:

    IF xDoRead THEN
    dwReturn := SysFileRead(File:=hFile, Buffer:=ADR(sBufferIn), Size:=dwSize);
    IF dwReturn <> 0 THEN
    sBuffer := sBufferIn;
    END_IF
    xDoRead := FALSE;
    END_IF

    IF xDoReadLine THEN
        dwReturn := SysFileRead(File:=hFile, Buffer:=ADR(sBufferIn), Size:=1);
        IF dwReturn <> 0 THEN
            IF sBufferIn <> '$R' THEN
                sBuffer := CONCAT(STR1:=sBuffer, STR2:=sBufferIn);
            ELSE
                (* Dummy Read *)
                SysFileRead(File:=hFile, Buffer:=ADR(sBufferIn), Size:=1);
                dwLine  := dwLine + 1;
                sBuffer := '';
            END_IF
        END_IF
    END_IF
    

    The read result must be adapted to the syntax of the written file.

    Writing can be done also with help of SysfileStream library.
    PROGRAM PLC_PRG
    VAR
    hFile: DWORD;
    diReturn:DINT;
    NeueZ:STRING:='$n'; ( Neue zeile )
    Zeile: INT;

       ValueReal: REAL:=1.23;
       hFileDelete: BOOL;
       FileName:STRING:='D:\temp\w2test.txt';                 (* Speicher ort auf SD Karte *)
        bWriteSystemText: BOOL;
    

    END_VAR

    IF bWriteSystemText THEN

                bWriteSystemText:=FALSE;
    
                hFile:=SysFileStreamFOpen(FileName, 'w');
    
                IF hFile > 0 THEN
    
                               FOR Zeile:=1 TO 20 DO
                                                               diReturn:=SysFileStreamFPrintf_Real(hFile, '%6.2f', ValueReal);
                                                               diReturn:=SysFileStreamFPrintf_String(hFile, '%s', ADR(NeueZ));
                               END_FOR;
    
                               diReturn:=SysFileStreamFClose(hFile);
                               hFile:=0;
                END_IF;
    

    END_IF;

     
  • bmattlet - 2011-08-29

    Hi,

    I don't need an example to write datas, I think mine works fine, I have trouble reading a file only.

    I have an example to write/read datas with the application note that Wago made on how to use SysFileLib and WagoLibFtp (to send a file on a FTP) but...

    What is my problem :

    I have a program that writes measures in a file and then send the file on a FTP server. What I'd like to do is : using an FTP client or an SD card, read a configuration file that contains the host ip, password, name of sensors and that kind of a information so I can run the same program on different PLCs and using different sensors without editing the program and only compiling it.
    what I can't do is reading the file line by line to extract the host ip, password, etc.

    Did I explain well?

    Thank you for your help.

     
  • bmattlet - 2011-08-29

    little add-on to my explanation :

    It says "The return value is the number of successfully read bytes" for the SysFileLib function. but I need the datas or the string that I'm reading...

    To do something like that :

    admin:=ReadMyConfigFile(filename, paramater1, parameter2,...);
    password:=ReadMyConfigFile(filename, paramater1, parameter2,...);
    
     

Log in to post a comment.