This page explains how to connect the Fanuc SDK to a real controller or to a ROBOGUIDE virtual robot: the protocols to enable, their setup on the controller and the connection parameters. The `ConnectionParameters` class lists the protocols, and each protocol is enabled on its own.

## Prerequisites

### Network

The PC and the controller are on the same network, and the PC can ping the controller. Each protocol uses its own TCP port: allow it in the firewalls between the PC and the controller.

### Protocols and controller options

| Protocol      | What it gives                                                    | Controller option                                                     | Setup                                                         |
| ------------- | ---------------------------------------------------------------- | --------------------------------------------------------------------- | ------------------------------------------------------------- |
| CGTP          | Programs, variables, registers, I/O, position, kinematics, files | None, firmware V8.30 or later                                          | [CGTP overview](/fanuc/documentation/cgtp)                   |
| SNPX          | Fast access to registers, variables, I/O, position, alarms      | R553 with the FANUC America parameters (R650 FRA), none with R651 FRL | [SNPX overview](/fanuc/documentation/snpx)                   |
| Telnet KCL    | Program control, variables, ports, breakpoints                   | None                                                                  | [Enable Telnet](/fanuc/documentation/telnet-enable-on-robot) |
| FTP           | Files, variable files, diagnostics                               | None                                                                  | [FTP overview](/fanuc/documentation/ftp)                     |
| RMI           | Motion instructions sent from the PC                             | R912                                                                  | [RMI overview](/fanuc/documentation/rmi)                     |
| Stream Motion | A position at every communication cycle                          | J519                                                                  | [Stream Motion overview](/fanuc/documentation/stream-motion) |

## Connect

### Quick connection

The simplest way to connect is to pass an IP address. This enables only the CGTP protocol, which is enabled by default.

**C# : ConnectQuick**
```csharp
using UnderAutomation.Fanuc;

public class ConnectQuick
{
    static void Main()
    {
        /**/
        var robot = new FanucRobot();
        robot.Connect("192.168.0.1");
        /**/
    }
}
```

**Python : ConnectQuick**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot

##
robot = FanucRobot()
robot.connect("192.168.0.1")
##
```

**Matlab : ConnectQuick**
```matlab
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 1
r1 = 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();
```

### Several protocols

To use several protocols, create a `ConnectionParameters` object and enable each protocol you need.

**C# : Connect**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Fanuc.Common;

public class Connect
{
  static void Main()
  {
    // Create a robot instance
    FanucRobot robot = new FanucRobot();

    // Configure connection parameters
    ConnectionParameters parameters = new ConnectionParameters("192.168.0.1");

    /**/
    // Enable Telnet KCL for remote commands
    parameters.Telnet.Enable = true;
    parameters.Telnet.TelnetKclPassword = "your_password";

    // Enable FTP for file and variable access
    parameters.Ftp.Enable = true;
    parameters.Ftp.FtpUser = "";
    parameters.Ftp.FtpPassword = "";

    // Enable SNPX for high-speed register and I/O access
    parameters.Snpx.Enable = true;

    // Enable CGTP Web Server (enabled by default)
    parameters.Cgtp.Enable = true;

    // Enable RMI for remote motion commands
    parameters.Rmi.Enable = true;

    // Connect to the robot
    robot.Connect(parameters);

    // Check connection status
    bool isConnected = robot.Enabled;

    // Disconnect when done
    robot.Disconnect();
    /**/
  }
}
```

**Python : Connect**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot
from underautomation.fanuc.connection_parameters import ConnectionParameters
from underautomation.fanuc.common.languages import Languages

# Create a robot instance
robot = FanucRobot()

# Configure connection parameters
parameters = ConnectionParameters("192.168.0.1")

##
# Enable Telnet KCL for remote commands
parameters.telnet.enable = True
parameters.telnet.telnet_kcl_password = "your_password"

# Enable FTP for file and variable access
parameters.ftp.enable = True
parameters.ftp.ftp_user = ""
parameters.ftp.ftp_password = ""

# Enable SNPX for high-speed register and I/O access
parameters.snpx.enable = True

# Enable CGTP Web Server (enabled by default)
parameters.cgtp.enable = True

# Enable RMI for remote motion commands
parameters.rmi.enable = True

# Connect to the robot
robot.connect(parameters)

# Check connection status
is_connected = robot.enabled

# Disconnect when done
robot.disconnect()
##
```

**Matlab : Connect**
```matlab
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();
```

### ROBOGUIDE

Instead of an IP address, pass the folder of a robot of a ROBOGUIDE workcell. The SDK reads the file `services.txt` of that folder to find the TCP ports of the virtual robot. See [Test with ROBOGUIDE](/fanuc/documentation/simulator).

**C# : ConnectRoboguide**
```csharp
using UnderAutomation.Fanuc;

public class ConnectRoboguide
{
    static void Main()
    {
        FanucRobot robot = new FanucRobot();

        /**/
        var parameters = new ConnectionParameters(@"C:\Users\you\Documents\My Workcells\CRX 10iA L\Robot_1");
        parameters.Telnet.Enable = true;
        parameters.Telnet.TelnetKclPassword = "";
        parameters.Ftp.Enable = true;
        parameters.Ftp.FtpUser = "";
        parameters.Ftp.FtpPassword = "";
        robot.Connect(parameters);
        /**/
    }
}
```

**Python : ConnectRoboguide**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot
from underautomation.fanuc.connection_parameters import ConnectionParameters

robot = FanucRobot()

##
parameters = ConnectionParameters(r"C:\Users\you\Documents\My Workcells\CRX 10iA L\Robot_1")
parameters.telnet.enable = True
parameters.telnet.telnet_kcl_password = ""
parameters.ftp.enable = True
parameters.ftp.ftp_user = ""
parameters.ftp.ftp_password = ""
robot.connect(parameters)
##
```

## Connection options

### Ping

When `PingBeforeConnect` is `true` (default), the SDK pings the controller before it opens any connection. If the controller does not answer within 100 ms, `Connect` throws an exception at once. Set it to `false` when ICMP is blocked on your network.

### Language of the controller

Set the `Language` property to the language of the controller, so that the strings read with SNPX, FTP and Telnet are decoded correctly: English (ASCII, default), Japanese (Shift-JIS) or Chinese (GB2312).

**C# : ConnectOptions**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Fanuc.Common;

public class ConnectOptions
{
    static void Main()
    {
        FanucRobot robot = new FanucRobot();
        ConnectionParameters parameters = new ConnectionParameters("192.168.0.1");

        /**/
        // Ping before connecting (default: true)
        parameters.PingBeforeConnect = true;
        // Set to false for ROBOGUIDE or when ICMP is blocked

        // Controller language for correct string decoding
        parameters.Language = Languages.English;   // default (ASCII)
                                                   // parameters.Language = Languages.Japanese; // Shift-JIS
                                                   // parameters.Language = Languages.Chinese;  // GB2312
        /**/

        robot.Connect(parameters);
    }
}
```

**Python : ConnectOptions**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot
from underautomation.fanuc.connection_parameters import ConnectionParameters
from underautomation.fanuc.common.languages import Languages

robot = FanucRobot()
parameters = ConnectionParameters("192.168.0.1")

##
# Ping before connecting (default: True)
parameters.ping_before_connect = True
# Set to False for ROBOGUIDE or when ICMP is blocked

# Controller language for correct string decoding
parameters.language = Languages.English   # default (ASCII)
# parameters.language = Languages.Japanese  # Shift-JIS
# parameters.language = Languages.Chinese   # GB2312
##

robot.connect(parameters)
```

## Standalone protocol clients

Each protocol can also be used without `FanucRobot`, when you only need one protocol:

**C# : ConnectStandalone**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Fanuc.Snpx;
using UnderAutomation.Fanuc.Telnet;
using UnderAutomation.Fanuc.Cgtp;

public class ConnectStandalone
{
    static void Main()
    {
        /**/
        // Standalone SNPX client
        var snpx = new SnpxClient();
        snpx.Connect("192.168.0.1");

        // Standalone Telnet client
        var telnet = new TelnetClient();
        telnet.Connect("192.168.0.1", "telnet_password");

        // Standalone CGTP client
        var cgtp = new CgtpClient();
        cgtp.Connect("192.168.0.1");
        /**/
    }
}
```

**Python : ConnectStandalone**
```python
from underautomation.fanuc.snpx.snpx_client import SnpxClient
from underautomation.fanuc.telnet.telnet_client import TelnetClient
from underautomation.fanuc.cgtp.cgtp_client import CgtpClient

##
# Standalone SNPX client
snpx = SnpxClient()
snpx.connect("192.168.0.1")

# Standalone Telnet client
telnet = TelnetClient()
telnet.connect("192.168.0.1", "telnet_password")

# Standalone CGTP client
cgtp = CgtpClient()
cgtp.connect("192.168.0.1")
##
```

## Disconnect

Always disconnect when you are done, to close the connections of every protocol.

**C# : ConnectDisconnect**
```csharp
using UnderAutomation.Fanuc;

public class ConnectDisconnect
{
    static void Main()
    {
        FanucRobot robot = new FanucRobot();
        robot.Connect("192.168.0.1");

        /**/
        robot.Disconnect();
        /**/
    }
}
```

**Python : ConnectDisconnect**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot

robot = FanucRobot()
robot.connect("192.168.0.1")

##
robot.disconnect()
##
```

## Try it in the demo application

Everything on this page can be tried without writing code, in the **Connection** page of the [demo application](/fanuc/documentation/demo-app).

![Connection page of the Fanuc SDK demo application](/fanuc/WinformsScreenshots/Connect.jpg)

Its C# source is [ConnectControl.cs](https://github.com/underautomation/Fanuc.NET/blob/main/UnderAutomation.Fanuc.Showcase.Forms/Components/ConnectControl.cs).

## Reference

**Members of ConnectionParameters**
```csharp
public class ConnectionParameters {
    // Instanciate a new connection parameters
    public ConnectionParameters()

    // Instanciate a new connection parameters with a specified address
    public ConnectionParameters(string address)

    // Address of the robot (IP, host name, or path to ROBOGUIDE project folder)
    public string Address { get; set; }

    // Parameters of the CGTP client, which uses the web server of the controller (HTTP)
    public CgtpConnectParameters Cgtp { get; set; }

    // Access controller internal memory to read variables, IO, positions, diagnosis, ...
    public FtpConnectParameters Ftp { get; set; }

    // Controller language (default: English)
    public Languages Language { get; set; }

    // Send a ping command before initializing any connections
    public bool PingBeforeConnect { get; set; }

    // Parameters for RMI (Remote Motion Interface)
    public RmiConnectParameters Rmi { get; set; }

    // Read and write IOs, read and clear alarms, read current program tasks
    public SnpxConnectParameters Snpx { get; set; }

    // Parameters for Stream Motion (J519 option) - real-time streaming motion control over UDP
    public StreamMotionConnectParameters StreamMotion { get; set; }

    // Parameters of the Telnet KCL client, which sends commands to the robot for remote control.
    // Telnet KCL is a legacy protocol: it is not secured (password and commands are sent in clear text), and its behavior changes with the firmware version and on ROBOGUIDE. The same KCL commands are available on the web server of the controller with robot.Cgtp.Kcl (firmware V8.30 and later): prefer it for new developments.
    public TelnetConnectParameters Telnet { get; set; }
}
```

**Members of Common.TelnetConnectParameters**
```csharp
public class TelnetConnectParameters : TelnetConnectParametersBase {
    public TelnetConnectParameters()

    // Should use this service (default: false). Prefer robot.Cgtp.Kcl, enabled by default, for new developments.
    public bool Enable { get; set; }
}
```

**Members of Common.FtpConnectParameters**
```csharp
public class FtpConnectParameters : FtpConnectParametersBase {
    public FtpConnectParameters()

    // Should enable memory access for this connection (default: true)
    public bool Enable { get; set; }
}
```

**Members of Common.SnpxConnectParameters**
```csharp
public class SnpxConnectParameters : SnpxConnectParametersBase {
    public SnpxConnectParameters()

    // Should enable SNPX for this connection (default: false)
    public bool Enable { get; set; }
}
```

**Members of Common.CgtpConnectParameters**
```csharp
public class CgtpConnectParameters : CgtpConnectParametersBase {
    public CgtpConnectParameters()

    // Should enable CGTP Web Server for this connection (default: true)
    public bool Enable { get; set; }
}
```

**Members of Common.RmiConnectParameters**
```csharp
public class RmiConnectParameters : RmiConnectParametersBase {
    public RmiConnectParameters()

    // Should enable RMI for this connection (default: false)
    public bool Enable { get; set; }
}
```

**Members of Common.StreamMotionConnectParameters**
```csharp
public class StreamMotionConnectParameters : StreamMotionConnectParametersBase {
    public StreamMotionConnectParameters()

    // Should enable Stream Motion for this connection (default: false)
    public bool Enable { get; set; }

    // IP address of the robot for standalone Stream Motion connections
    public string Ip { get; set; }
}
```

## What to read next

- [Test with ROBOGUIDE](/fanuc/documentation/simulator): work without a real robot.
- [Demo application](/fanuc/documentation/demo-app): try the connection without writing code.
- The protocol pages: [CGTP](/fanuc/documentation/cgtp), [SNPX](/fanuc/documentation/snpx), [Telnet](/fanuc/documentation/telnet), [FTP](/fanuc/documentation/ftp), [RMI](/fanuc/documentation/rmi), [Stream Motion](/fanuc/documentation/stream-motion).