.

Serialization Example Code

Visual Basic

 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Examples of saving and loading MPX object configuration
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class SerializationExamples

    ' Save an entire Axis configuration, including child objects.
    Public Sub SaveAxis()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim saver As Mpx.ConfigSaver = New Mpx.ConfigSaver()

       ' Have the OnSavePropertyError method handle PropertyError events
       AddHandler saver.PropertyErrorEvent, AddressOf OnSavePropertyError

       saver.SaveToFile(controller.Axis(0), "axisConfig.xml")
    End Sub

    ' Save Axis configuration, but exclude child objects.
    Public Sub SaveAxisOnly()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim axis0 As Mpx.Axis = controller.Axis(0)
       Dim saver As Mpx.ConfigSaver = new Mpx.ConfigSaver()

       ' Don't save child objects
       saver.IncludeChildren = False

       ' Have the OnSavePropertyError method handle PropertyError events
       AddHandler saver.PropertyErrorEvent, AddressOf OnSavePropertyError

       saver.SaveToFile(axis0, "axisOnlyConfig.xml")
    End Sub

    ' Save only the Controller configuration, excluding child objects.
    Public Sub SaveControllerOnly()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim saver As Mpx.ConfigSaver = New Mpx.ConfigSaver()

       ' Don't save child objects
       saver.IncludeChildren = False

       ' Have the OnSavePropertyError method handle PropertyError events
       AddHandler saver.PropertyErrorEvent, AddressOf OnSavePropertyError

       saver.SaveToFile(controller, "controllerOnlyConfig.xml")
    End Sub

    ' Save the entire Controller configuration, including child objects.
    '
    ' Handle ConfigSavePropertyStatus events in order to display
    ' progress and exclude Can and CanNode objects from being saved.
    '
    ' Handle PropertyError events in order to display errors.
    Public Sub SaveController()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim saver As Mpx.ConfigSaver = New Mpx.ConfigSaver()

       ' Have the OnSavePropertyError method handle PropertyError events
       AddHandler saver.PropertyErrorEvent, AddressOf OnSavePropertyError

       ' Have the OnSavePropertyStatus method handle 
       ' ConfigSavePropertyStatus events
       AddHandler saver.PropertyStatusEvent, AddressOf OnSavePropertyStatus

       saver.SaveToFile(controller, "controllerConfig.xml")
    End Sub

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Handlers for events that occur while saving MPX object configuration.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    ' This method handles PropertyError events that occur while saving
    ' the configuration of an object.
    Public Sub OnSavePropertyError(saver As Mpx.ConfigSaver, _
       e As Mpx.ConfigSavePropertyErrorEventArgs)

       ' Example output:
       ' ERROR: Controller.Axis[2]: Axis is not enabled
       System.Console.WriteLine("ERROR: " + e.PropertyPath + ": " + e.Message)

       If e.ErrorCode = Mpx.ConfigSaveErrorCode.Internal Then
          ' Cancel the save operation
          saver.Cancel()
          System.Console.WriteLine("Canceled!")
       End If
    End Sub

    ' This method handles PropertyStatus events that occur while loading 
    ' configuration to object.
    Public Sub OnSavePropertyStatus(saver As Mpx.ConfigSaver, _
       e As Mpx.ConfigSavePropertyStatusEventArgs)

       ' Print the current property and the percentage done
       ' Example output:
       ' 25%: Controller.Axis[2].AmpDisableAction
       ' skipped
       System.Console.WriteLine(e.PercentageDone + "%: " + e.PropertyPath)

       ' Don't save CAN or CAN Node objects
       If e.PropertyPath.StartsWith("Controller.Can") Then
          System.Console.WriteLine(" skipped")
          e.SkipProperty = True
       End If
    End Sub

    '''''''''''''''''''''''''''''''

    ' Load axis configuration onto 2 different axes
    Public Sub LoadAxes()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim loader As Mpx.ConfigLoader = New Mpx.ConfigLoader()

       ' Have the OnLoadPropertyError method handle PropertyError events
       AddHandler loader.PropertyErrorEvent, AddressOf OnLoadPropertyError

       loader.LoadFromFile(controller.Axis(0), "axisOnlyConfig.xml")
       loader.LoadFromFile(controller.Axis(1), "axisOnlyConfig.xml")
    End Sub

    ' Load controller configuration.
    ' Handle PropertyError events in order to display errors and warnings.
    ' Handle PropertyStatus events in order to display progress.
    Public Sub LoadController()
       Dim controller As Mpx.Controller = New Mpx.Controller(0)
       Dim loader As Mpx.ConfigLoader = New Mpx.ConfigLoader()

       ' Have the OnLoadPropertyError method handle PropertyError events
       AddHandler loader.PropertyErrorEvent, AddressOf OnLoadPropertyError

       ' Have the OnLoadPropertyStatus method handle PropertyStatus
       ' events.
       AddHandler loader.PropertyStatusEvent, AddressOf OnLoadPropertyStatus

       loader.LoadFromFile(controller, "controllerConfig.xml")
    End Sub

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Handlers for events that occur while loading MPX object 
    ' configuration.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    ' This method handles PropertyError events that occur while loading 
    ' configuration to an object.
    Public Sub OnLoadPropertyError(loader As Mpx.ConfigLoader, _
       e As Mpx.ConfigLoadPropertyErrorEventArgs)

       Dim msg As String

       If e.ErrorCode = Mpx.ConfigLoadErrorCode.Missing Then
          If e.PropertyPath.StartsWith("Controller.Can") Then
             ' Ignore missing Can and CanNode objects
             Return
          End If

          ' A property is missing in the config file. Print a warning.
          msg = "Warning: "
       Else
          ' For all other errors, print an error message and cancel the save.
          msg = "Error: "

          ' Cancel the load operation
          loader.Cancel()
       End If

       ' Print an informative message
       ' Example output:
       ' ERROR: Controller.Axis[2]: Axis is not enabled
       msg += e.PropertyPath + ": " + e.Message
       System.Console.WriteLine(msg)
    End Sub

    ' This method handles PropertyStatus events that occur while loading 
    ' configuration to an object.
    Public Sub OnLoadPropertyStatus(loader As Mpx.ConfigLoader, _
        e As Mpx.ConfigLoadPropertyStatusEventArgs)

        ' Print the current property and the percentage done
        ' Example output:
        ' 25%: Controller.Axis[2].AmpDisableAction
        ' Current Value: None
        ' New Value: CmdEqAct
        System.Console.WriteLine(e.PercentageDone + "%: " + e.PropertyPath)

        If e.NewValueText <> e.CurrentValueText Then
           ' The property value is going to change. Display the current 
           ' value and the new value.
           System.Console.WriteLine(" Current Value: " + e.CurrentValueText)

     System.Console.WriteLine("New Value: " + e.NewValueText)
  End If

    End Sub

    Public Sub DelagateExample()
        Dim loader As Mpx.ConfigLoader = New Mpx.ConfigLoader()

        Dim loaderPropertyErrorEvent As Mpx.ConfigLoader.PropertyErrorEventHandler 
        loaderPropertyErrorEvent = AddressOf OnLoadPropertyError
        AddHandler loader.PropertyErrorEvent, loaderPropertyErrorEvent

        Dim loaderPropertyStatusEvent As Mpx.ConfigLoader.PropertyStatusEventHandler        
        loaderPropertyStatusEvent = AddressOf OnLoadPropertyStatus
        AddHandler loader.PropertyStatusEvent, loaderPropertyStatusEvent

        Dim saver As Mpx.ConfigSaver = New Mpx.ConfigSaver()

        Dim saverPropertyErrorEvent As Mpx.ConfigSaver.PropertyErrorEventHandler 
        saverPropertyErrorEvent = AddressOf OnSavePropertyError
        AddHandler saver.PropertyErrorEvent, saverPropertyErrorEvent

        Dim saverPropertyStatusEvent As Mpx.ConfigSaver.PropertyStatusEventHandler 
        saverPropertyStatusEvent = AddressOf OnSavePropertyStatus
        AddHandler saver.PropertyStatusEvent, saverPropertyStatusEvent
    End Sub
End Class


C#

 

public class SerializationExamples
{
///////////////////////////////////////////////////////////
// Examples of saving and loading MPX object configuration
///////////////////////////////////////////////////////////

// Save an entire Axis configuration, including child objects.
public void SaveAxis()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.ConfigSaver saver = new Mpx.ConfigSaver();

   // Have the OnSavePropertyError method handle PropertyError events
   saver.PropertyErrorEvent += OnSavePropertyError;

   saver.SaveToFile(controller.Axis[0], "axisConfig.xml");
}

// Save Axis configuration, but exclude child objects.
public void SaveAxisOnly()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.Axis axis0 = controller.Axis[0];
   Mpx.ConfigSaver saver = new Mpx.ConfigSaver();

   // Don't save child objects
   saver.IncludeChildren = false;

   // Have the OnSavePropertyError method handle PropertyError events
   saver.PropertyErrorEvent += OnSavePropertyError;

   saver.SaveToFile(axis0, "axisOnlyConfig.xml");
}

// Save only the Controller configuration, excluding child objects.
public void SaveControllerOnly()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.ConfigSaver saver = new Mpx.ConfigSaver();

   // Don't save child objects
   saver.IncludeChildren = false;

   // Have the OnSavePropertyError method handle PropertyError events
   saver.PropertyErrorEvent += OnSavePropertyError;

   saver.SaveToFile(controller, "controllerOnlyConfig.xml");
}

// Save the entire Controller configuration, including child objects.
//
// Handle ConfigSavePropertyStatus events in order to display
// progress and exclude Can and CanNode objects from being saved.
//
// Handle PropertyError events in order to display errors.

public void SaveController()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.ConfigSaver saver = new Mpx.ConfigSaver();

   // Have the OnSavePropertyError method handle PropertyError events
   saver.PropertyErrorEvent += OnSavePropertyError;

   // Have the OnSavePropertyStatus method handle
   // ConfigSavePropertyStatus events

   saver.PropertyStatusEvent += OnSavePropertyStatus;

   saver.SaveToFile(controller, "controllerConfig.xml");
}

/////////////////////////////////////////////////////////////////////////
// Handlers for events that occur while saving MPX object configuration.
/////////////////////////////////////////////////////////////////////////

// This method handles PropertyError events that occur while saving
// the configuration of an object.

public void OnSavePropertyError(Mpx.ConfigSaver saver,
   Mpx.ConfigSavePropertyErrorEventArgs e)
{
   // Example output:
   // ERROR: Controller.Axis[2]: Axis is not enabled

   string msg = "ERROR: "
      + e.PropertyPath
      + ": "
      + e.Message
      + "\n";
   System.Console.Write(msg);

   if (e.ErrorCode == Mpx.ConfigSaveErrorCode.Internal)
   {
      // Cancel the save operation
      saver.Cancel();
      System.Console.Write("Canceled!\n");
   }
}

// This method handles PropertyStatus events that occur while loading
// configuration to object.

public void OnSavePropertyStatus(Mpx.ConfigSaver saver,
   Mpx.ConfigSavePropertyStatusEventArgs e)
{
   string msg;

   // Print the current property and the percentage done
   // Example output:
   // 25%: Controller.Axis[2].AmpDisableAction
   // skipped

   msg = e.PercentageDone
      + "%: "
      + e.PropertyPath
      + "\n";
   System.Console.Write(msg);

   // Don't save CAN or CAN Node objects
   if (e.PropertyPath.StartsWith("Controller.Can"))
   {
      System.Console.Write(" skipped\n");
      e.SkipProperty = true;
   }
}
///////////////////////////////

// Load axis configuration onto 2 different axes
public void LoadAxes()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.ConfigLoader loader = new Mpx.ConfigLoader();

   // Have the OnLoadPropertyError method handle PropertyError events
   loader.PropertyErrorEvent += OnLoadPropertyError;

   loader.LoadFromFile(controller.Axis[0], "axisOnlyConfig.xml");
   loader.LoadFromFile(controller.Axis[1], "axisOnlyConfig.xml");
}

// Load controller configuration.
// Handle PropertyError events in order to display errors and warnings.
// Handle PropertyStatus events in order to display progress.

public void LoadController()
{
   Mpx.Controller controller = new Mpx.Controller(0);
   Mpx.ConfigLoader loader = new Mpx.ConfigLoader();

   // Have the OnLoadPropertyError method handle PropertyError events
   loader.PropertyErrorEvent += OnLoadPropertyError;

   // Have the OnLoadPropertyStatus method handle PropertyStatus
   // events.

   loader.PropertyStatusEvent += OnLoadPropertyStatus;

   loader.LoadFromFile(controller, "controllerConfig.xml");
}

///////////////////////////////////////////////////////////
// Handlers for events that occur while loading MPX object
// configuration.
///////////////////////////////////////////////////////////

// This method handles PropertyError events that occur while loading
// configuration to an object.

public void OnLoadPropertyError(Mpx.ConfigLoader loader,
   Mpx.ConfigLoadPropertyErrorEventArgs e)
{
   string msg;

   if (e.ErrorCode == Mpx.ConfigLoadErrorCode.Missing)
   {
      if (e.PropertyPath.StartsWith("Controller.Can"))
      {
         // Ignore missing Can and CanNode objects
         return;
      }

      // A property is missing in the config file. Print a warning.
      msg = "Warning: ";
      }
   else
   {
      // For all other errors, print an error message and cancel the save.
      msg = "Error: ";

      // Cancel the load operation
      loader.Cancel();
   }

   // Print an informative message
   // Example output:
   // ERROR: Controller.Axis[2]: Axis is not enabled

   msg += e.PropertyPath
      + ": "
      + e.Message
      + "\n";
   System.Console.Write(msg);
   }

   // This method handles PropertyStatus events that occur while loading
   // configuration to an object.

   public void OnLoadPropertyStatus(Mpx.ConfigLoader loader,
      Mpx.ConfigLoadPropertyStatusEventArgs e)
   {
      string msg;

      // Print the current property and the percentage done
      // Example output:
      // 25%: Controller.Axis[2].AmpDisableAction
      // Current Value: None
      // New Value: CmdEqAct

      msg = e.PercentageDone
         + "%: "
         + e.PropertyPath
         + "\n";

      if (e.NewValueText != e.CurrentValueText)
      {
         // The property value is going to change. Display the current
         // value and the new value.

         msg += " Current Value: "
            + e.CurrentValueText
            + "\n New Value: "
            + e.NewValueText
            + "\n";
      }

      System.Console.Write(msg);
   }

   public void DelagateExample()
   {
      Mpx.ConfigLoader loader = new Mpx.ConfigLoader();

      Mpx.ConfigLoader.PropertyErrorEventHandler loaderPropertyErrorEvent;
      loaderPropertyErrorEvent = OnLoadPropertyError;
      loader.PropertyErrorEvent += loaderPropertyErrorEvent;

      Mpx.ConfigLoader.PropertyStatusEventHandler       loaderPropertyStatusEvent;
      loaderPropertyStatusEvent = OnLoadPropertyStatus;
      loader.PropertyStatusEvent += loaderPropertyStatusEvent;

      Mpx.ConfigSaver saver = new Mpx.ConfigSaver();

      Mpx.ConfigSaver.PropertyErrorEventHandler saverPropertyErrorEvent;
      saverPropertyErrorEvent = OnSavePropertyError;
      saver.PropertyErrorEvent += saverPropertyErrorEvent;

      Mpx.ConfigSaver.PropertyStatusEventHandler saverPropertyStatusEvent;
      saverPropertyStatusEvent = OnSavePropertyStatus;
      saver.PropertyStatusEvent += saverPropertyStatusEvent;
   }
}


 

       Legal Notice  |  Tech Email  |  Feedback
      
Copyright ©
2001-2021 Motion Engineering