Universal Robots Forum

CREATE and ASSIGN a STRING VARIABLE IN URCAP

Hello,
I am developing a urcap and I have the following error:
I would like to create a variable in the urcap and assign this variable to a String value that is going to read it from another device,
I could create the variable, set the variable and get selected variable using the following functions:

public Variable getSelectedVariable(){
return model.get(KEY_REPLY_FROM_SERVER, (Variable)null);
}

public void setVariable(final Variable variable){
    undoRedoManager.recordChanges(new UndoableChanges() {

        @Override
        public void executeChanges() {
           model.set(KEY_REPLY_FROM_SERVER, variable);    
        }
    });
}

public GlobalVariable createGlobalVariable(String variableName){
    GlobalVariable variable = null;
    try {
        variable = variableFactory.createGlobalVariable(variableName, programAPI.getValueFactoryProvider().createExpressionBuilder().append("0").build());
    } catch (VariableException variableException) {
        System.out.println("Could not crerate the variable. ERROR " + variableException);
        variableException.printStackTrace();
    } catch (InvalidExpressionException e) {
        e.printStackTrace();
    }
    return variable;
}

public void generateScript(ScriptWriter writer) {
Variable errorReset = getSelectedVariable();
if (errorReset != null) {
String errorVariableStringValue = writer.getResolvedVariableName(errorReset);
//errorVariableStringValue.toString();
writer.appendLine(errorVariableStringValue + “=” +""" + “SHOULD BE STRING NOT INT” + “”");
}

and everything is working well if I assign the variable to an integer value, but I need to assign this variable to a string value…

do you have any proposal.?

Best Regards.

Hi @eyas,

Welcome to the UR community!

Your issue is that URScript variables cannot change type. It looks like you initiated it with an integer.

So I would suggest you make the createGlobalVariable(String) method instead of createGlobalVariable(String, Expression)

Hope that solves your issue.

Best regards
Ebbe

Hi @Ebbe
Thanks for your reply, and welcoming me.
there is no error any more but something is still missing, if you check the image attached,
I could get an output as a variable(String) in the program(teach pendant) but I could not have the same value in the terminal, that I need to use the result in the URCAP or clearly in the background,

even if I use : String errorVariableStringValue = writer.getResolvedVariableName(errorReset); and assign it to a String, the output in the terminal is the same.
For further information or explanation, please let me know!!

On the java side errorReset is a variable that is equal to the String “errorVariable”; while on the script side you are creating a variable called errorVariable that is equal to the String “testingOutput” so the System.out call is displaying the correct String on the defined variable.

Are you trying to use the value of the script variable on the Java side as well? Or are you just trying to see it in this case?

1 Like

I need to use the variable in the urcap(java side) !

A convenient way to serialize data from the controller to the java side is XML-RPC. You can find one sample via a Daemon process here: