Read and write variables
Read the program and installation variables of a UR cobot in C# or Python with the Primary Interface or the Dashboard Server, and how to change them.
This article shows how to read the global variables of a Universal Robots cobot from a PC, in C# or Python, and how to change them. A global variable is a program variable or an installation variable of PolyScope. To exchange values with a running program at high speed, use registers instead.
Which way to choose
| Way | Reads | Writes | When |
|---|---|---|---|
| Primary Interface | Every variable, updated when its value changes | no | Follow the variables of a running program |
| Dashboard Server | One variable, by its name, when you ask | no | Read one value from time to time (PolyScope 5) |
| URScript | yes, the program stops | Set an installation variable before a program | |
| Registers (RTDE) | 0 to 47 numbers and 0 to 127 bits | yes, while the program runs | Exchange values with a running program |
In the SDK, a variable is a GlobalVariable: its Name, its Type and its Value, with typed conversions (ToInt, ToFloat, ToBool, ToPose, ToList, ToMatrix).
Read with the Primary Interface
The Primary Interface sends the list of the variables and their new values when they change. GlobalVariables keeps the last values received:
ListUpdatedis raised when the list of the variables changes;ValuesUpdatedis raised when at least one value changes;GetAll()andGetByName()return the last values received.
// The Primary Interface is enabled by defaultrobot.Connect("192.168.0.1");// Raised when the list of variables changesrobot.PrimaryInterface.GlobalVariables.ListUpdated += (sender, e) =>{GlobalVariable[] variables = e.Variables;};// Raised when at least one value changesrobot.PrimaryInterface.GlobalVariables.ValuesUpdated += (sender, e) =>{GlobalVariable[] variables = e.Variables;};// Last values receivedGlobalVariable[] all = robot.PrimaryInterface.GlobalVariables.GetAll();GlobalVariable myVar = robot.PrimaryInterface.GlobalVariables.GetByName("myVar");if (myVar != null){// Typed value, according to myVar.Typeswitch (myVar.Type){case GlobalVariableTypes.Bool: bool b = myVar.ToBool(); break;case GlobalVariableTypes.Int: int i = myVar.ToInt(); break;case GlobalVariableTypes.Float: float f = myVar.ToFloat(); break;case GlobalVariableTypes.String: string s = myVar.ToString(); break;case GlobalVariableTypes.Pose: Pose p = myVar.ToPose(); break;case GlobalVariableTypes.List: GlobalVariableValue[] l = myVar.ToList(); break;case GlobalVariableTypes.Matrix: Array m = myVar.ToMatrix(); break;}}}}
The robot sends the variables when they change. If your application connects while a program runs, the list stays empty until a value changes. The decoding depends on the PolyScope version (up to 3.2, up to 5.9, later versions): the SDK selects it, and FirmwareVersion gives it.
Read with the Dashboard Server
GetVariable asks the robot for one variable of the running program. The type is deduced from the value.
// The Dashboard Server is enabled by defaultrobot.Connect("192.168.0.1");// Read the variable "myVar" of the running programvar response = robot.Dashboard.GetVariable("myVar");if (response.Succeed){GlobalVariable variable = response.Value;Console.WriteLine($"{variable.Name} = {variable.Value} ({variable.Type})");// Typed conversions, according to variable.Typeif (variable.Type == GlobalVariableTypes.Pose){Pose pose = variable.ToPose();}}}}
Write a variable
No interface of the robot writes a variable while a program runs. Two ways remain:
Before the program
Send URScript that sets the variable. The running program stops, so do it when no program runs, for example to initialize an installation variable. To make PolyScope save the new value of an installation variable, also set _hidden_verificationVariable to 0:
robot.Connect("192.168.0.1");// Set the installation variable i_var_1 to 6.// The running program stops: send it when no program runs.// _hidden_verificationVariable = 0 makes PolyScope save the new valuerobot.PrimaryInterface.Script.Send("def SetInstallationVariable():\n" +" global _hidden_verificationVariable = 0\n" +" global i_var_1 = 6\n" +"end\n");}}
A secondary program (sec) does not stop the program, but it cannot write a global variable.
While the program runs
Use the registers: the program reads them with read_input_integer_register, read_input_float_register or read_input_boolean_register, and your application writes them with RTDE up to 500 Hz. See Read and write registers.
Try it in the demo application
Everything on this page can be tried without writing code, in the Running program and variables page of the demo application.

The demo application is open source. The C# source of this page is ProgramControl.cs.
Reference
Describes a global variable
| Member | Type | Description |
|---|---|---|
GlobalVariable() Constructor | ||
Name Property read only | string | Variable name |
Time Property read only | TimeSpan | Last time the variable was sampled |
Equals(object) Method | bool | Determines whether the specified object is a GlobalVariable with the same name, type, and value. |
GetHashCode() Method | int | Returns a hash code based on the variable name, type, and value. |
ToString() Method | string | Returns the variable name followed by its string representation. |
Describes a typed variable value
| Member | Type | Description |
|---|---|---|
GlobalVariableValue() Constructor | ||
Type Property read only | GlobalVariableTypes | Type of a variable |
Value Property read only | object | Value of the variable |
Equals(object) Method | bool | Determines whether the specified object is a GlobalVariableValue with the same type and value.
|
GetHashCode() Method | int | Returns a hash code based on the variable type and value. |
Parse(string) Method static | GlobalVariableValue | Estimate variable value from its string representation |
ToBool() Method | bool | Returns variable value if type is Bool. Il type is Float or Int, it returns True if value is not 0. Else, it returns false |
ToFloat() Method | float | Returns variable value if type is Float. Il type is int, it casts it to float. If Type is bool, it returns 1 or 0. Else it returns NaN |
ToInt() Method | int | Returns variable value if type is Int. Il type is Float, it tries to cast it to int. If Type is bool, it returns 1 or 0. Else it returns 0 |
ToList() Method | GlobalVariableValue[] | Returns an array of GlobalVariableValue if Type is List. Else, null is returned |
ToMatrix() Method | Array | Return variable value GlobalVariable[,] if variable is a matrix. First dimension is row index and second dimension is column index. Use GetLength(0) to get row number and GetLength(1) to get column count |
ToPose() Method | Pose | Returns a Pose if Type is Pose. Else, null is returned |
ToString() Method | string | Returns a string that describes the variable, regardless of its type |
Possible types of a variable
| Name | Value | Description |
|---|---|---|
Bool | 4 | Variable value is bool |
Float | 6 | Variable value is float |
Int | 5 | Variable value is int |
List | 2 | Variable value is an array : GlobalVariableValue[] |
Matrix | 7 | Variable value is a matrix |
None | 0 | Variable value is null, the value has not been assigned yet |
Pose | 3 | Variable value is a UnderAutomation.UniversalRobots.Pose |
String | 1 | Variable value is a System.String |
List of all global variables
| Member | Type | Description |
|---|---|---|
FirmwareVersion Property read only | GlobalVariablesFirmwareVersion | Indicates which decoder is used used to read variables according to firmware version |
ListUpdated Event | EventHandler<GlobalVariablesEventArgs> | Event raised whan the variable list changed. For example, after a program starts |
ValuesUpdated Event | EventHandler<GlobalVariablesEventArgs> | Event raised at 10Hz when variable values are updated |
GetAll() Method | GlobalVariable[] | Returns a list of all variables declared in the robot |
GetByName(string) Method | GlobalVariable | Get a variable by its name. Null is returned if the variable doesn't exist
|
ToString() Method | string | Return a string where each line contains type, name and variable value |
What to read next
- Read and write registers: exchange values with a running program.
- Send URScript: the forms of URScript and their effect on the program.