This document describes how to create a simple Windows Forms Application using some Open Core Interface functions in C# with Microsoft Visual Studio Express 2013. 1 Preconditions The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at: www.boschrexroth.com/oce The COM-DLL of the Open Core Interface SDK must be registered at the Windows Registry. Therefore you need to have administration privileges. Open the installation directory of the MLPI-SDK in your File-Explorer and move into the folder 01VRS / mlpi4com / bin. Execute the batch-file register.bat as administrator. A message window will report the successful registration. Page 1 of 20
2 Creating a Visual Studio Project Open Visual Studio and create a new Visual C# Project and choose Windows Forms Application. Page 2 of 20
Visual Studio will create a new Visual Studio solution with a default form. Now you have to add a reference to the Open Core Interface DLL. Therefore open the Reference Manager by choosing PROJECT / Add Reference The Open Core Interface Library will appear under its name Rexroth MotionLogicProgrammingInterface 1.0 Type Library. Mark this library. Page 3 of 20
After pressing OK you will find the library commlpilib in your project references. Hint: The abbreviation MLPI means Motion Logic Programming Interface and is the name of the implementation of an Open Core Interface at a MLC / XLC control unit. Page 4 of 20
3 Adding some UI Controls Add three Textboxes and Labels for: IP-Address, User-Name and Password and two Buttons for Connect and Disconnect. Add a Textbox and a Label for displaying the MLC Status message. Page 5 of 20
4 Adding C# Code for the UI Controls Open the source code of the default form ( Form1.cs ). Add the namespace commlpilib by adding the following line of code: using commlpilib; Declare a MLPI-Connection-Variable called mymlc: public MlpiConnection mymlc; Create a new MLPI-Connection by using its constructor: mymlc = new MlpiConnection( ); Page 6 of 20
Now we have to add some callback functions for the Connect and Disconnect buttons. Therefore double-click on the Button Connect and Visual Studio will automatically add a callback-function which looks like this: private void buttonname_click(object sender, EventArgs e) A text string will be used to handle IP-Adress, User-Name and Password for the connect-method of the MLPI-Connection-Object. Therefore we read the values from the UI controls ( Textboxes ). Page 7 of 20
Now we have to call the MLPI-method Connect by using a try-catch exception handler: Page 8 of 20
Page 9 of 20
The status message of the MLC Control is being read by calling the MLPImethod MLPIConnection.System.GetDisplayedDiagnosis( ). Write the returned text string into the UI text box. Page 10 of 20
Used C# Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; // Add Open Core Interface Library using commlpilib; namespace VS2013_WindowsFormsApplication_Demo public partial class Form1 : Form // declare a public MLPI connection variable public MlpiConnection mymlc; public Form1() InitializeComponent(); // create new MLPI connection instance mymlc = new MlpiConnection(); private void buttonconnect_click(object sender, EventArgs e) string strconnectionarguments, stripaddress, strusername, strpassword; stripaddress = textboxip.text; strusername = textboxuser.text; strpassword = textboxpassword.text; strconnectionarguments = stripaddress + " user=" + strusername + " password=" + strpassword; try mymlc.connect(strconnectionarguments); catch(systemexception ex) System.Console.WriteLine("Exception: " + ex.message); if (mymlc.isconnected == true) textboxstatusdisplay.text = mymlc.system.getdisplayeddiagnosis().text; else textboxstatusdisplay.text = "Connection couldn t be established"; Page 11 of 20
5 Access to symbol variables Add UI controls for selecting, reading and writing a symbol variable. We will use a ComboBox to show all available symbol variables. 5.1 Filling the ComboBox To read the list of all accessible symbol variables we use the MLPI Method MlpiConnection.Logic.Applications[ ].GetSymbolOfApplications( ). This method returns an array of strings which contains all symbolic variable names. This list will be copied to a ComboBox. Page 12 of 20
5.2 Reading variable values Add a callback method of the button. Values of symbol variables can be read by using the method MlpiConnection.Logic.ReadVariableBySymbolAsString( ). 5.3 Writing variable values Add a callback method of the button. Values of symbol variables can be written by using the method MlpiConnection.Logic.WriteVariableBySymbolAsString( ). Page 13 of 20
5.4 Testing the application To test this symbol variables access a Logic Application must run on the MLC / XLC control and the data access has to be enabled in the IndraWorks symbol configuration. In case of questions please have a look at the IndraWorks Online help and look for symbol configuration. After a successful connection the combo box will show all available symbol variables: The chosen variable will be read or written by pressing the related button. Page 14 of 20
Page 15 of 20
Example C# Code: // **************************************************************************** // C# DEMO Program for Open Core Interface of MLC / XLC // // This example shows how to connect to a MLC / XLC system by using the // Open Core Interface. Implemented functions are: // Connect and disconnect to MLC/XLC, read diagnosis text from MLC, // read list of available symbol variables, read and write symbol variables, // read/write access to MLC or Motion Axis Parameters (e. g. A 0 0100), // read/write access to axis interface in motion lib. // It does not show how to implemented cyclical parameter reading with an // asynchronous background thread. // **************************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; // Add Open Core Interface Library namespace using commlpilib; namespace VS2013_WindowsFormsApplication_Demo public partial class MainForm : Form const string strversion = "v0.1"; // Declare a public MLPI connection variable public MlpiConnection mymlc; public MainForm() InitializeComponent(); // Show app version labelversion.text = strversion; // Create new MLPI connection instance mymlc = new MlpiConnection(); private void buttonconnect_click(object sender, EventArgs e) string strconnectionarguments, stripaddress, strusername, strpassword; stripaddress = textboxip.text; strusername = textboxuser.text; strpassword = textboxpassword.text; strconnectionarguments = stripaddress + " user=" + strusername + " password=" + strpassword; // Clear the symbol variable combo box comboboxsymbolvar.items.clear(); try // Try to connect to MLC / XLC mymlc.connect(strconnectionarguments); catch(systemexception ex) System.Console.WriteLine("Exception: " + ex.message); Page 16 of 20
if (mymlc.isconnected == true) // Read MLC / XLC display diagnosis message textboxstatusdisplay.text = mymlc.system.getdisplayeddiagnosis().text; else textboxstatusdisplay.text = "Connection couldn t be established"; // Read name of MLCs PLC Application (Logic) string strnameofapplication = mymlc.logic.getnameofapplication(0); // Read complete list of symbol variable names string[] symbollist = (string[])mymlc.logic.applications[strnameofapplication].getsymbolsofapplication(); // Check if the list isn t empty if (symbollist!= null) foreach (string symbol in symbollist) // Write each symbol variable name into the ComboBox comboboxsymbolvar.items.add(symbol); private void buttonread_click(object sender, EventArgs e) // Read symbol variable value and put it into UI control if (comboboxsymbolvar.text!= "") textboxactualvalue.text = mymlc.logic.readvariablebysymbolasstring(comboboxsymbolvar.text); // Example how to read an axis parameter value directly // textboxactualvalue.text = mymlc.parameter.readdataasstring("a015:a 0 0100"); // Example how to read data from MLCs axis interface // textboxactualvalue.text = ( mymlc.motion.axes[15].getactualposition() ).ToString(); private void buttonwrite_click(object sender, EventArgs e) if (comboboxsymbolvar.text!= "") // Write symbol variable value mymlc.logic.writevariablebysymbolasstring(comboboxsymbolvar.text, textboxnewvalue.text); // Read the written value back into UI control textboxactualvalue.text = mymlc.logic.readvariablebysymbolasstring(comboboxsymbolvar.text); // Example how to write a value directly to an axis parameter (position limit) // mymlc.parameter.writedata(textboxnewvalue.text, "A015:A 0 0030"); // Example how to write directly to an axis interface element // double newvalue = Convert.ToDouble( textboxnewvalue.text ); // mymlc.motion.axes[15].setpositionlimitpos( newvalue ); private void buttondisconnect_click(object sender, EventArgs e) if (mymlc.isconnected == true) Page 17 of 20
mymlc.disconnect(); // Disconnect MLPI Connection textboxstatusdisplay.text = "No Connection!"; Page 18 of 20
6 Using of asynchronous background threads for cyclic polling of values To read a variable value cyclically it is necessary to use a background thread to avoid blocking the main application. An example could look like the following code: 6.1 Creating a background thread using System.Threading; // Create BackgroundWorker Thread, assign and start methods BackgroundWorker bgworkerpollstatus = new BackgroundWorker(); bgworkerpollstatus.dowork += new DoWorkEventHandler(bgWorkerPollStatus_DoWork); bgworkerpollstatus.runworkercompleted += new RunWorkerCompletedEventHandler(bgWorker_WorkCompleted); bgworkerpollstatus.progresschanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged); // Activate ProgressChanged Event Handler bgworkerpollstatus.workerreportsprogress = true; // Start polling... bgworkerpollstatus.dowork will be cyclically called bgworkerpollstatus.runworkerasync(); brunning = true; 6.2 Handler for cyclic DoWork-Event // ******************************************************************** // Asynchronous BackgroundWorker Thread for polling of status info **** // ******************************************************************** public void bgworkerpollstatus_dowork(object sender, DoWorkEventArgs e) int idummy; do if (mymlc.isconnected == true) try // Read diagnosis and variables, return values back to calling thread strdisplaytext = mymlc.system.getdisplayeddiagnosis().text; strplcvariable1 = mymlc.logic.readvariablebysymbol(strsymbolvar1name).tostring(); catch // Exception Handling ausführen ; (sender as BackgroundWorker).ReportProgress(iDummy); // triggers _ProgressChanged Thread.Sleep(100); // wait 100 milliseconds while (brunning == true); Page 19 of 20
6.3 Handler for ProgressChanged-Event // ******************************************************************** // *** CHANGED Callback for asynchronous Background Thread ************ // ******************************************************************** private void bgworker_progresschanged(object sender, ProgressChangedEventArgs e) // This code will be called each time when method.reportprogress( ) of // bgworkerpollstatus_dowork is called textboxdisplay.text = strdisplaytext; textboxplcvar1.text = strplcvariable1; textboxplcvar2.text = strplcvariable2; // Update Textbox UI Controls 6.4 Handler for WorkCompleted-Event // ******************************************************************** // *** COMPLETED Callback for asynchronous Background Thread ********** // ******************************************************************** private void bgworker_workcompleted(object sender, RunWorkerCompletedEventArgs e) // This code will be called once when bgworkerpollstatus_dowork will be exited textboxdisplay.text = strdisplaytext; // update Textbox UI Control Page 20 of 20