UnderAutomation
質問ですか?

[email protected]

お問い合わせ
UnderAutomation
⌘Q
このページは英語でのみ提供されています。

Read and write robot variables in C# and Python

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.

  • Prerequisites
  • Brand support
  • Example
  • Read and write a variable on each controller of a cell
  • Write the other types
  • What changes from one brand to another
  • Errors
  • What to read next

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 or Get started with Python.
  • The connection settings of each controller. See Connect to your robot.
  • The license of each brand you connect, or the 30 day trial. See Licensing.
  • 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.

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.

Variables
MethodFanucYaskawaUniversal RobotsABBStäubli
Variable.ReadCGTP (Cgtp), SNPXHigh Speed Ethernet Server, Ethernet ServerDashboard Server, Primary Interface (Primary)Robot Web ServicesNot available
Variable.WriteIntegerCGTP (Cgtp), SNPXHigh Speed Ethernet Server, Ethernet ServerInterpreter Mode (manual)Robot Web ServicesNot available
Variable.WriteRealCGTP (Cgtp), SNPXHigh Speed Ethernet Server, Ethernet ServerInterpreter Mode (manual)Robot Web ServicesNot available
Variable.WriteStringCGTP (Cgtp), SNPXHigh Speed Ethernet Server, Ethernet ServerInterpreter Mode (manual)Robot Web ServicesNot available
Variable.WriteBooleanCGTP (Cgtp), SNPXHigh Speed Ethernet Server, Ethernet ServerInterpreter Mode (manual)Robot Web ServicesNot available
Methods of this article5 of 55 of 55 of 55 of 50 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).

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();
}
}
}
Click to see the full code

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.

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();
}
Click to see the full code

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.

BrandNative nameExamplesBefore a writeBrand article
FanucRegister 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 moreRead and write registers
YaskawaLetter of the type, then the index of the variable: B (byte), I (integer), D (double integer), R (real), S (string).B000, I012, D003, R005, S010Nothing moreRead and write variables
Universal RobotsName of a global variable of the program that runs on the robot.my_varThe writes have one route, Interpreter Mode: enable it in the connection parameters, and run the interpreter program on the robot. See Routing settings.Variables
ABBPath 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.Read and write RAPID variables
StäubliNot 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 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 explains each exception.

What to read next

  • Variable: positions, comments, lists and reads of several variables.
  • Read and write robot IO: the signals of the same controllers.
  • Which protocol for which task: what each route needs on the controller.