TwinCAT FTP Client: Reading of a filelist from the FTP Server

This sample illustrates the reading of a filelist from the FTP Server.

To be able to run this example, you should have created a user with the name "TestUser" and the password "TestPwd123" at your FTP server.

You can start the sample with a rising edge at the variable "startstop".

At first a connection to the FTP Server will be created with the function block FB_FTP_Open. Then all files with the extension "*.txt" are loaded from the FTP server with the function block FB_FTP_FileList.

At the end the connection to the FTP Server will be closed with the function block FB_FTP_Close.

Variable declaration

PROGRAM MAIN
VAR
    RisingEdge          : R_TRIG;
    startstop           : BOOL;

    state           : BYTE;

    FB_FTP_Open1        : FB_FTP_Open;
    FB_FTP_FileList1        : FB_FTP_FileList;
    FB_FTP_Close1       : FB_FTP_Close;

    busy            : BOOL;
    err             : BOOL;
    errid           : UDINT;

    handle          : T_HFTP;
    
    FileList            : ARRAY [0..MAX_FILELIST_ITEMS] OF STRING;
    Items           : UDINT;
END_VAR

PLC Program

RisingEdge(CLK:= startstop);
IF RisingEdge.Q THEN
    state := 1;
END_IF

CASE state OF
    0:
        ;

    1:
        FB_FTP_Open1(
            sNetID:= '5.0.252.142.1.1',
            sHost:= '172.16.9.223',
            nPort:= 21,
            sUsername:= 'TestUser',
            sPassword:= 'TestPwd123',
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> busy,
            bError=> err,
            nErrId=> errid,
            hFTP=> handle);

        IF NOT busy AND NOT err THEN
            FB_FTP_Open1(bExecute:= FALSE);
            state := 2;
        END_IF

    2:
        FB_FTP_FileList1(
            sNetID:= '5.0.252.142.1.1',
            hFTP:= handle,
            sMask:= '*.txt',
            nIndex:= 0,
            pList:= ADR(FileList),
            cbList:= SIZEOF(FileList),
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> busy,
            bError=> err,
            nErrID=> errid,
            nItems=> Items);

        IF NOT busy AND NOT err THEN
            FB_FTP_FileList1(bExecute:= FALSE);
            state := 3;
        END_IF

    3:
        FB_FTP_Close1(
            sNetID:= '5.0.252.142.1.1',
            hFTP:= handle,
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy => busy,
            bError => err,
            nErrId => errid);

        IF NOT busy AND NOT err THEN
            FB_FTP_Close1(bExecute:= FALSE);
            state := 0;
        END_IF

END_CASE    

The "TcFTPClient.lib", "TcSystem.lib", "TcBase.lib" and "STANDARD.lib" libraries are to be linked.