Get started with Matlab
Load the .NET DLL of the Fanuc SDK in Matlab with NET.addAssembly, then read registers, positions and I/O of a Fanuc controller from a Matlab script.
This page shows how to use the Fanuc SDK from Matlab, to read and control a Fanuc controller or a ROBOGUIDE virtual robot from a Matlab script. Matlab loads the .NET DLL of the SDK with its .NET interface: nothing else is installed, on the PC or on the controller.
Requirements
| Item | Supported |
|---|---|
| Windows | Matlab with the .NET Framework 4.6.2 or later (the default on Windows 10 and 11). Load the net48 DLL |
| Linux and macOS | Matlab R2024b or later with the .NET runtime, selected by dotnetenv("core"), and the netstandard2.1 DLL. Not tested by UnderAutomation yet |
| Controllers | R-J3iB, R-30iA, R-30iB, R-30iB Plus, R-50iA, and ROBOGUIDE robots |
Matlab documents its .NET interface in Call .NET from MATLAB.
Load the SDK
On Windows
- Download UnderAutomation.Fanuc.zip from the latest release of Fanuc.NET.
- Unblock the zip before you extract it: right click,
Properties, checkUnblock. - Copy its
net48folder next to your script. - Load the DLL with
NET.addAssembly, then import the namespaces you use.
% The net48 folder of UnderAutomation.Fanuc.zip, copied next to this scriptdll = fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll');if ~NET.isNETSupportederror('No supported .NET Framework found on this computer');end% Load the SDK once per Matlab sessionNET.addAssembly(dll);% Import the namespaces you useimport UnderAutomation.Fanuc.*import UnderAutomation.Fanuc.Common.*
NET.addAssembly needs an absolute path: fullfile(pwd, ...) builds one that works on every operating system. Matlab cannot unload a .NET assembly: restart Matlab to load a new version of the DLL.
On Linux and macOS
Matlab R2024b and later can use the .NET runtime instead of the .NET Framework. Set the environment variable DOTNET_ROOT to the folder of the .NET runtime, call dotnetenv("core") before NET.addAssembly, and load the DLL of the netstandard2.1 folder. See dotnetenv in the Matlab documentation.
First program
Connect and read a register
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*% With an IP address only, the SDK connects to the web server of the controller (CGTP)robot = FanucRobot();robot.Connect('192.168.0.1');% Numeric register R[1] and current position of motion group 1r1 = robot.Cgtp.ReadNumericRegisterWithComment(1);disp(char(r1.ToString()));position = robot.Cgtp.ReadCartesianPosition(1);fprintf('X=%.3f, Y=%.3f, Z=%.3f\n', position.X, position.Y, position.Z);robot.Disconnect();
With an IP address only, Connect uses the web server of the controller (CGTP), with no option and firmware V8.30 or later (V9.10 for the position).
Enable other protocols
Telnet KCL, FTP, SNPX, RMI and Stream Motion are enabled in a ConnectionParameters object. Each one has its own setup on the controller: see Connect to your robot.
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*parameters = ConnectionParameters('192.168.0.1');% Enable the protocols you use. Each one has its own setup on the controller.parameters.Telnet.Enable = true;parameters.Telnet.TelnetKclPassword = 'your_password';parameters.Ftp.Enable = true;parameters.Ftp.FtpUser = '';parameters.Ftp.FtpPassword = '';parameters.Snpx.Enable = true;robot = FanucRobot();robot.Connect(parameters);robot.Disconnect();
License
The SDK runs for 30 days without a key. After that, register your license once per Matlab session:
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*% Register the license once, before the first connectionFanucRobot.RegisterLicense('YourCompanyName', 'YOUR_LICENSE_KEY');info = FanucRobot.LicenseInfo;% A readable description of the current statedisp(char(info.ToString()));% Check the license once at startupif ~info.IsLicensederror(char(info.ToString()));end
From .NET to Matlab
The names of the classes, methods and properties are the .NET names of the other pages. The C# samples of the documentation translate line by line, with these rules:
| .NET | Matlab |
|---|---|
new FanucRobot() | FanucRobot(), after import UnderAutomation.Fanuc.* |
"192.168.0.1" | '192.168.0.1', a char vector |
Pause("MyProgram", force: true) | Pause('MyProgram', true): no named arguments |
An optional argument, like groupNum | Pass every argument. [] passes null |
A float argument, like a register value | single(123.45) |
An int argument | int32(999) |
A nullable value (int?, bool?) | An object with HasValue and Value, or GetValueOrDefault() |
positions[0] | positions(1): Matlab indexes .NET arrays from 1 |
A string result | char(result) gives a Matlab char vector |
An enumeration value, like KCLPorts.DOUT | KCLPorts.DOUT, after import UnderAutomation.Fanuc.Common.Kcl.* |
try { } finally { } | try ... catch, then the cleanup in both branches |
Examples
Read the position
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*robot = FanucRobot();robot.Connect('192.168.0.1');% Cartesian position of motion group 1cartesian = robot.Cgtp.ReadCartesianPosition(1);fprintf('X=%.3f, Y=%.3f, Z=%.3f\n', cartesian.X, cartesian.Y, cartesian.Z);fprintf('W=%.3f, P=%.3f, R=%.3f\n', cartesian.W, cartesian.P, cartesian.R);% Joint position of motion group 1, in degreesjoints = robot.Cgtp.ReadJointPosition(1);fprintf('J1=%.3f, J2=%.3f, J3=%.3f\n', joints.J1, joints.J2, joints.J3);robot.Disconnect();
Read and write numeric registers
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*parameters = ConnectionParameters('192.168.0.1');parameters.Snpx.Enable = true;robot = FanucRobot();robot.Connect(parameters);% Read R[1] as a real valuevalue = robot.Snpx.NumericRegisters.Read(1);% Write R[1]: the .NET method takes a float, so pass a singlerobot.Snpx.NumericRegisters.Write(1, single(123.45));% Read and write R[2] as a 32 bit integerintValue = robot.Snpx.NumericRegistersInt32.Read(2);robot.Snpx.NumericRegistersInt32.Write(2, int32(999));robot.Disconnect();
Read and write digital signals
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*parameters = ConnectionParameters('192.168.0.1');parameters.Snpx.Enable = true;robot = FanucRobot();robot.Connect(parameters);% Read SDI[10]sdi10 = robot.Snpx.SDI.Read(10);% Write RDO[1] = ONrobot.Snpx.RDO.Write(1, true);% Read UI[5]ui5 = robot.Snpx.UI.Read(5);robot.Disconnect();
Run a program
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.*parameters = ConnectionParameters('192.168.0.1');parameters.Telnet.Enable = true;parameters.Telnet.TelnetKclPassword = 'your_password';robot = FanucRobot();robot.Connect(parameters);% Run a programrobot.Telnet.Run('MyProgram');% Pause it (pass every argument: Matlab has no optional arguments)robot.Telnet.Pause('MyProgram', false);% Resume itrobot.Telnet.Continue('MyProgram');robot.Disconnect();
Forward kinematics, offline
NET.addAssembly(fullfile(pwd, 'net48', 'UnderAutomation.Fanuc.dll'));import UnderAutomation.Fanuc.Common.*import UnderAutomation.Fanuc.Kinematics.*% Offline: no robot and no connection are neededdh = DhParameters.FromArmKinematicModel(ArmKinematicModels.CRX10iA);% Joint angles in degreesjoints = JointsPosition(0, -30, 45, 0, 60, 90);% Forward kinematics: X, Y, Z in mm and W, P, R in degreespose = KinematicsUtils.ForwardKinematics(joints, dh);fprintf('X=%.2f, Y=%.2f, Z=%.2f, W=%.2f, P=%.2f, R=%.2f\n', ...pose.X, pose.Y, pose.Z, pose.W, pose.P, pose.R);
Known limits
- Matlab has no named arguments and no optional arguments: pass all the arguments of a .NET method, in order.
- A .NET event, like
robot.Telnet.CommandSent, needsaddlistener, and its handler runs on a thread of the SDK. Prefer to read the values in a loop. - Stream Motion sends a position at every communication cycle, 2 to 8 ms. Use the target tracking or a trajectory of the motion planner, not a Matlab callback for each cycle.
- An exception of the SDK is a
NET.NetExceptionin Matlab. ItsExceptionObjectproperty holds the .NET exception, with itsMessage. - Matlab keeps the DLL loaded until it closes.
See also the Matlab pages on the limits of .NET arrays, .NET methods and .NET enumerations.
What to read next
- Connect to your robot: the protocols, their setup on the controller, the connection parameters.
- Test with ROBOGUIDE: work without a real robot.
- The protocol pages: CGTP, SNPX, Telnet.
- Licensing: the 30 day trial and the license key.