FTPClient, connects to a FTP server

List

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

Questo blocco funzione da eseguire in task Back, permette di gestire la connessione al server FTP definito in FTPServer con username definito in User e password definita in Password.

Attivando Store il file locale definito in LocalFile viene trasferito nel server FTP con il nome indicato in RemoteFile, terminato il trasferimento si attiva l’uscita Done che rimane attiva fino alla disabilitazione del comando.

Attivando Retrieve il file nel server FTP con il nome indicato in RemoteFile viene trasferito in locale con il nome definito in LocalFile, terminato il trasferimento si attiva l’uscita Done che rimane attiva fino alla disabilitazione del comando.

Attivando Delete il file nel server FTP con il nome indicato in RemoteFile viene cancellato e si attiva l’uscita Done che rimane attiva fino alla disabilitazione del comando.

In caso di errore l’esecuzione comando termina e si attiva per un loop l’uscita Fault e si attiva l’uscita Done che rimane attiva fino alla disabilitazione del comando.

Upgrade list

FTPClient_v4

Eliminato parametro Timeout, aggiunto parametri TRatio, ETime e CRetry, in uscita. Migliorata la gestione del resume sui comandi di Store e Retrieve.

Datalogger con trasferimento automatico file su server FTP

Il programma FTPDataLogger (Download) esegue l’acquisizione di 2 ingressi analogici ed ad ogni minuto ne esegue la storicizzazione dei valori su di un file in formato CSV, ogni ora il file di log viene automaticamente trasferito su di un server FTP remoto nel cloud.

Il programma può essere modificato per le proprie esigenze acquisendo un numero maggiore di ingressi analogici o tramite l’FB ModbusMaster acquisendo valori da strumenti con il protocollo Modbus. Una particolarità del programma e l’utilizzo del FB FIFOFile che storicizza i dati nel registro FIFO durante il trasferimento del file di log sul server FTP remoto in modo da non perdere le registrazioni.

Dare una occhiata al programma sorgente è utile per apprendere non solo come utilizzare il FB FTPClient ma anche per comprendere come gestire il file system del sistema eseguendo la scrittura di file in formato CSV.

Information Circle

Blocco funzione

CODESYS: Non disponibile

LogicLab: eLLabNetworkLib

Descrizione

Store (BOOL) Comando trasferimento file da directory locale a server FTP.
Retrieve (BOOL) Comando trasferimento file da server FTP a directory locale.
Delete (BOOL) Comando cancellazione file su server FTP.
SpyOn (BOOL) Se attivo permette di spiare il funzionamento della FB.
FTPServer (@STRING) Puntatore stringa definizione server FTP.
FTPPort (UINT) Definizione porta connessione a server FTP.
User (@STRING) Puntatore stringa definizione nome utente.
Password (@STRING) Puntatore stringa definizione password.
LocalFile (@STRING) Puntatore stringa definizione nome del file locale.
RemoteFile (@STRING) Puntatore stringa definizione nome del file su server FTP. Se nel nome vengono definite cartelle (Esempio /DirA/DirB/File.txt), il FB provvederà alla loro creazione.
Done (BOOL) Si attiva al termine della esecuzione comando. Rimane attivo fino alla disabilitazione di tutti i comandi, per eseguire un nuovo comando disabilitare e poi riabilitare il comando.
Fault (BOOL) Attivo per un loop se errore esecuzione.
TRatio (REAL) Percentuale di trasferimento effettuata.
ETime (TIME) Ritorna il tempo impiegato esecuzione comando.
CRetry (UDINT) Numero di tentativi effettuati.
Errors (UDINT) Contatore errori di esecuzione.

Immagine FB FTPClient_v4

Spionaggio funzionamento

Se SpyOn attivo è possibile utilizzare utilizzare la console di spionaggio per verificare il funzionamento della FB. Sono previsti vari livelli di triggers.

Trigger di spionaggio
16#00000001Tx: Comandi inviati al server FTP.
16#00000002Rx: Stati risposta dal server FTP.
16#10000000Lg: Messaggi di log funzionamento.
16#40000000Er: Errori di esecuzione.

Analizzare i messaggi visualizzati nella console di spionaggio è un aiuto per comprendere eventuali errori nelle sequenze.

Messaggi in spionaggio
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

Esempi

Come utilizzare gli esempi.

ST_FTPClient: Viene gestita la connessione ad un server FTP gestendo il comando di Store per inviare un file dalla cartella locale del sistema al server FTP. Il comando Retrieve per trasferire un file dal server FTP alla cartella locale del sistema. Il comando Delete per cancellare il file sul server FTP.

ST_FTPFileTransfer: Esempio di trasferimento di un file da un server FTP ad un’altro, il file viene appoggiato sul disco locale. Nell’esempio come server FTP sono stati utilizzati due terminali Weintek.

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?