Interpreter Mode lets you send URScript statements while your program is running. To do this, your robot program must call the blocking function `interpreter_mode()`. While your robot program is in this mode, you can call any of the functions described below. For more information on Interpreter Mode for Universal Robots, please refer to the official documentation: https://www.universal-robots.com/articles/ur/programming/interpreter-mode

![interpreter mode polyscope](/universal-robots/interpreter-mode-polyscope.jpg)

In the robot, the Interpreter Mode Socket must be enabled in the Polyscope settings for security reasons, see : [this page](connect)

## Example

Once your robot program is in interpreter mode, you can play with the functions in `robot.InterpreterMode`.

**C# : InterpreterMode**
```csharp
using UnderAutomation.UniversalRobots;

class InterpreterMode
{
  static void Main()
  {
    var robot = new UR();

    var param = new ConnectParameters("192.168.0.1");

    // Enable Interpreter Mode
    param.InterpreterMode.Enable = true;

    // Connect to robot
    robot.Connect(param);

    //...

    // append lines of URScript to execute. The robot program should execute interpreter_mode() to run this.
    robot.InterpreterMode.ExecuteCommand("movej([0.94, -1.3, 2.2, -2.6, -1, 4], a=1, v=1)");
    robot.InterpreterMode.ExecuteCommand("set_digital_out(1, True)");
    robot.InterpreterMode.ExecuteCommand("movej([0.94, -1.3, 2.2, -2.6, -1, 4], a=1, v=1)");

    // ...

    // Request the robot program to quit interpreter_mode() and continue the program
    robot.InterpreterMode.EndInterpreter();

    // Disconnect from interpreter mode only
    robot.InterpreterMode.Disconnect();
  }
}
```

## Connect

To enable interpreter mode, you must enable it in the connect parameters.

```cs
var param = new ConnectParameters("192.168.0.1");

// Enable Interpreter Mode
param.InterpreterMode.Enable = true;
```

## Send script statements

Then, you can call `ExecuteCommand()` method to append script to the robot internal buffer.

## End interpreter mode

The method `EndInterpreterMode()` allows to stop interpreter mode and force the robot to exit `interpreter_mode()` and continue the program execution.

## Skip buffer

All script statements are stored in a buffer. You can empty this buffer with the method `SkipBuffer()`.

## Get state

Methods `StateLastExecuted()`, `StateLastInterpreted()`, `StateLastCleared()`, `StateLastUnexecuted()` returns informations about executing instructions.

## Try it with the Windows example

![interpreter mode](/universal-robots/WinformsScreenshots/InterpreterMode.jpg)

## API reference

**Members of InterpreterMode.Internal.InterpreterModeClientBase**
```csharp
public abstract class InterpreterModeClientBase : URServiceBase {
    protected InterpreterModeClientBase()

    // The interpreter mode offers a mechanism to abort limited number of script functions, even if they are
    // called from the main program. Currently only movej and movel can be aborted.
    // Aborting a movement will result in a controlled stop if no blend radius is defined.
    // If a blend radius is defined then a blend with the next movement will be initiated right away if not
    // already in an initial blend, otherwise the command is ignored.
    // Return value should be ignored
    public CommandResponse Abort()

    // Clears all interpreted statements, objects, functions, threads, etc. generated in the current
    // interpreter mode.Threads started in current interpreter session will be stopped, and deleted.
    // Variables defined outside of the current interpreter mode will not be affected by a call to thisfunction.
    // Only statements interpreted before the clear_interpreter() function will be cleared.
    // Statements sent after clear_interpreter() will be queued. When cleaning is done, any
    // statements queued are interpreted and responded to. Note that commands such as abort,
    // skipbuffer and state commands are executed as soon as they are received.
    public CommandResponse ClearInterpreter()

    protected void ConnectInternal(string ip, int port)

    // Indicates that the interpreter mode client is connected and ready to send commands
    public bool Connected { get; }

    // Disconnect interpreter mode socket connection
    public void Disconnect()

    // Ends the interpreter mode, and causes the interpreter_mode() function to return. This
    // function can be compiled into the program by sending it to the interpreter socket(30020) as any
    // other statement, or can be called from anywhere else in the program.
    // By default everything interpreted will be cleared when ending, though the state of the robot, the
    // modifications to local variables from the enclosing scope, and the global variables will remain
    // affected by any changes made.The interpreter thread will be idle after this call.
    public CommandResponse EndInterpreter()

    // Executes a command on the Interpreter Mode
    public CommandResponse ExecuteCommand(string command)

    // IP of the robot to connect to for sending commands
    public string IP { get; }

    // Interpreter mode server port
    public int Port { get; set; }

    // The interpreter mode furthermore supports the opportunity to skip already sent but not executed
    // statements.The interpreter thread will then(after finishing the currently executing statement) skip
    // all received but not executed statements.
    // After the skip, the interpreter thread will idle until new statements are received.skipbuffer will
    // only skip already received statements, new statements can therefore be send right away.
    // Return value should be ignored
    public CommandResponse SkipBuffer()

    // Replies with the id for the latest statement to be cleared from the interpreter mode. This clear can happen when ending interpreter mode, or by calls to clear_interpreter()
    public CommandResponse StateLastCleared()

    // Replies with the largest id of a statement that has started being executed.
    public CommandResponse StateLastExecuted()

    // Replies with the latest interpreted id, i.e. the highest number of interpreted statement so far.
    public CommandResponse StateLastInterpreted()

    // Replies with the number of non executed statements, i.e. the number of statements that would have be skipped if skipbuffer was called instead.
    public CommandResponse StateLastUnexecuted()
}
```
**Members of InterpreterMode.CommandResponse**
```csharp
public class CommandResponse {
    // Answer from the interpreter mode
    public string Body { get; }

    // Command sent to the interpreter mode
    public string Command { get; }

    // Command unique identifier
    public int Id { get; }

    // Raw line sent from the controller
    public string RawAnswer { get; }

    // Response type to check if command succeed
    public CommandResponseStatus Status { get; }

    public override string ToString()
}
```
**Members of InterpreterMode.CommandResponseStatus**
```csharp
public enum CommandResponseStatus {
    // The command compilation succeed and Interpreter mode will execute the statement
    Ack = 0

    // Program is not running or the statement results in a compilation or linker error
    Discard = 1

    // Something went wrong when receiving response
    Error = -1

    // Answer from a state command
    State = 2
}
```
**Members of InterpreterMode.Internal.InterpreterModeClientParametersBase**
```csharp
public abstract class InterpreterModeClientParametersBase {
    protected InterpreterModeClientParametersBase()

    // Default Interpreter Mode server TCP port
    public const int DEFAULT_PORT = 30020

    // Interpreter Mode client TCP port. Default : 30020
    public int Port { get; set; }
}
```
**Members of Common.InterpreterModeConnectParameters**
```csharp
public class InterpreterModeConnectParameters : InterpreterModeClientParametersBase {
    // Enable Interpreter Mode client communication
    // Default value is false
    public bool Enable { get; set; }
}
```