Hello and I will explain how to use TF6300 to implement a FTP client to upload file to an FTP server.It is very easy to configure and program.
Let’s go!
Installation
Please access the following link the download the function setup file.
Double the Exe file.
Choose English.
Next>.
Accept the license and Next>.
Enter the Information and Next>.
Choose Complete setup and Next>.
Next>.
Install.
Please wait a minute..
Finished.
Configuration
Here is my configuration in this tutorial. I will configure the raspberry pi4 as a FTP Server and using TwinCAT TF6300 to implement a FTP client, then transfer the text files.
Do not worry that you can see how to configure the raspberry pi4 side also.
System Requirment
Add Reference
Open your XAE and Add the Tc2_FTP library.
FB_FTP_FILEIPLOADEX
We can upload files to a FTP-Server with this function block FB_FTP_FileUpload. The connection mode”passive FTP” will be used.
VAR_INPUT
sNetID | T_AmsNetId := ”; | AMS network ID |
sHost | T_IPv4Addr := ‘127.0.0.1’; | A string containing the IPv4 address of the FTP-Server. |
nPort | UDINT:= 21; | FTP port (default 21). |
sUsername | STRING := ”; | Username for the FTP Server authentication |
sPassword | STRING := ”; | Password for the FTP Server authentication |
sSrcFile | T_MaxString := ”; | Source file which will be copied to the FTP Server. |
sDesFile | T_MaxString := ”; | Path and name of the destination file on the FTP Server |
bExecute | BOOL | The command is executed with the rising edge. |
tTimeout | TIME := T#15s; | States the time before the function is cancelled. |
VAR_OUTPUT
bBusy | BOOL | True=FB is executing, no new command can be received. |
bError | BOOL | True=Error |
nErrID | UDINT | The Error information |
nProgress | UDINT | current status of the data transmission |
Raspberry Setup
Now we will configure the Raspberry pi as the FTP Server.
Install vsftpd
The vsftpd package is used here.
Please update your pi4 and install that package.
sudo apt-get update sudo apt-get install vsftpd |
Edit vsftpd.conf
open the .conf file.
sudo nano /etc/vsftpd.conf |
uncomment the following setting.
anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 chroot_local_user=YES |
add these line in the end of your .conf file.
user_sub_token=$USER local_root=/home/$USER/ftp |
FTP Directory for Pi User
create a ftp directory for pi users.
mkdir /home/pi/ftp mkdir /home/pi/ftp/files |
Change the permission of that directory.
chmod a-w /home/pi/ftp |
Restart
Restart your FTP Server and Finish!
sudo service vsftpd restart |
Server test
FileZilla is used in this tutorial to test the connection between Server and Client.
https://filezilla-project.org/download.php?type=client#close
if the connection is established, the following message is shown.
Program
Now we will implement the program to upload the file to FTP Server and here is the class diagram.
File to Raspberry
This is the file that I planned to upload to FTP Server.
FB_FTPClient
VAR
FUNCTION_BLOCK FB_FTPClient VAR_INPUT END_VAR VAR_OUTPUT END_VAR VAR FTPFIleUpload:FB_FTP_FileUploadEx; END_VAR |
PROPERTY DestFilePath : String
The property to set/get the Destination of the path.
GET
DestFilePath:=FTPFIleUpload.sDesFile; |
SET
FTPFIleUpload.sDesFile:=DestFilePath; |
PROPERTY ErrorID : UDINT
The property to get the ErrorID of the Function Block
GET
ErrorID:=FTPFIleUpload.nErrId; |
PROPERTY Host : String
The property to set/get the IPv4 of the FTP Server.
GET
Host:=FTPFIleUpload.sHost; |
SET
FTPFIleUpload.sHost:=Host; |
PROPERTY Password : String
The property to set the password for the FTP user login..
SET
FTPFIleUpload.sPassword:=Password; |
PROPERTY Port : UDINT
The property to set/get the port setting of your Function Block..
GET
Port:=FTPFIleUpload.nPort; |
SET
FTPFIleUpload.nPort:=Port; |
PROPERTY Progress : UDINT
The property to set/get the current Progress of the Function Block.
GET
Progress:=FTPFIleUpload.nProgress; |
PROPERTY SrcFilePath : String
The property to set/get the Setting of the source file path in the Function Block.
GET
SrcFilePath:=FTPFIleUpload.sSrcFile; |
SET
FTPFIleUpload.sSrcFile:=SrcFilePath; |
PROPERTY UserName : String
The property to set/get the username setting for the FTP server in the function block.
GET
UserName:=FTPFIleUpload.sUsername; |
SET
FTPFIleUpload.sUsername:=UserName; |
METHOD Send:DINT
Here is the method to receive a Bool parameter and execute the Function Block.
VAR
METHOD Send : DINT VAR_INPUT bSend:BOOL; END_VAR |
Code
FTPFIleUpload( bExecute:=bSend ); Send:=16#0; IF FTPFIleUpload.bBusy THEN Send:=16#1; END_IF IF FTPFIleUpload.bError THEN Send:=16#2; END_IF |
MAIN
VAR
VAR FTPUpload:FB_FTPClient; bSend:BOOL; END_VAR |
Code
FTPUpload.UserName:=’pi’; FTPUpload.Password:=’raspberry’; FTPUpload.Host:=’192.168.3.14′; FTPUpload.Port:=DEFAULT_FTP_PORT; FTPUpload.SrcFilePath:=’C:\FTPFiles\text.txt’; FTPUpload.DestFilePath:=’/files/text.txt’; FTPUpload(); FTPUpload.Send(bSend); |
Result
Set the bSend to true and start to send the file to FTP Server.
you can use FileZilla to check if the file is transfered or not.
you can also check the file by using cat command in Raspberry pi.
Touble – 16×0707
0x707 is an error to indicate the Device is not ready to operate.
please enter the 7-days trial license of TF6300.
Touble 16×8226
Please check if your file path is correct or not.
Sample Code
please download the source code from this link:
https://github.com/soup01Threes/TwinCAT3/blob/main/TF6300_Demo01_UploadFile.tnzip