SysFfopen, file open

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

La funzione permette l’apertura del collegamento tra la risorsa indicata dal parametro FName, ed un flusso di dati I/O stream da impiegare nelle successive chiamate alle funzioni di I/O. La funzione ritorna il puntatore alla risorsa.

Se la risorsa indicata è già aperta oppure il nome della risorsa è errato, la funzione ritorna eNULL. Se si sta aprendo un file su disco per crearlo, accertarsi che il disco sia formattato.

Sostituisce Sysfopen

Sostituisce la funzione Sysfopen modificando il tipo di parametri in ingresso da stringa a puntatore a stringa.

// La precedente dichiarazione:

    Fp:=Sysfopen('C:/Logs.txt', 'r');

// Diventa:

    Fp:=SysFfopen(ADR('C:/Logs.txt'), ADR('r'));
Information Circle

Funzione

CODESYS: Non disponibile

LogicLab: eLLabXUnified12Lib

Descrizione

FName (@STRING) Occorre indicare il nome della risorsa da utilizzare. Nel caso di un file su disco occorre indicare il file con il percorso completo (Esempio C:/Directory/File.txt). La lunghezza massima definibile è 70 caratteri.
Mode (@STRING) Occorre indicare il modo in cui la risorsa è aperta: r=read; w=write ; a=append. Per creare un file su disco, occorre eseguire l’apertura in ‘w’ o ‘a’. Per le porte seriali definire ‘rw’.

La funzione ritorna un (eFILEP) con il puntatore alla risorsa, eNULL in caso di errore.

Immagine F SysFfopen

Esempi

Come utilizzare gli esempi.

ST_Sysfopen: Al primo loop di esecuzione viene aperto in lettura il file “C:/Logs.txt” e se presente vengono letti alcuni caratteri dal file e trasferiti nel buffer RData.

ST_MemoryDump: Questo programma permette attivando da debug il comando Write di trasferire tutta l’area di backup della DB100 (Da DB100.2048 a DB100.4095) su di un file su disco. Attivando da debug il comando Read viene letto il file ed eseguito il restore dell’area.

ST_PenDriveUse: Questo esempio da utilizzarsi su SlimLine  Raspberry dimostra come gestire un pen drive connesso al sistema. Ne viene controllata la presenza e all’inserimento nel sistema se sul pen drive è presente il file desiderato ne viene letta parte del contenuto. Il sistema Raspberry può montare i più comuni file  systems (FAT, NTFS, and HFS+), il disco viene di default montato nella cartella /media/pi/<HARD-DRIVE-LABEL>.

LogicLab (Ptp116, ST_SysFfopen)
PROGRAM ST_SysFfopen
VAR
    i : UDINT; (* Auxiliary counter *)
    Fp : eFILEP; (* File pointer *)
    RData : STRING[ 32 ]; (* Read data *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_SysFfopen"
// *****************************************************************************
// It's opened the "C:/System/Logs.txt" file and readed some character from it.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // FILE READ
    // -------------------------------------------------------------------------
    // On First program loop open file and read from it.

    IF (SysFirstLoop) THEN 
        Fp:=SysFfopen(ADR('C:/Logs.txt'), ADR('r')); //File pointer
        
        IF (SysFIsOpen(Fp)) THEN
            i:=Sysfread(ADR(RData), TO_INT(SIZEOF(RData)), 1, Fp);
            Fp:=Sysfclose(Fp);
        END_IF;
    END_IF;

// [End of file]
LogicLab (Ptp116, ST_MemoryDump)
PROGRAM ST_MemoryDump
VAR
    i : INT; (* Auxiliary variable *)
    j : INT; (* Auxiliary variable *)
    CaseNr : USINT; (* Case number *)
    FPos : DINT; (* File position *)
    Fp : eFILEP; (* File pointer *)
    Offset : ARRAY[0..1] OF UDINT; (* Offset address *)
    Ptr : @USINT; (* Internal pointer *)
    AStr : STRING[ 64 ]; (* Ausiliary string *)
    SPos : UDINT; (* String position *)
    Filename : @STRING; (* Path and name dump file *)
    ENr : USINT; (* Error number *)
    Read : BOOL; (* Read command *)
    Write : BOOL; (* Write command *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_MemoryDump"
// *****************************************************************************
// The write command executes the dump of the DB100 backup memory area on the
// defined file. The read command restores it. 
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Initialize all the variables.

    IF (SysFirstLoop) THEN
        Filename:=ADR('D:/Dir/Dump.txt'); //Path and name dump file
    END_IF;

    // -------------------------------------------------------------------------
    // CHECK EXECUTION ERROR
    // -------------------------------------------------------------------------
    // On execution error the command is ended and a spy message is printed out.

    IF (ENr <> 0) THEN
        i:=SysVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('Error:%d'), USINT_TYPE, ADR(ENr));
        i:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR(', On Case:%d'), USINT_TYPE, ADR(CaseNr));
        i:=SysWrSpyData(SPY_ASCII, 0, 16#00000001, ADR('Er'), ADR(AStr)); //Ascii mode
        ENr:=0; //Error number
        CaseNr:=0; //Case number
    END_IF;

    // -------------------------------------------------------------------------
    // PROGRAM CASES
    // -------------------------------------------------------------------------
    // Execute the program cases.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // CHECK THE COMMAND
        // ---------------------------------------------------------------------
        // Check the command to execute. by setting the proper flag the related
        // command will be executed.

        0:            
        IF (Read) THEN Read:=FALSE; CaseNr:=10; RETURN; END_IF;
        IF (Write) THEN Write:=FALSE; CaseNr:=20; RETURN; END_IF;

        // ---------------------------------------------------------------------
        // READ DUMP FILE
        // ---------------------------------------------------------------------
        // Start dump file read sequence.

        10:            
        FPos:=0; //File position
        Offset[0]:=0; //Offset address
        CaseNr:=CaseNr+1; //Case number

        // ---------------------------------------------------------------------
        // Open the file in read, if file it's not present exit. The file
        // management must terminate in the case execution, always terminate
        // program execution with the file closed.

        11:
        Fp:=SysFfopen(Filename, ADR('r')); //File pointer
        IF (Fp = NULL) THEN ENr:=10; RETURN; END_IF;

        // Set file position and reads how many characters from position to end.

        IF (Sysfseek(Fp, FPos, ID_SEEK_SET) = eEOF) THEN Fp:=Sysfclose(Fp); ENr:=11; RETURN; END_IF;
        j:=SysFGetIChars(Fp); // Number of characters

        // Reading a line of report, if number of characters is equal to or
        // greater than a record length it's read completely, otherwise are read
        // only the characters available. This is a record example.

        // 0         1         2         3         4         5         6
        // 0123456789012345678901234567890123456789012345678901234567890
        // -------------------------------------------------------------
        // 00000000: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00  

        IF (j > 61) THEN j:=61; END_IF;
        i:=Sysfread(ADR(AStr), 1, j, Fp); //Read data
        FPos:=FPos+j; //File position
        Fp:=Sysfclose(Fp); //File pointer
        IF (TO_INT(Fp) = eEOF) THEN ENr:=12; RETURN; END_IF;

        // Check if read the desired number of characters.

        IF (i <> j) THEN ENr:=13; RETURN; END_IF;

        // Address acquisistion, it's the first field of record.

        IF NOT(SysVsscanf(ADR(AStr), ADR('%08x'), UDINT_TYPE, ADR(Offset[1]))) THEN ENr:=14; RETURN; END_IF;

        // If the acquired address is correct point the memory.

        IF (Offset[0] <> Offset[1]) THEN ENr:=15; RETURN; END_IF;
        SPos:=9; //String position

        // Report data read loop.

        FOR i:=0 TO 15 DO
            Ptr:=ADR(SysDataBk)+Offset[0]; //Internal pointer
            IF NOT(SysVsscanf(ADR(AStr)+SPos, ADR('%02x'), USINT_TYPE, Ptr)) THEN ENr:=16; RETURN; END_IF;
            SPos:=SPos+3; //String position
            Offset[0]:=Offset[0]+1; //Offset address
            IF (Offset[0] >= 2048) THEN CaseNr:=CaseNr+1; RETURN; END_IF;

            // Skip the column separator character.

            IF (i = 7) THEN SPos:=SPos+2; END_IF;
        END_FOR;
        CaseNr:=CaseNr+1; //Case number

        // ------------------------------------------------------------------
        // Every dump record terminates with , control them.

        12:
        IF (TO_UDINT(SysStrFind(ADR(AStr), ADR('$r$n'), FIND_DEFAULT)-ADR(AStr)) <> SPos) THEN ENr:=17; RETURN; END_IF;

        // Check if read sequence has been completed.

        IF (Offset[0] < 2048) THEN CaseNr:=CaseNr-1; RETURN; END_IF;
        CaseNr:=0; //Case number
        Read:=FALSE; //Read command

        // ---------------------------------------------------------------------
        // WRITE DUMP FILE
        // ---------------------------------------------------------------------
        // Check if file is present, if yes it will be deleted.

        20:            
        IF (SysGetFileLen(Filename) > 0) THEN
            IF (SysFileRemove(Filename)) THEN END_IF;
        END_IF;

        Offset[0]:=0; //Offset address
        CaseNr:=CaseNr+1; //Case number

        // ---------------------------------------------------------------------
        // Create a dump record.

        // 0         1         2         3         4         5         6
        // 0123456789012345678901234567890123456789012345678901234567890
        // -------------------------------------------------------------
        // 00000000: 00 00 00 00 00 00 00 00 | 00 00 00 00 00 00 00 00  

        21:
        j:=SysVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%08X:'), UDINT_TYPE, ADR(Offset[0]));

        // Eseguo report dati in memoria.

        FOR i:=0 TO 15 DO
            Ptr:=ADR(SysDataBk)+Offset[0]; //Internal pointer
            j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR(' %02X'), USINT_TYPE, Ptr);
            Offset[0]:=Offset[0]+1; //Offset address
            IF (Offset[0] >= 2048) THEN CaseNr:=CaseNr+1; RETURN; END_IF;

            // Add the column separator character.

            IF (i = 7) THEN
                j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%s'), STRING_TYPE, ADR(' |'));
                SPos:=SPos+2; //String position
            END_IF;
        END_FOR;
        CaseNr:=CaseNr+1; //Case number

        // ---------------------------------------------------------------------
        // Open the file in append, if file it's not present it will be created.
        // The file management must terminate in the case execution always
        // terminate program execution with the file closed.

        22:
        Fp:=SysFfopen(Filename, ADR('a')); //File pointer
        IF (Fp = NULL) THEN ENr:=20; RETURN; END_IF;

        // Write data record on file.

        j:=SysCVsnprintf(ADR(AStr), SIZEOF(AStr), ADR('%s'), STRING_TYPE, ADR('$r$n'));
        IF (Sysfwrite(ADR(AStr), TO_INT(LEN(AStr)), 1, Fp) <> TO_INT(LEN(AStr))) THEN
            ENr:=21; //Error number
            Fp:=Sysfclose(Fp); //File pointer
            RETURN;
        END_IF;

        // Eseguo chiusura file.

        Fp:=Sysfclose(Fp); //File pointer
        IF (TO_INT(Fp) = eEOF) THEN ENr:=22; RETURN; END_IF;

        // Check if write sequence has been completed.

        IF (Offset[0] < 2048) THEN CaseNr:=CaseNr-1; RETURN; END_IF;
        CaseNr:=0; //Case number
        Write:=FALSE; //Write command

        // ---------------------------------------------------------------------
        // CASE ERROR
        // ---------------------------------------------------------------------

    ELSE
        ENr:=90; //Error number
    END_CASE;

// [End of file]
LogicLab (Ptp116, ST_PenDriveUse)
PROGRAM ST_PenDriveUse
VAR
    Pulse : BOOL; (* One shot flag *)
    i : UDINT; (* Auxiliary counter *)
    Fp : eFILEP; (* File pointer *)
    PDAttached : BOOL; (* Pen drive attached *)
    TimeBf : UDINT; (* Time buffer (mS) *)
    RData : STRING[ 32 ]; (* Read data *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_PenDriveUse"
// *****************************************************************************
// This example works on a SlimLine Raspberry system, it shows how to detect if
// a Pen Drive has been attached/detached from the system.
// When attached a file is reads from it.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // CHECK THE PEN DRIVE PRESENCE
    // -------------------------------------------------------------------------
    // By default Raspberry Pi automatically mounts some of the popular file
    // systems such as FAT, NTFS, and HFS+ at the /media/pi/.
    // -------------------------------------------------------------------------
    // Every 5 seconds the pen drive presence is detected.

    IF ((SysTimeGetMs()-TimeBf) > TO_UDINT(T#5s)) THEN
        TimeBf:=SysTimeGetMs(); //Time buffer (mS)
        PDAttached:=(SysGetFileLen(ADR('/media/pi/HARD-DRIVE-LABEL')) <> eEOF); //Pen drive attached
    END_IF;

    // -------------------------------------------------------------------------
    // FILE READ
    // -------------------------------------------------------------------------
    // On pen drive attaching, a file on it if exists is read.

    IF (PDAttached <> Pulse) THEN 
        Pulse:=PDAttached; //One shot flag
        
        IF (PDAttached) THEN
            IF (SysGetFileLen(ADR('/media/pi/HARD-DRIVE-LABEL/Filename')) <> eEOF) THEN
                Fp:=SysFfopen(ADR('/media/pi/HARD-DRIVE-LABEL/Filename'), ADR('r')); //File pointer

                IF (SysFIsOpen(Fp)) THEN
                    i:=Sysfread(ADR(RData), TO_INT(SIZEOF(RData)), 1, Fp);
                    Fp:=Sysfclose(Fp); //File pointer
                END_IF;
            END_IF;
        END_IF;
    END_IF;

// [End of file]
Was this article helpful?