TwinCAT FTP Client: Removing a file from the FTP Server

This sample illustrates the removing of an existing file from the FTP Server.

At first a connection to the FTP Server will be created with the function block FB_FTP_Open. After that the function block FB_FTP_FileExist checks if the file "\TestFolder\File1.txt" is available on the FTP Server. The function block FB_FTP_FileRemove deletes the specified file. At the end the connection to the FTP Server will be closed with the function block FB_FTP_Close.

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

Variable declaration

PROGRAM MAIN
VAR
    RisingEdge      : R_TRIG;
    startstop           : BOOL;

    state       : BYTE;

    FB_FTP_Open1    : FB_FTP_Open;
    FB_FTP_FileExist1       : FB_FTP_FileExist;
    FB_FTP_FileRemove1      : FB_FTP_FileRemove;
    FB_FTP_Close1   : FB_FTP_Close;

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

    handle      : T_HFTP;

    exist       : BOOL;
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:= DEFAULT_FTP_PORT,
            sUsername:= 'TestUser',
            sPassword:= 'TestPwd123',
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> busy,
            bError=> err,
            nErrID=> errid,
            hFTP=> handle);

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

    2:
        FB_FTP_FileExist1(
            sNetID:= '5.0.252.142.1.1',
            hFTP:= handle,
            sFile:= '\TestFolder\File1.txt',
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> busy,
            bError=> err,
            nErrID=> errid,
            bExist=> exist);

        IF NOT busy AND NOT err THEN
            IF exist THEN
                state := 3;
            ELSE
                state := 4;
            END_IF
            FB_FTP_FileExist1(bExecute:= FALSE);
        END_IF
    3:
        FB_FTP_FileRemove1(
            sNetID:= '5.0.252.142.1.1',
            hFTP:= handle,
            sFile:= '\TestFolder\File1.txt',
            bExecute:= TRUE,
            tTimeout:= T#15s,
            bBusy=> busy,
            bError=> err,
            nErrID=> errid);

        IF NOT busy AND NOT err THEN
            state := 4;
            FB_FTP_FileRemove1(bExecute:=FALSE);
        END_IF
    4:
        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
            state := 0;
            FB_FTP_Close1(bExecute:=FALSE);
        END_IF
END_CASE

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