TwinCAT FTP Client: Downloading a file from a FTP Server to an ADS device
This sample will illustrate the downloading of files from a FTP Server.
To execute this sample you have to create a user with the username "TestUser" and the password "TestPwd123" on the FTP Server.
If you set a rising edge at the variable "startstop", the downloading will be started.
At first a connection to a FTP Server will be opened with the function block FB_FTP_Open. After that the given file "\TestFolder\File1.txt" from the FTP Server will be downloaded with the function block FB_FTP_FileDownload. The following path "\Program Files\TestFile1.txt" is the place where the file will be stored on the ADS device.
At the end the connection to the FTP Server will be closed with the function block FB_FTP_Close.
Notice that the user "TestUser" has rights for reading and writing.
Variable declaration
PROGRAM MAIN
VAR
RisingEdge : R_TRIG;
startstop : BOOL;
state : BYTE;
FB_FTP_Open1 : FB_FTP_Open;
FB_FTP_FileDownload1 : FB_FTP_FileDownload;
FB_FTP_Close1 : FB_FTP_Close;
busy : BOOL;
err : BOOL;
errid : UDINT;
handle : T_HFTP;
progress : 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_FileDownload1(
sNetID:= '5.0.252.142.1.1',
hFTP:= handle,
sSrcFile:= '\TestFolder\File1.txt',
sDesFile:= '\Program Files\TestFile1.txt',
bExecute:= TRUE,
tTimeout:= T#15s,
bBusy => busy,
bError => err,
nErrId => errid,
nProgress => progress);
IF NOT busy AND NOT err THEN
FB_FTP_FileDownload1(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.