This article shows how to set breakpoints in TP and Karel programs of a Fanuc controller from a PC, and how to build a small TP editor with breakpoints, using the Telnet and FTP features of the Fanuc SDK.

Fanuc controllers support breakpoints, but the TP editor of ROBOGUIDE and the teach pendant do not show them. With breakpoints, a program stops at a given line, without flags or other synchronization in the program.

<p align="center">
  <img
    src="/fanuc/winforms/editor-with-breakpoints.gif"
    alt="Fanuc TP Editor with breakpoints"
    style={{ maxWidth: '100%' }}
  />
</p>

## Breakpoints

### What a breakpoint does

A breakpoint pauses a program at a given line, so that you can look at the state of the robot and of the variables. A program can have several breakpoints, in TP and in Karel programs.

### Add and remove breakpoints

Telnet KCL manages the breakpoints:

- `FanucRobot.Telnet.AddBreakpoint`: adds a breakpoint at a line.
- `FanucRobot.Telnet.GetBreakpoints`: lists the breakpoints of a program.
- `FanucRobot.Telnet.RemoveAllBreakpoints`: removes all the breakpoints of a program.

The controller removes the breakpoints of a task when the task is aborted.

### Example

**C# : TpEditorWithBreakpoints**
```csharp
using UnderAutomation.Fanuc;
using UnderAutomation.Fanuc.Telnet;

public class TpEditorWithBreakpoints
{
  static void Main()
  {
    // Create a new Fanuc robot instance
    FanucRobot robot = new FanucRobot();

    // Set connection parameters
    ConnectionParameters parameters = new ConnectionParameters("192.168.0.1");
    parameters.Telnet.Enable = true;
    parameters.Telnet.TelnetKclPassword = "TELNET_PASS";

    // Connect to the robot
    robot.Connect(parameters);

    /**/
    // Add a breakpoint at line 10 of TP or Karel program MyProgram
    robot.Telnet.AddBreakpoint("MyProgram", 10);

    // List all breakpoints of TP program MyProgram
    BreakpointsResult result = robot.Telnet.GetBreakpoints("MyProgram");
    foreach (Breakpoint breakpoint in result.Breakpoints)
    {
      Console.WriteLine($"Breakpoint at line {breakpoint.Line}");
    }

    // Remove all breakpoints of TP program MyProgram
    robot.Telnet.RemoveAllBreakpoints("MyProgram");
    /**/
  }

}
```

**Python : TpEditorWithBreakpoints**
```python
from underautomation.fanuc.fanuc_robot import FanucRobot
from underautomation.fanuc.connection_parameters import ConnectionParameters

# Create a new Fanuc robot instance
robot = FanucRobot()

# Set connection parameters
parameters = ConnectionParameters("192.168.0.1")
parameters.telnet.enable = True
parameters.telnet.telnet_kcl_password = "TELNET_PASS"

# Connect to the robot
robot.connect(parameters)

##
# Add a breakpoint at line 10 of TP or Karel program MyProgram
robot.telnet.add_breakpoint("MyProgram", 10)

# List all breakpoints of TP program MyProgram
result = robot.telnet.get_breakpoints("MyProgram")
for breakpoint in result.breakpoints:
    print(f"Breakpoint at line {breakpoint.line}")

# Remove all breakpoints of TP program MyProgram
robot.telnet.remove_all_breakpoints("MyProgram")
##
```

## A TP editor with breakpoints

### Components

The demo application has a TP editor with breakpoints, written with Windows Forms:

- a `TreeView` that lists the `*.ls` files of the controller;
- a `RichTextBox` with the syntax highlighting of TP code;
- a margin that sets the breakpoints, shows the line being executed, and numbers the lines from the `/MN` section;
- buttons to save and run the program.

### SDK features used

It uses the [FTP features](/fanuc/documentation/ftp) of the SDK to:

- list the files;
- read the `*.ls` files;
- upload the modified programs;
- monitor the tasks in the background: task names, call stacks and current line.

And the [Telnet features](/fanuc/documentation/telnet) to start and stop the program, and to run it line by line.

### Source and video

The source is the C# class [TPEditorControl](https://github.com/underautomation/Fanuc.NET/blob/main/UnderAutomation.Fanuc.Showcase.Forms/Components/TPEditorControl.cs) of the [demo application](/fanuc/documentation/demo-app).

<p align="center">
  <iframe
    width="560"
    height="315"
    style={{ maxWidth: '100%' }}
    src="https://www.youtube.com/embed/Wxxd21P8NLM"
    title="TP editor with breakpoints"
    frameBorder="0"
    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
    allowFullScreen="true"
  ></iframe>
</p>

### Limits of the demo

- Karel programs are not edited.
- The call stack is simplified: the demo assumes that the task and the program are the same.

## Reference

**Members of Common.Kcl.BreakpointsResult**
```csharp
public class BreakpointsResult : Result {
    public BreakpointsResult()

    // Gets the breakpoints set on the task.
    public Breakpoint[] Breakpoints { get; }

    // Indicates that responses have been completed and received
    protected override void EndReceive()

    // During implementation, return true if it is assumed that the frame has finished being received.
    protected override bool FromResult(string data)

    // Controls whether the result should be finalized when an empty answer is received (i.e. a frame consisting only of VT100 escape sequences and whitespace).
    // Override and return false until meaningful data has been accumulated, to prevent premature finalization caused by intermediate display-update frames.
    // Default is true (backward compatible: finalize immediately on empty answer).
    protected override bool ShouldCompleteOnEmptyAnswer()
}
```

**Members of Common.Kcl.Breakpoint**
```csharp
public class Breakpoint {
    public Breakpoint()

    public override bool Equals(object obj)

    public override int GetHashCode()

    // Gets the line number where the breakpoint is set.
    public int Line { get; }

    public override string ToString()
}
```

**Members of Common.Kcl.AddBreakpointResult**
```csharp
public class AddBreakpointResult : Result {
    public AddBreakpointResult()

    // During implementation, return true if it is assumed that the frame has finished being received.
    protected override bool FromResult(string data)
}
```

**Members of Common.Kcl.RemoveBreakpointResult**
```csharp
public class RemoveBreakpointResult : Result {
    public RemoveBreakpointResult()

    // During implementation, return true if it is assumed that the frame has finished being received.
    protected override bool FromResult(string data)
}
```