Controlling the recording

Recording settings or methods of starting and stopping the recording and also saving the values are contained in the ScopeProject class, which was already created in the first step.

In order to start a recording you should first check whether data still exist. If so they can be deleted with Disconnect. After that the recording can be started in the ScopeProject with the "StartRecord" method.

private void btnStart_Click(object sender, EventArgs e)
{
  try
  {
    //discard old data
    if (scopeProjectPanel.ScopeProject.ScopeState == TwinCAT.Measurement.Scope.API.ScopeViewState.Reply)
      scopeProjectPanel.ScopeProject.Disconnect(false);

    //start record
    if (scopeProjectPanel.ScopeProject.ScopeState == TwinCAT.Measurement.Scope.API.ScopeViewState.Config)
      scopeProjectPanel.ScopeProject.StartRecord();
  }
  catch (Exception err)
  {
    MessageBox.Show(this, err.Message, "Error on start record!",MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

To stop a recording the "StopRecord" method is called on the ScopeProject. Before executing this method it is also possible to check whether a recording is running. If this is the case the ScopeState in the ScopeProject will indicate Record.

private void btnStop_Click(object sender, EventArgs e)
{
  try
  {
    if (scopeProjectPanel.ScopeProject.ScopeState == TwinCAT.Measurement.Scope.API.ScopeViewState.Record)
    {
      scopeProjectPanel.ScopeProject.StopRecord();
    }
  }
  catch (Exception err)
  {
    MessageBox.Show(this, err.Message, "Error on stop record!",MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

Before closing the application, a running recording should be stopped first and the application should be cleanly disconnected from the Scope server.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (scopeProjectPanel != null && scopeProjectPanel.ScopeProject != null)
  {
    if (scopeProjectPanel.ScopeProject.ScopeState == TwinCAT.Measurement.Scope.API.ScopeViewState.Record)
    {
      scopeProjectPanel.ScopeProject.StopRecord();
    }
    if (scopeProjectPanel.ScopeProject.ScopeState == TwinCAT.Measurement.Scope.API.ScopeViewState.Reply)
    {
      scopeProjectPanel.ScopeProject.Disconnect();
    }
  }
}