UnderAutomation
Any question?

[email protected]

Contact us
UnderAutomation
⌘Q

File management

Upload, download, delete, rename files and directories on the Fanuc robot controller via FTP.

  • Upload files
  • Download files
  • Delete, list, and manage directories
  • Asynchronous transfers
  • Errors
  • Rights of the user
  • Program in use
  • Complete example
  • API reference

Upload, download, delete, and rename files and directories on the Fanuc robot controller via FTP.

Upload files

parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
// Upload a TP program to the controller (overwrites if it already exists)
robot.Ftp.DirectFileHandling.UploadFileToController(@"C:\Programs\MyPrg.tp", "md:/MyPrg.tp");
// Skip upload if the file already exists on the controller
robot.Ftp.DirectFileHandling.UploadFileToController(
@"C:\Programs\MyPrg.tp", "md:/MyPrg.tp",
existsBehavior: FtpExistsBehavior.Skip);
// Resume a partial upload (appends missing bytes)
robot.Ftp.DirectFileHandling.UploadFileToController(
@"C:\LargeFile.tp", "md:/LargeFile.tp",
existsBehavior: FtpExistsBehavior.Append);
// Upload from a byte array
byte[] fileBytes = File.ReadAllBytes(@"C:\Programs\MyPrg.tp");
robot.Ftp.DirectFileHandling.UploadFileToController(
fileBytes, "md:/MyPrg.tp",
existsBehavior: FtpExistsBehavior.Overwrite);
// Upload multiple files to a directory
robot.Ftp.DirectFileHandling.UploadFilesToController(
new[] { @"C:\file1.tp", @"C:\file2.tp" },
"md:/programs/");
}
}
Click to see the full code

Download files

parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
// Download to a local file
robot.Ftp.DirectFileHandling.DownloadFileFromController(@"C:\Backup\Backup.va", "md:/Backup.va");
// Download to a byte array
byte[] data;
robot.Ftp.DirectFileHandling.DownloadFileFromController(out data, "md:/MyPrg.tp");
// Download multiple files
robot.Ftp.DirectFileHandling.DownloadFilesFromController(
@"C:\Backup\",
new[] { "md:/file1.tp", "md:/file2.va" });
}
}
Click to see the full code

Delete, list, and manage directories

parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
// Delete a file
robot.Ftp.DirectFileHandling.DeleteFile("md:/OldProgram.tp");
// Delete a directory and its contents
robot.Ftp.DirectFileHandling.DeleteDirectory("md:/OldFolder");
// Check if a directory exists
bool exists = robot.Ftp.DirectFileHandling.DirectoryExists("md:/programs");
// Create a directory
robot.Ftp.DirectFileHandling.CreateDirectory("md:/NewFolder");
// List files and directories
FtpListItem[] items = robot.Ftp.DirectFileHandling.GetListing("md:/");
foreach (var item in items)
{
Console.WriteLine($"{item.Name} ({item.Type}) - {item.Size} bytes");
}
// Rename or move a file
robot.Ftp.DirectFileHandling.Rename("md:/old.tp", "md:/new.tp");
}
}
Click to see the full code

Asynchronous transfers

Each method has an asynchronous version (UploadFileToControllerAsync, DownloadFileFromControllerAsync, DownloadBytesFromControllerAsync, GetListingAsync, FileExistsAsync, DeleteFileAsync...) with an optional CancellationToken. They are not available on .NET Framework 3.5 and 4.0.

parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
var files = robot.Ftp.DirectFileHandling;
// Download a program, with the progress of the transfer (0 to 100)
byte[] program = await files.DownloadBytesFromControllerAsync("md:/MyPrg.ls", p => Console.WriteLine($"{p:F0} %"));
// Upload it again, cancelled after 30 seconds
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
{
await files.UploadFileToControllerAsync(program, "md:/MyPrg.ls", cancellationToken: cts.Token);
}
// Check and delete
if (await files.FileExistsAsync("md:/OldPrg.ls"))
await files.DeleteFileAsync("md:/OldPrg.ls");
robot.Disconnect();
}
Click to see the full code

Errors

When the controller refuses an operation, the SDK throws an FtpException with the reply of the controller (ReplyCode, ReplyMessage) and, when it is known, what to do. A download that does not complete returns false (or null for DownloadBytesFromControllerAsync).

parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
try
{
robot.Ftp.DirectFileHandling.UploadFileToController(@"C:\Programs\MyPrg.ls", "md:/MyPrg.ls");
}
catch (FtpException ex) when (ex.ProgramInUse)
{
// The program is selected on the teach pendant or it runs:
// select another program (teach pendant, or robot.Cgtp.SelectProgram from V9.10), then upload again
Console.WriteLine(ex.Message);
}
catch (FtpException ex)
{
// Other refusal of the controller, for example "Operation password protected":
// the FTP user does not have enough rights
Console.WriteLine($"{ex.ReplyCode} {ex.ReplyMessage}");
}
robot.Disconnect();
}
Click to see the full code

Rights of the user

The password settings of the controller can restrict the rights of the FTP user. Without a user, the controller logs in at the OPERATOR level and can refuse the upload of a program with "Operation password protected". Connect with a user that has the needed level, for example INSTALL.

Program in use

A program that is selected on the teach pendant, or that runs, cannot be replaced: the controller replies "Specified program is in use" and FtpException.ProgramInUse is true. Select another program, then upload again:

  • on the teach pendant, with the SELECT key;
  • remotely, with robot.Cgtp.SelectProgram("OTHER") (web server of the controller, firmware V9.10 and later). See Programs.

The controller writes the modification date into the program when it is uploaded: a program downloaded after an upload can differ from the uploaded file on the date lines only.

Complete example

using UnderAutomation.Fanuc;
public class FtpFileManagement
{
static void Main()
{
FanucRobot robot = new FanucRobot();
ConnectionParameters parameters = new ConnectionParameters("192.168.0.1");
parameters.Ftp.Enable = true;
parameters.Ftp.FtpUser = "";
parameters.Ftp.FtpPassword = "";
robot.Connect(parameters);
// Upload a TP program to the controller
robot.Ftp.DirectFileHandling.UploadFileToController(@"C:\Programs\MyPrg.tp", "md:/MyPrg.tp");
// Download a file from the robot
robot.Ftp.DirectFileHandling.DownloadFileFromController(@"C:\Backup\Backup.va", "md:/Backup.va");
// Delete a file
robot.Ftp.DirectFileHandling.DeleteFile("md:/OldProgram.tp");
// List files in a directory
var items = robot.Ftp.DirectFileHandling.GetListing("md:/");
foreach (var item in items)
Console.WriteLine($"{item.Name} ({item.Type})");
// Create and delete directories
robot.Ftp.DirectFileHandling.CreateDirectory("md:/NewFolder");
// Rename a file
robot.Ftp.DirectFileHandling.Rename("md:/old.tp", "md:/new.tp");
// Check file existence
bool exists = robot.Ftp.DirectFileHandling.FileExists("md:/MyPrg.tp");
}
}

API reference

Class
FtpDirectFileHandling
C#Python

Methods to handle files on a Fanuc controller (upload, download, delete, enumerate, ...). The controller can refuse an operation: the rights depend on the FTP user and on the password settings of the controller (for example, an upload of a program needs a user with enough rights), and a program that is selected or runs cannot be replaced. The error is an FtpException with the reply of the controller.

MemberTypeDescription
CreateDirectory(string)
Method
void
Creates a directory on the controller. If the preceding directories do not exist, then they are created.
  • path : The full or relative path to the new remote directory
DeleteDirectory(string)
Method
void
Deletes the specified directory and all its contents.
  • path : The full or relative path of the directory to delete
DeleteFile(string)
Method
void
Deletes a file on the controller
  • path : The full or relative path to the file
DirectoryExists(string)
Method
bool
Tests if the specified directory exists on the controller. This method works by trying to change the working directory to the path specified. If it succeeds, the directory is changed back to the old working directory and true is returned. False is returned otherwise and since the CWD failed it is assumed the working directory is still the same.
  • path : The path of the directory
DownloadFileFromController(out byte[], string, OnProgressDelegate)
Method
bool
Downloads the specified file and return the raw byte array. High-level API that takes care of various edge cases internally. Supports very large files since it downloads data in chunks.
  • outBytes : The variable that will receive the bytes.
  • remotePath : The full or relative path to the file on the controller
  • progress : Track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
DownloadFileFromController(Stream, string, OnProgressDelegate)
Method
bool
Downloads the specified file into the specified stream. High-level API that takes care of various edge cases internally. Supports very large files since it downloads data in chunks.
  • outStream : The stream that the file will be written to. Provide a new MemoryStream if you only want to read the file into memory.
  • remotePath : The full or relative path to the file on the controller
  • progress : Track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
DownloadFileFromController(string, string, OnProgressDelegate)
Method
bool
Downloads the specified file onto the local file system. High-level API that takes care of various edge cases internally. Supports very large files since it downloads data in chunks. It overwrites the file if it already exists.
  • localPath : The full or relative path to the file on the local file system
  • remotePath : The full or relative path to the file on the controller
  • progress : Track download progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
DownloadFilesFromController(string, string[], OnProgressDelegate)
Method
string[]
Downloads the specified files into a local single directory. High-level API that takes care of various edge cases internally. Supports very large files since it downloads data in chunks. A file that fails is skipped: it is not in the returned list.
  • localDir : The full or relative path to the directory that files will be downloaded into.
  • remotePaths : The full paths to the files on the controller
  • progress : Track download progress. The value provided is in the range 0 to 100, indicating the percentage of the files transferred.
FileExists(string)
Method
bool
Checks if a file exists on the controller.
  • path : The full or relative path to the file
GetListing(string)
Method
FtpListItem[]
Gets a file listing from the controller. Each FtpListItem object returned contains information about the file that was able to be retrieved.
  • path : The path of the directory to list
GetObjectInfo(string)
Method
FtpListItem
Returns information about a file system object. Returns null if the controller response can't be parsed or the controller returns a failure completion code. No exception is thrown on error because that would negate the usefulness of this method for checking for the existence of an object.
  • path : The path of the file or folder
Rename(string, string)
Method
void
Renames an object on the remote file system. Throws exceptions if the file does not exist, or if the destination file already exists.
  • path : The full or relative path to the object
  • dest : The new full or relative path including the new name of the object
UploadFileToController(byte[], string, bool, OnProgressDelegate, FtpExistsBehavior)
Method
bool
Uploads the specified byte array as a file onto the controller. High-level API that takes care of various edge cases internally. Supports very large files since it uploads data in chunks. It overwrites file if it already exists.
  • fileData : The full data of the file, as a byte array
  • remotePath : The full or relative path to the file on the controller
  • createRemoteDir : Create the remote directory if it does not exist. Slows down upload due to additional checks required.
  • progress : Track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
  • existsBehavior : Specifies the behavior when the file already exists on the controller.
UploadFileToController(Stream, string, bool, OnProgressDelegate, FtpExistsBehavior)
Method
bool
Uploads the specified stream as a file onto the controller. High-level API that takes care of various edge cases internally. Supports very large files since it uploads data in chunks. It overwrites file if it already exists.
  • fileStream : The full data of the file, as a stream
  • remotePath : The full or relative path to the file on the controller
  • createRemoteDir : Create the remote directory if it does not exist. Slows down upload due to additional checks required.
  • progress : Track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
  • existsBehavior : Specifies the behavior when the file already exists on the controller.
UploadFileToController(string, string, bool, OnProgressDelegate, FtpExistsBehavior)
Method
bool
Uploads the specified file directly onto the controller. High-level API that takes care of various edge cases internally. Supports very large files since it uploads data in chunks.
  • localPath : The full or relative path to the file on the local file system
  • remotePath : The full or relative path to the file on the controller
  • createRemoteDir : Create the remote directory if it does not exist. Slows down upload due to additional checks required.
  • progress : Track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
  • existsBehavior : Specifies the behavior when the file already exists on the controller.
UploadFilesToController(string[], string, OnProgressDelegate)
Method
string[]
Uploads the given file paths to a single folder on the controller. All files are placed directly into the given folder regardless of their path on the local filesystem. High-level API that takes care of various edge cases internally. Supports very large files since it uploads data in chunks. A file that fails is skipped: it is not in the returned list.
  • localPaths : The full or relative paths to the files on the local file system. Files can be from multiple folders.
  • remoteDir : The full or relative path to the directory that files will be uploaded on the controller
  • progress : Track upload progress. The value provided is in the range 0 to 100, indicating the percentage of the file transferred. If the progress is indeterminate, -1 is sent.
Class
FtpListItem
C#Python

Represents a file system object on the controller

MemberTypeDescription
Chmod
Property
read only
int
Gets the file permissions in the CHMOD format.
Created
Property
read only
DateTime
Gets the created date of the object.
FullName
Property
read only
string
Gets the full path name to the object.
Modified
Property
read only
DateTime
Gets the last write time of the object.
Name
Property
read only
string
Gets name to the object.
Size
Property
read only
long
Gets the size of the object. Only a few files (like *.tp or *.df) have a size that can be retrieved, for most files this is 0 even if they are not empty. For directories this is always 0.
Type
Property
read only
FtpFileSystemObjectType
Gets the type of file system object.
Class
FtpExceptioninherits Exception
C#Python

Exception thrown when the controller refuses an FTP operation, or when the FTP communication fails. The message gives the reply of the controller and, when it is known, what to do.

MemberTypeDescription
ProgramInUse
Property
read only
bool
True when the controller refused the operation because the program is in use: it is selected on the teach pendant or it runs. Select another program before you upload or delete it.
RemotePath
Property
read only
string
Path of the file or folder on the controller concerned by the operation. Null when the operation has no path.
ReplyCode
Property
read only
int
FTP reply code returned by the controller (for example 550). 0 when the controller did not reply.
ReplyMessage
Property
read only
string
Reply text returned by the controller (for example "Specified program is in use"). Null when the controller did not reply.

Easily integrate Universal Robots, Fanuc, Yaskawa, ABB or Staubli robots into your .NET, Python, LabVIEW or Matlab applications

UnderAutomation
Contact usLegal

© All rights reserved.