Read and write robot IO in C# and Python
Read the inputs and write the outputs of Fanuc, Yaskawa, Universal Robots, ABB and Stäubli controllers with one code: signal names of each brand, brand support table, C# and Python examples.
This article shows how to read the inputs and write the outputs of Fanuc, Yaskawa, Universal Robots, ABB and Stäubli controllers from a PC, with one code in C# or Python. Only the address of the controller and the native name of the signal change from one brand to another: the table at the end gives the names of each brand.
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 each signal, the one of the pendant and the programs. See the table at the end.
- An output is connected to the cell. Writing it can start a machine, close a gripper or open a valve. Check what the signal does before you write it, and test on a simulator first.
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.
| Method | Fanuc | Yaskawa | Universal Robots | ABB | Stäubli |
|---|---|---|---|---|---|
| Io.Read | SNPX, CGTP (Cgtp) | High Speed Ethernet Server, Ethernet Server | RTDE, Primary Interface (Primary) | Robot Web Services | SOAP |
| Io.WriteBoolean | SNPX, CGTP (Cgtp), CGTP (CgtpKcl), Telnet | High Speed Ethernet Server, Ethernet Server | Primary Interface (Script), Interpreter Mode (manual) | Robot Web Services | SOAP |
| Io.WriteNumber | SNPX, CGTP (Cgtp), CGTP (CgtpKcl), Telnet | High Speed Ethernet Server, Ethernet Server | Primary Interface (Script), Interpreter Mode (manual) | Robot Web Services | SOAP |
| Methods of this article | 3 of 3 | 3 of 3 | 3 of 3 | 3 of 3 | 3 of 3 |
Example
Read an input and write an output on each controller of a cell
The example connects to the controllers of a cell one after the other. It reads one input and switches one output on. Read returns an IoValue: IsOn is true when the signal is on, or when its number is not 0. WriteBoolean writes a digital signal.
static void Main(){// One line per controller: the brand, the address, an input and an output, with their native namesvar cell = new[]{(Brand: "fanuc", Address: "192.168.0.1", Input: "DI[1]", Output: "DO[1]"),// Yaskawa: #27010 is a network input, the only kind of signal that the controller accepts to write by default(Brand: "yaskawa", Address: "192.168.0.2", Input: "IN#(1)", Output: "#27010"),(Brand: "universalrobots", Address: "192.168.0.3", Input: "DI0", Output: "DO0"),(Brand: "abb", Address: "192.168.125.1", Input: "DI_PartPresent", Output: "DO_Gripper"),(Brand: "staubli", Address: "192.168.0.254", Input: @"BasicIO-1\%I0", Output: @"BasicIO-1\%Q0"),};foreach (var controller in cell){// The same code for each controller of this cellRobotController robot = new RobotController();robot.Connect(RobotConnectionParameters.Create(controller.Brand, controller.Address));IoValue input = robot.Io.Read(controller.Input);Console.WriteLine($"{robot.Brand} {input.Name}: {(input.IsOn ? "on" : "off")}");// An output acts on the cell: check what it does before you write itrobot.Io.WriteBoolean(controller.Output, true);robot.Disconnect();}}}
In Python, the names are in snake_case: robot.io.read(...), robot.io.write_boolean(...). A Stäubli name contains a backslash: write it with a verbatim string in C# (@"BasicIO-1\%I0") and a raw string in Python (r"BasicIO-1\%I0").
Group and analog signals
Number of an IoValue gives the integer of a group signal and the value of an analog signal. WriteNumber writes them. This example uses the signals 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.Io.WriteBoolean("DO[1]", true);robot.Io.WriteNumber("GO[1]", 5);robot.Disconnect();}
What changes from one brand to another
The code does not change. The name of the signal follows the syntax of the controller, and some brands accept the write of some signals only.
| Brand | Native name | Examples | Before a write | Brand article |
|---|---|---|---|---|
| Fanuc | Kind of signal and index, as the pendant writes it. A comment can follow the index after :. | DI[1], DO[1], RI[1], RO[1], UI[1], UO[1], GI[1], GO[1], AI[1], AO[1], F[1] | To give an input a value of your own, simulate it first. See Io. | Set and simulate inputs and outputs |
| Yaskawa | General purpose signals IN#(n) and OT#(n), groups of 8 signals IG#(n) and OG#(n), or a contact number. | IN#(1), OT#(3), IG#(1), OG#(2), #10010, #27010 | By default, the controller accepts the write of the network inputs only (#27010 to #29567). | Read and write IO |
| Universal Robots | Name of the I/O tab of PolyScope. | DI0 to DI7, DO0 to DO7, CI0 to CI7, CO0 to CO7, TI0, TI1, TO0, TO1, AI0, AI1, AO0, AO1 | The default write route sends a script: the program that runs on the robot stops. On e-Series and PolyScope X, the robot is in remote control. | Read and write IO |
| ABB | Name of the signal in the I/O configuration of the controller. | DI_PartPresent, DO_Gripper | The user of the connection has the write grant. | Read and write IO signals |
| Stäubli | Board and I/O, separated by a backslash. The boards depend on the configuration of the controller. | BasicIO-1\%I0, BasicIO-1\%Q0 | The user of the connection has the right to write the output. | Read and write IO |
On Yaskawa, the controller writes 8 signals at a time. The library reads the byte of the signal, changes the signal and writes the byte back. A signal of the same byte that the ladder, a job or another client changes between the read and the write gets its old value back.
The names of your cell can differ from these examples. robot.Io.List(kind) gives the signals of one kind with their names, on the brands that have this method: see Io.
Errors
FeatureNotSupportedException: the brand has no route for the method.RouteUnavailableException: the method has routes, but none can be used, for example because the protocol did not answer at the connection.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
- Io: comments, lists, reads of several signals and simulation.
- Read and write robot variables: the variables of the brands that have them.
- Monitor the state of a robot controller: follow the controllers of a cell.