---
title: "Read and write robot variables in C# and Python"
description: "Read and write the registers and variables of Fanuc, Yaskawa, Universal Robots and ABB controllers with one code: native names of each brand, brand support table, C# and Python examples."
---

# Read and write robot variables in C# and Python

This article shows how to read and write the variables and registers of Fanuc, Yaskawa, Universal Robots and ABB controllers from a PC, with one code in C# or Python. Only the address of the controller and the native name of the variable change from one brand to another: the table at the end gives the names of each brand.

The variables of a Stäubli controller (VAL 3 data) are not in the common API: the support table below shows it.

## Prerequisites

- UnderAutomation.Robotics in your project. See [Get started with .NET](/robotics/documentation/get-started-net) or [Get started with Python](/robotics/documentation/get-started-python).
- The connection settings of each controller. See [Connect to your robot](/robotics/documentation/connect).
- The license of each brand you connect, or the 30 day trial. See [Licensing](/robotics/documentation/license).
- The native name of the variable, the one of the pendant and the programs. See the table at the end.
- A write changes a value that a program of the robot can use. Test the writes on a simulator first. See [Work without a robot](/robotics/documentation/simulator).

## Brand support

Each cell gives the protocol that the method uses on this brand, in the default order. The table is generated from the route data of the library. "Not available" means that the brand has no route for the method: the call throws `FeatureNotSupportedException`, and `IsAvailable` returns false before the call. (manual) marks a route that you enable yourself in the connection parameters.

Routes of each method in the default order. The first route is used, the next ones are fallbacks. (manual) marks a route that you enable yourself.

| Method | Fanuc | Yaskawa | Universal Robots | ABB | Stäubli |
| --- | --- | --- | --- | --- | --- |
| Variable.Read | CGTP (Cgtp), SNPX | High Speed Ethernet Server, Ethernet Server | Dashboard Server, Primary Interface (Primary) | Robot Web Services | Not available |
| Variable.WriteInteger | CGTP (Cgtp), SNPX | High Speed Ethernet Server, Ethernet Server | Interpreter Mode (manual) | Robot Web Services | Not available |
| Variable.WriteReal | CGTP (Cgtp), SNPX | High Speed Ethernet Server, Ethernet Server | Interpreter Mode (manual) | Robot Web Services | Not available |
| Variable.WriteString | CGTP (Cgtp), SNPX | High Speed Ethernet Server, Ethernet Server | Interpreter Mode (manual) | Robot Web Services | Not available |
| Variable.WriteBoolean | CGTP (Cgtp), SNPX | High Speed Ethernet Server, Ethernet Server | Interpreter Mode (manual) | Robot Web Services | Not available |
| Methods of this article | **5 of 5** | **5 of 5** | **5 of 5** | **5 of 5** | **0 of 5** |

## Example

### Read and write a variable on each controller of a cell

The example connects to the controllers of a cell one after the other. It reads one variable on each, then writes it when the brand can. `Read` returns a `VariableValue`: `Type` gives the type, `Text` the value as text for every type, and `Number` the value of a number.

`IsAvailable(VariableFeature.WriteInteger)` tells before the call if the brand has a write route that can be used. It is false on a brand without a write route, and false on Universal Robots until Interpreter Mode is enabled. It does not check the state of the controller: on ABB, it is true without the RAPID mastership, and the controller then refuses the write. Take the mastership first (see the table at the end).

**C# : VariableAllBrands**
```csharp
using UnderAutomation.Robotics.Controllers;

public class VariableAllBrands
{
    static void Main()
    {
        /**/
        // One line per controller: the brand, the address and the native name of a variable
        var cell = new[]
        {
            (Brand: "fanuc", Address: "192.168.0.1", Variable: "R[1]"),
            (Brand: "yaskawa", Address: "192.168.0.2", Variable: "I000"),
            (Brand: "universalrobots", Address: "192.168.0.3", Variable: "my_var"),
            (Brand: "abb", Address: "192.168.125.1", Variable: "T_ROB1/user/reg1"),
        };

        foreach (var controller in cell)
        {
            // The same code for each controller of this cell
            RobotController robot = new RobotController();
            robot.Connect(RobotConnectionParameters.Create(controller.Brand, controller.Address));

            VariableValue value = robot.Variable.Read(controller.Variable);
            Console.WriteLine($"{robot.Brand} {value.Name} ({value.Type}) = {value.Text}");

            // False when the brand has no write route, or when its route is off. Not a check of the state:
            // true on ABB without the mastership, and the controller then refuses the write
            // ABB: take the RAPID mastership with the ABB SDK through robot.Native before the write
            if (robot.Variable.IsAvailable(VariableFeature.WriteInteger))
                robot.Variable.WriteInteger(controller.Variable, 42);

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

**Python : VariableAllBrands**
```python
from underautomation.robotics.controllers.robot_controller import RobotController
from underautomation.robotics.controllers.robot_connection_parameters import RobotConnectionParameters
from underautomation.robotics.controllers.variable_feature import VariableFeature

##
# One line per controller: the brand, the address and the native name of a variable
cell = [
    ("fanuc", "192.168.0.1", "R[1]"),
    ("yaskawa", "192.168.0.2", "I000"),
    ("universalrobots", "192.168.0.3", "my_var"),
    ("abb", "192.168.125.1", "T_ROB1/user/reg1"),
]

for brand, address, variable in cell:
    # The same code for each controller of this cell
    robot = RobotController()
    robot.connect(RobotConnectionParameters.create(brand, address, ""))

    value = robot.variable.read(variable)
    print(f"{robot.brand.name} {value.name} ({value.type.name}) = {value.text}")

    # False when the brand has no write route, or when its route is off. Not a check of the state:
    # true on ABB without the mastership, and the controller then refuses the write
    # ABB: take the RAPID mastership with the ABB SDK through robot.native before the write
    if robot.variable.is_available(VariableFeature.WriteInteger):
        robot.variable.write_integer(variable, 42)

    robot.disconnect()
##
```

In Python, the names are in snake_case: `robot.variable.read(...)`, `robot.variable.write_integer(...)`, `robot.variable.is_available(...)`.

### Write the other types

There is one write method per type: `WriteInteger`, `WriteReal`, `WriteString` and `WriteBoolean`. This example writes registers of a Fanuc controller. On another brand, give the names of the table below.

**C# : VariableWrite**
```csharp
using UnderAutomation.Robotics.Controllers;

public class VariableWrite
{
    static void Main()
    {
        RobotController robot = new RobotController();
        robot.Connect(RobotConnectionParameters.Create("fanuc", "192.168.0.1"));

        /**/
        robot.Variable.WriteInteger("R[1]", 42);
        robot.Variable.WriteReal("R[2]", 3.14);
        robot.Variable.WriteString("SR[1]", "Hello");
        /**/

        robot.Disconnect();
    }
}
```

**Python : VariableWrite**
```python
from underautomation.robotics.controllers.robot_controller import RobotController
from underautomation.robotics.controllers.robot_connection_parameters import RobotConnectionParameters

robot = RobotController()
robot.connect(RobotConnectionParameters.create("fanuc", "192.168.0.1", ""))

##
robot.variable.write_integer("R[1]", 42)
robot.variable.write_real("R[2]", 3.14)
robot.variable.write_string("SR[1]", "Hello")
##

robot.disconnect()
```

## What changes from one brand to another

The code does not change. The name of the variable follows the syntax of the controller, and some brands need a setting before a write.

| Brand | Native name | Examples | Before a write | Brand article |
| --- | --- | --- | --- | --- |
| Fanuc | Register or variable as the pendant writes it. A comment can follow the index after `:`. | `R[1]`, `SR[1]` (string register), `PR[1]` (position register), `$MCR.$GENOVERRIDE` (system variable), `[MAIN]COUNT` (variable of a KAREL program) | Nothing more | [Read and write registers](/fanuc/documentation/how-to-read-write-fanuc-registers) |
| Yaskawa | Letter of the type, then the index of the variable: B (byte), I (integer), D (double integer), R (real), S (string). | `B000`, `I012`, `D003`, `R005`, `S010` | Nothing more | [Read and write variables](/yaskawa/documentation/how-to-read-write-variables) |
| Universal Robots | Name of a global variable of the program that runs on the robot. | `my_var` | The writes have one route, Interpreter Mode: enable it in the connection parameters, and run the interpreter program on the robot. See [Routing settings](/robotics/documentation/routing-settings). | [Variables](/universal-robots/documentation/variables) |
| ABB | Path of a RAPID data: the task, the module and the name of the data. | `T_ROB1/user/reg1` (the predefined registers are in the built-in module `user`) | A write needs the RAPID mastership, taken with the ABB SDK through `robot.Native`. See [Mastership](/abb/documentation/rws-mastership). | [Read and write RAPID variables](/abb/documentation/read-write-rapid-variables) |
| Stäubli | Not available | | | |

Positions use their own methods: `ReadPosition` and `WritePosition`, for example for `PR[1]` on Fanuc or a `robtarget` data on ABB. The page [Variable](/robotics/documentation/variable) explains them, with the comments, the lists and `ReadMany`.

## Errors

- `FeatureNotSupportedException`: the brand has no route for the method, for example any Variable method on Stäubli.
- `RouteUnavailableException`: the method has routes, but none can be used. On Universal Robots, the writes throw it until Interpreter Mode is enabled. The message says what to enable.
- `ControllerRefusedException`: the controller received the write and refused it.
- `ArgumentException`: the name does not follow the syntax of the brand. The message gives the expected syntax.

The page [Errors](/robotics/documentation/errors) explains each exception.

## What to read next

- [Variable](/robotics/documentation/variable): positions, comments, lists and reads of several variables.
- [Read and write robot IO](/robotics/documentation/how-to-read-write-io): the signals of the same controllers.
- [Which protocol for which task](/robotics/documentation/which-protocol-for-which-task): what each route needs on the controller.