Read & write RAPID variables
Read and write a RAPID num, bool, string, robtarget or a custom record from C#, on IRC5 and on OmniCore.
To read a RAPID variable from C#, call robot.Rws.Rapid.GetSymbolValue(path). To write one, take the RAPID mastership and call SetSymbolValue(path, value). Values travel as text, written the way RAPID writes them: a num is "42", a bool is "TRUE", a robtarget is one bracketed line.
The path of a variable
A variable is named by its path in the RAPID tree: the task, the module, then the name of the symbol. reg1 and the other predefined registers live in the built-in module user.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// A variable declared in the module MainModule of the task T_ROB1robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/myCounter");// reg1 and the other predefined registers live in the built-in module "user"robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/user/reg1");// A leading slash is accepted, it is removed by the SDKrobot.Rws.Rapid.GetSymbolValue("/RAPID/T_ROB1/user/reg1");// A module of the task, and the task itself, are symbols toorobot.Rws.Rapid.GetSymbolProperties("RAPID/T_ROB1/MainModule");robot.Rws.Rapid.GetSymbolProperties("RAPID/T_ROB1");// A RAPID type has a path of its own, without any taskrobot.Rws.Rapid.GetSymbolProperties("RAPID/robtarget");robot.Disconnect();}
Variables, persistents and constants are read the same way. A persistent is the usual choice for data exchanged with a PC, because its value survives a program restart.
Read a num, a bool or a string
Reading needs no mastership. The value comes back in RapidSymbolValue.Value, as text.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// A value always comes back as the text RAPID writes it with. Reading needs no mastership.RapidSymbolValue value = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/user/reg1");Console.WriteLine(value.Value); // "42"Console.WriteLine(value.DeclarationPosition); // where the declaration sits in the module// num and dnum: parse with the invariant culture, RAPID uses a dot as decimal separatordouble number = double.Parse(robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/user/reg1").Value,CultureInfo.InvariantCulture);// bool: the controller writes TRUE or FALSEbool flag = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/myFlag").Value.Trim().Equals("TRUE", StringComparison.OrdinalIgnoreCase);// string: the value carries the RAPID quotes, remove themstring text = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/myText").Value.Trim('"');Console.WriteLine(number + " " + flag + " " + text);robot.Disconnect();}
Two details cost time when they are discovered late:
- RAPID writes numbers with a dot as decimal separator. Parse with
CultureInfo.InvariantCulture, otherwise a French or German machine reads1.5as15. - A
stringcarries its RAPID quotes. Trim them.
Write a value
Writing needs the RAPID mastership. Take it, write, give it back in a finally block.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// Writing needs the RAPID mastership. Take it, write, give it back.robot.Rws.Mastership.Request(MastershipDomain.Rapid);try{// num: format with the invariant culture, "1,5" is refused, "1.5" is accepteddouble speed = 1.5;robot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/user/reg1",speed.ToString(CultureInfo.InvariantCulture));// bool: TRUE or FALSE, in capitalsrobot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/MainModule/myFlag", "TRUE");// string: the RAPID quotes are part of the valuerobot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/MainModule/myText", "\"hello\"");}finally{robot.Rws.Mastership.Release(MastershipDomain.Rapid);}robot.Disconnect();}
A value the controller refuses, a text where a number is expected for example, fails with the HTTP status code 400. A write attempted without the mastership fails with 403.
SetSymbolValue changes the value the program uses now. SetSymbolInitialValue changes the value written in the declaration, which is what the variable goes back to when the module is reloaded.
Check a value before writing it
ValidateSymbolValue asks the controller whether it would accept a text for a given RAPID type, without writing anything. It answers false instead of throwing, so it fits well after a user input.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// Ask the controller whether it would take the value, without writing it anywhereConsole.WriteLine(robot.Rws.Rapid.ValidateSymbolValue("T_ROB1", "num", "1.5")); // true// A value that does not fit the type gives false, it does not throwConsole.WriteLine(robot.Rws.Rapid.ValidateSymbolValue("T_ROB1", "num", "hello")); // falserobot.Rws.Mastership.Request(MastershipDomain.Rapid);try{if (robot.Rws.Rapid.ValidateSymbolValue("T_ROB1", "num", "1.5"))robot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/user/reg1", "1.5");// The value written in the declaration, the one the symbol goes back to when the// program is reset. This rewrites the source of the module.robot.Rws.Rapid.SetSymbolInitialValue("RAPID/T_ROB1/MainModule/myCounter", "0");}finally{robot.Rws.Mastership.Release(MastershipDomain.Rapid);}robot.Disconnect();}
Arrays and records
An array and a record are both one value, written between brackets. An array of three num is [1,2,3]. A robtarget is [trans, rot, robconf, extax], where the first two fields are records themselves. There is no partial write: you read the whole value, change what you need, and write the whole value back.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// A record comes back in the bracketed form RAPID writes it with. The SDK does not// take it apart, you get the text and parse what you need.RapidSymbolValue target = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/pHome");Console.WriteLine(target.Value);// [[515,0,712],[0,0,1,0],[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]]// An array is one value too, its elements separated by commasConsole.WriteLine(robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/myArray").Value); // [1,2,3]// Build the text with the invariant culture, a comma as decimal separator is refuseddouble x = 515.5, y = 0, z = 712;string pose = string.Format(CultureInfo.InvariantCulture,"[[{0},{1},{2}],[0,0,1,0],[0,0,0,0],[9E9,9E9,9E9,9E9,9E9,9E9]]",x, y, z);robot.Rws.Mastership.Request(MastershipDomain.Rapid);try{// The value has to carry every component the type declaresrobot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/MainModule/pHome", pose);}finally{robot.Rws.Mastership.Release(MastershipDomain.Rapid);}robot.Disconnect();}
Parse a record into an object
The SDK gives the text as the controller wrote it and does not take it apart, because a record can be any type you declared. Splitting the top level of a bracketed value is enough to read an array, a robtarget, or your own record.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// An array of num arrives as one bracketed text : [1,2.5,3]string arrayText = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/myArray").Value;string[] items = SplitRapidValue(arrayText);double[] numbers = new double[items.Length];for (int i = 0; i < items.Length; i++)numbers[i] = double.Parse(items[i], CultureInfo.InvariantCulture);// Writing takes the same text back. The whole array is written at once.robot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/MainModule/myArray", "[1,2.5,3]");// A record is bracketed too, and its fields can be records themselves.// A robtarget is [trans, rot, robconf, extax].string targetText = robot.Rws.Rapid.GetSymbolValue("RAPID/T_ROB1/MainModule/pHome").Value;string[] fields = SplitRapidValue(targetText);string[] trans = SplitRapidValue(fields[0]); // [515,0,712]string[] rot = SplitRapidValue(fields[1]); // [0.707107,0,0.707107,0]RobTarget home = new RobTarget();home.X = double.Parse(trans[0], CultureInfo.InvariantCulture);home.Y = double.Parse(trans[1], CultureInfo.InvariantCulture);home.Z = double.Parse(trans[2], CultureInfo.InvariantCulture);home.Orientation = new Quaternion(double.Parse(rot[0], CultureInfo.InvariantCulture),double.Parse(rot[1], CultureInfo.InvariantCulture),double.Parse(rot[2], CultureInfo.InvariantCulture),double.Parse(rot[3], CultureInfo.InvariantCulture));Console.WriteLine($"pHome is at X={home.X} Y={home.Y} Z={home.Z}");// Writing the record back : move it 10 mm up and rebuild the text.// The two last fields are kept as they were read.home.Z += 10;string newValue = "[" + FormatTrans(home) + "," + FormatRot(home) + "," + fields[2] + "," + fields[3] + "]";// Ask the controller whether it accepts the text before writing itif (robot.Rws.Rapid.ValidateSymbolValue("T_ROB1", "robtarget", newValue))robot.Rws.Rapid.SetSymbolValue("RAPID/T_ROB1/MainModule/pHome", newValue);// Splits the top level of a RAPID value : "[[1,2],3]" gives "[1,2]" and "3".// A value that is not bracketed is returned as a single element.static string[] SplitRapidValue(string value){string text = value == null ? "" : value.Trim();if (!text.StartsWith("[") || !text.EndsWith("]"))return new string[] { text };text = text.Substring(1, text.Length - 2);List<string> parts = new List<string>();StringBuilder current = new StringBuilder();int depth = 0;bool inString = false;foreach (char c in text){if (c == '"') inString = !inString;if (!inString && c == '[') depth++;if (!inString && c == ']') depth--;if (!inString && c == ',' && depth == 0){parts.Add(current.ToString().Trim());current.Length = 0;}else{current.Append(c);}}if (current.Length > 0)parts.Add(current.ToString().Trim());return parts.ToArray();}// RAPID always writes numbers with a dot, so does the invariant culturestatic string Number(double value){return value.ToString(CultureInfo.InvariantCulture);}static string FormatTrans(RobTarget target){return "[" + Number(target.X) + "," + Number(target.Y) + "," + Number(target.Z) + "]";}static string FormatRot(RobTarget target){Quaternion q = target.Orientation;return "[" + Number(q.Q1) + "," + Number(q.Q2) + "," + Number(q.Q3) + "," + Number(q.Q4) + "]";}}
To read the current position of the robot as a RobTarget object rather than as text, use the motion system instead. See Get the robot position.
Find the variables of a program
You do not always know the module a variable is declared in. SearchSymbols walks the loaded program and returns the symbols matching a criteria: a name pattern, a data type, a kind of symbol, one block or the whole task.
AbbController robot = new AbbController();robot.Connect("192.168.0.1");// Always give a starting point, a search without any criterion walks the whole systemRapidSymbolSearchCriteria criteria = new RapidSymbolSearchCriteria();criteria.View = RapidSymbolSearchView.Block;criteria.BlockUrl = "RAPID/T_ROB1";criteria.Recursive = true;criteria.NamePattern = "^p[0-9]+$"; // regular expression on the namecriteria.DataType = "robtarget";// Only the first entry is sent, search again for another kindcriteria.SymbolTypes = new RapidSymbolType[] { RapidSymbolType.Persistent };foreach (RapidSymbolProperties symbol in robot.Rws.Rapid.SearchSymbols(criteria)){Console.WriteLine(symbol.Name + " (" + symbol.DataType + ")");// SymbolUrl is the path the other symbol methods takeConsole.WriteLine(robot.Rws.Rapid.GetSymbolValue(symbol.SymbolUrl).Value);}// What one symbol is declared as, without reading its valueRapidSymbolProperties properties = robot.Rws.Rapid.GetSymbolProperties("RAPID/T_ROB1/user/reg1");Console.WriteLine(properties.SymbolType); // Variable, Persistent, Constant, ...Console.WriteLine(properties.DataType); // numConsole.WriteLine(properties.ReadOnly); // null when the controller did not report itrobot.Disconnect();}
Reading many values
There is no batch read, one variable is one request. A loop over 200 variables is 200 requests, which is slow on a controller that also has a program to run. When a lot of data has to be published, group it in one RAPID record or one array and read that in a single call.
Going further
- RAPID variables & symbols, the complete reference of the symbol methods
- Mastership, when and how to take the write lock
- Start & stop a RAPID program
- RAPID modules & program files, to load a module that declares your variables
// Gets the declaration the controller finds at a position of a module (synchronous)RapidModuleSymbol GetModuleSymbol(string task, string module, int row, int column);// Gets what a RAPID symbol is declared as (synchronous)RapidSymbolProperties GetSymbolProperties(string symbolUrl);// Gets the value of a RAPID symbol and where it is declared (synchronous)RapidSymbolValue GetSymbolValue(string symbolUrl);// Finds the RAPID symbols matching a set of criteria (synchronous)RapidSymbolProperties[] SearchSymbols(RapidSymbolSearchCriteria criteria);// Sets the value a RAPID symbol is declared with, which is the one it goes back to when the program is reset (synchronous)void SetSymbolInitialValue(string symbolUrl, string value);// Sets the value a RAPID symbol currently holds (synchronous)void SetSymbolValue(string symbolUrl, string value);// Asks the controller whether a value would be accepted for a given RAPID type, without writing it anywhere (synchronous) This is what an editor uses to tell an operator that what they typed is wrong before the write is attempted.bool ValidateSymbolValue(string task, string dataType, string value);
Every method also exists in an asynchronous version, with the same name followed by Async and an optional CancellationToken.
The value of a RAPID symbol and where it is declared. Returned by RapidService.GetSymbolValue(). The value is the text the controller wrote it as, which for a record is the bracketed form RAPID itself uses, for example [[515,0,712],[0.707107,0,0.707107,0],[0,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]].
| Member | Type | Description |
|---|---|---|
RapidSymbolValue() Constructor | Initializes a new instance of the RapidSymbolValue class | |
DeclarationPosition Property | RapidTextRange | Where the symbol is declared, null when the controller did not report it |
InitialValuePosition Property | RapidTextRange | Where the initial value of the symbol is written, null when the controller did not report it. The controller reports zeros when the declaration carries no initial value. |
Value Property | string | Value of the symbol, written the way RAPID writes it |
ToString() Method | string | Returns a string representation of this value |
What a RAPID symbol is declared as. Returned by RapidService.GetSymbolProperties() and RapidService.SearchSymbols(). A search fills in Name and leaves Storage alone, a direct read does the opposite on some controllers, so treat both as optional.
| Member | Type | Description |
|---|---|---|
RapidSymbolProperties() Constructor | Initializes a new instance of the RapidSymbolProperties class | |
DataType Property | string | Name of the type of the symbol, for example "num" |
Dimension Property | string | Size of each array dimension as the controller worded it, empty when the symbol is not an array |
Dimensions Property | int? | Number of array dimensions of the symbol, null when the controller did not report it |
Heap Property | bool? | Whether the symbol is allocated on the heap, null when the controller did not report it |
Linked Property | bool? | Whether the declaration is complete, null when the controller did not report it |
Local Property | bool? | Whether the symbol is local to its module, null when the controller did not report it |
Name Property | string | Name of the symbol, for example "reg1" |
Named Property | bool? | Whether the symbol is named, null when the controller did not report it |
ReadOnly Property | bool? | Whether the symbol may not be written, null when the controller did not report it |
Storage Property | string | How the controller stores the symbol, for example "loaded" |
SymbolType Property | RapidSymbolType | What kind of symbol this is |
SymbolUrl Property | string | Path of the symbol, which the other symbol methods take |
TaskVariable Property | bool? | Whether the symbol is global within its task, null when the controller did not report it |
TypeUrl Property | string | Path of the type of the symbol, for example "RAPID/num" |
ToString() Method | string | Returns a string representation of this symbol |
What a symbol search looks for. Passed to RapidService.SearchSymbols(). Every property is optional; leaving one alone means the search does not filter on it. A search with no criterion at all walks the whole system, which is slow, so at least set BlockUrl.
| Member | Type | Description |
|---|---|---|
RapidSymbolSearchCriteria() Constructor | Initializes a new instance of the RapidSymbolSearchCriteria class | |
BlockUrl Property | string | Path the search starts from, for example "RAPID/T_ROB1" |
DataType Property | string | Name of the type a symbol has to have to be kept, for example "robtarget" |
NamePattern Property | string | Regular expression the name of a symbol has to match to be kept |
OnlyUsed Property | bool? | Whether only the symbols the program actually refers to are kept, null to leave it to the controller |
PositionColumn Property | int? | Column the search starts from, used together with Scope |
PositionRow Property | int? | Line the search starts from, used together with Scope |
Recursive Property | bool? | Whether the search also walks what the starting point contains, null to leave it to the controller |
SkipShared Property | bool? | Whether the symbols shared between tasks are skipped, null to leave it to the controller |
StackFrame Property | int? | Frame of the call stack the search starts from, used together with Stack |
SymbolTypes Property | RapidSymbolType[] | Kinds of symbol the search keeps, empty to keep every kind |
VariableType Property | RapidSymbolVariableType | Which variables the search keeps, by what may be done with them |
View Property | RapidSymbolSearchView | Which part of the system the search walks |