FTPClient, connects to a FTP server

List

This page is part of the IEC 61131-3 Programming Manual. Go to the index.

This function block from run in task Back, allows you to manage the connection to the FTP server defined in FTPServer with username defined in User and password defined in Password.

activating Store the local file defined in LocalFile is transferred to the FTP server with the name indicated in RemoteFile, once the transfer is complete, the output is activated Done which remains active until the command is disabled.

activating Retrieve the file on the FTP server with the name indicated in RemoteFile is transferred locally with the name defined in LocalFile, once the transfer is complete, the output is activated Done which remains active until the command is disabled.

activating Delete the file on the FTP server with the name indicated in RemoteFile is deleted and the output is activated Done which remains active until the command is disabled.

In the event of an error, the command execution ends and the output is activated for a loop Fault and the output is activated Done which remains active until the command is disabled.

Upgrade list

FTPClient_v4

Parameter removed Timeout, added parameters TRatio, ETime e CRetry, output. Improved resume management on commands Store e Retrieve.

Datalogger with automatic file transfer on FTP server

The FTPDataLogger (Download) performs the acquisition of 2 analog inputs and every minute performs the historicization of the values ​​on a file in CSV format, every hour the log file is automatically transferred to a remote FTP server in the cloud.

The program can be modified to suit your needs by acquiring a larger number of analog inputs or via the FB ModbusMaster acquiring values ​​from instruments with the Modbus protocol. A special feature of the program is the use of the FB FIFOFile which stores the data in the FIFO log during the transfer of the log file to the remote FTP server so as not to lose the logs.

Taking a look at the source program is useful to learn not only how to use the FB FTPClient but also to understand how to manage the system file system by writing files in CSV format.

information circle

Function lock

CODESYS: Not available

LogicLab: eLLabNetworkLib

Description

Store (BOOL) File transfer command from local directory to FTP server.
Retrieve (BOOL) File transfer command from FTP server to local directory.
Delete (BOOL) File deletion command on FTP server.
SpyOn (BOOL) If active, it allows you to spy on the operation of the FB.
FTPServer (@STRING) FTP server definition string pointer.
FTPPort (UINT) Definition of port for connection to FTP server.
User (@STRING) Username definition string pointer.
Password (@STRING) Password definition string pointer.
LocalFile (@STRING) Local file name definition string pointer.
RemoteFile (@STRING) File name definition string pointer on FTP server. If folders are defined in the name (Example /DirA/DirB/File.txt), the FB will create them.
Done (BOOL) It is activated at the end of the command execution. It remains active until all commands are disabled, to execute a new command disable and then re-enable the command.
Fault (BOOL) Active for a loop if execution error.
Treatment (REAL) Percentage of transfer made.
ETime (TIME) Returns the time taken to execute the command.
CRetry (UDINT) Number of attempts made.
Errors (UDINT) Execution error counter.

FB FTPClient_v4 image

Operation espionage

Se SpyOn active you can use the spy console to check the operation of the FB. There are various levels of triggers.

Espionage triggers
16 00000001 #Tx: Commands sent to the FTP server.
16 00000002 #Rx: Response states from the FTP server.
16 10000000 #LG: Operation log messages.
16 40000000 #He: Execution errors.

Analyzing the messages displayed in the espionage console helps you understand any errors in the sequences.

Messages in espionage
Messaggi di connessione al server validi per tutti comandi.

| FTPClient:Rx|[0010] 227 Entering Passive Mode (81,88,52,106,222,229)          Apertura connessione passiva
| FTPClient:Lg|[0010] PASV port:57061                                           Porta per connessione passiva
| FTPClient:Lg|[0022] Server do not allows the resume command                   Server non supporta il comando di resume
| FTPClient:Lg|[0022] Server allows the resume command                          Server supporta il comando di resume
| FTPClient:Lg|[0100] User logged in                                            Client connesso al server

Messaggi del comando di store.

| FTPClient:Lg|[1000] Send file: Local file => Remote file                      Informazioni sul file locale e remoto
| FTPClient:Lg|[1000] File to send length: xxxxxxx                              Dimensione file locale da inviare in bytes
| FTPClient:Tx|[1305] ABOR                                                      Abortita sequenza in corso segue un resume
| FTPClient:Lg|[1405] xxxxxxx bytes has been transferred, xx.xxx KB/S           Dimensione file inviato e velocità di invio
| FTPClient:Lg|--------------------------------------[Store end, xxx.xxx (S)]-- Fine comando e durata

Messaggi del comando di retrieve.

| FTPClient:Lg|[2002] File to receive length: xxxxxx                            Dimensione file da scaricare in bytes (Se resume)
| FTPClient:Lg|[2203] Data reception restart                                    Non si ricevono dati per 80% di Timeout, eseguo resume
| FTPClient:Lg|[2203] Server timeout restart                                    Prima di timeout server eseguo resume (Timeout*2)
| FTPClient:Tx|[2205] ABOR                                                      Abortita sequenza in corso segue un resume
| FTPClient:Lg|[2302] xxxxxxx bytes has been transferred, xx.xxx KB/S           Dimensione file ricevuto e velocità di invio
| FTPClient:Lg|-----------------------------------[Retrieve end, xxx.xxx (S)]-- Fine comando e durata

Examples

How to use the examples.

ST_FTPClient: The connection to an FTP server is managed by managing the command Store to send a file from the local system folder to the FTP server. The command Retrieve to transfer a file from the FTP server to the local system folder. The command Delete to delete the file on the FTP server.

ST_FTPFileTransfer: Example of transferring a file from one FTP server to another, the file is placed on the local disk. In the example, two Weintek terminals were used as an FTP server.

LogicLab (Ptp119)
PROGRAM ST_FTPClient
VAR
    Store : BOOL; (* FTP store command *)
    Retrieve : BOOL; (* FTP retrieve command *)
    Delete : BOOL; (* FTP delete command *)
    CaseNr : USINT; (* Program case *)
    FTP : FTPClient_v4; (* FTP client *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_FTPClient"
// *****************************************************************************
// This program connects to the SlimLine FTP server and allows to transfer file
// from local directory to FTP server and viceversa.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Program initializations.

    IF (SysFirstLoop) THEN
        FTP.SpyOn:=TRUE; //Spy active
        FTP.FTPServer:=ADR('192.168.0.230'); //Server FTP
        FTP.FTPPort:=21; //Server FTP
        FTP.User:=ADR('Admin'); //User
        FTP.Password:=ADR('Admin'); //Password
    END_IF;

    // -------------------------------------------------------------------------
    // FTP CLIENT
    // -------------------------------------------------------------------------
    // Executs the FTP client and resets the commands on execution fault.

    FTP(); //FTP client
    IF (FTP.Fault) THEN CaseNr:=0; END_IF;

    // -------------------------------------------------------------------------
    // PROGRAM CASES
    // -------------------------------------------------------------------------
    // Program cases management.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // MANAGES THE COMMAND
        // ---------------------------------------------------------------------
        // Reset commands.

        0:
        FTP.Store:=FALSE; //Store command
        FTP.Retrieve:=FALSE; //Retrieve command
        FTP.Delete:=FALSE; //Delete command

        // Check commands.

        IF (Store) THEN Store:=FALSE; CaseNr:=10; RETURN; END_IF;
        IF (Retrieve) THEN Retrieve:=FALSE; CaseNr:=20; RETURN; END_IF;
        IF (Delete) THEN Delete:=FALSE; CaseNr:=30; RETURN; END_IF;

        // ---------------------------------------------------------------------
        // STORE COMMAND
        // ---------------------------------------------------------------------
        // Command execution.

        10:
        FTP.Store:=TRUE; //Store command
        FTP.LocalFile:=ADR('C:/System/Logs.txt'); //Local file definition
        FTP.RemoteFile:=ADR('D:/Logs.txt'); //Remote file definition
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Store:=FALSE; //Store command
        CaseNr:=0; //Program case

        // ---------------------------------------------------------------------
        // RETRIEVE COMMAND
        // ---------------------------------------------------------------------
        // Command execution.

        20:
        FTP.Retrieve:=TRUE; //Retrieve command
        FTP.LocalFile:=ADR('D:/Logs.txt'); //Local file definition
        FTP.RemoteFile:=ADR('C:/System/Logs.txt'); //Remote file definition
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Retrieve:=FALSE; //Retrieve command
        CaseNr:=0; //Program case

        // ---------------------------------------------------------------------
        // DELETE COMMAND
        // ---------------------------------------------------------------------
        // Command execution.

        30:
        FTP.Delete:=TRUE; //Delete command
        FTP.LocalFile:=eNULL; //Local file definition
        FTP.RemoteFile:=ADR('D:/Logs.txt'); //Remote file definition
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Delete:=FALSE; //Delete command
        CaseNr:=0; //Program case
    END_CASE;

// [End of file]
LogicLab (Ptp119)
PROGRAM ST_FTPFileTransfer
VAR
    DTransfer : BOOL; (* Data transfer command *)
    CaseNr : USINT; (* Program case *)
    FTP : FTPClient_v4; (* FTP client *)
END_VAR

// *****************************************************************************
// PROGRAM "ST_FTPFileTransfer"
// *****************************************************************************
// This program transfers a file from one FTP server to another.
// -----------------------------------------------------------------------------

    // -------------------------------------------------------------------------
    // INITIALIZATION
    // -------------------------------------------------------------------------
    // Program initializations.

    IF (SysFirstLoop) THEN
        FTP.SpyOn:=TRUE; //Spy active
    END_IF;

    // -------------------------------------------------------------------------
    // FTP CLIENT
    // -------------------------------------------------------------------------
    // Executs the FTP client and resets the commands on execution fault.

    FTP(); //FTP client
    IF (FTP.Fault) THEN CaseNr:=0; END_IF;

    // -------------------------------------------------------------------------
    // PROGRAM CASES
    // -------------------------------------------------------------------------
    // Program cases management.

    CASE (CaseNr) OF

        // ---------------------------------------------------------------------
        // TRANSFER FILE
        // ---------------------------------------------------------------------
        // Wait for the command.

        0:
        IF (DTransfer) THEN DTransfer:=FALSE; CaseNr:=10; RETURN; END_IF;

        // ---------------------------------------------------------------------
        // READ FILE FROM THE SERVER AND DELETE IT
        // ---------------------------------------------------------------------
        // Define server credential and connects to it.

        10:
        FTP.FTPServer:=ADR('192.168.0.234'); //Server FTP
        FTP.FTPPort:=21; //Server port
        FTP.User:=ADR('uploadhis'); //User
        FTP.Password:=ADR('111111'); //Password
        FTP.LocalFile:=ADR('D:/Logs.txt'); //Local file definition
        FTP.RemoteFile:=ADR('datalog/Logs.txt'); //Remote file definition
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Retrieve command execution.

        11:
        FTP.Retrieve:=TRUE; //Retrieve command
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Retrieve:=FALSE; //Retrieve command
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Delete command execution.

        12:
        FTP.Delete:=TRUE; //Delete command
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Delete:=FALSE; //Delete command
        CaseNr:=20; //Program case

        // ---------------------------------------------------------------------
        // STORE FILE TO THE SERVER
        // ---------------------------------------------------------------------
        // Define server credential and connects to it.

        20:
        FTP.FTPServer:=ADR('192.168.0.216'); //Server FTP
        FTP.FTPPort:=21; //Server port
        FTP.User:=ADR('uploadhis'); //User
        FTP.Password:=ADR('111111'); //Password
        FTP.LocalFile:=ADR('D:/Logs.txt'); //Local file definition
        FTP.RemoteFile:=ADR('datalog/Logs.txt'); //Remote file definition
        CaseNr:=CaseNr+1; //Program case

        // ---------------------------------------------------------------------
        // Command end waiting.

        21:
        FTP.Store:=TRUE; //Store command
        IF NOT(FTP.Done) THEN RETURN; END_IF;
        FTP.Store:=FALSE; //Store command
        CaseNr:=0; //Program case
    END_CASE;

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