Getting Started With CX-Compolet Using Visual Studio .NET
CX-Compolet provides a large number of Classes and functionality to communicate with Omron devices. At first it can seem a little daunting. However, especially since greater use of EIP, it is now easy to get started using CX-Compolet in your programs. All you need is The latest CX-Compolet and Sysmac Gateway installed (v22.214.171.124 or later) and a copy of Visual Studio - Express Edition is free from Microsoft (www.microsoft.com/visualstudio/eng/products/visual-studio-express-products) and is enough to get you started.
This article will step through creating your first VB.NET example communicating directly with an EIP Controller. The article uses VB.NET Express 2012 but you may prefer to use another language if you are more comfortable - the CX-Compolet classes are the same regardless!
After completing this tutorial you will be able to:
Previous experience on CX-Programmer or Sysmac Studio is expected, knowledge of Visual Studio is also advisable. This article covers both NJ and CJ Controllers (The syntax for NJCompolet and CJCompolet is the same for basic device connection)
Setting Up The Tags
Create 3 output tags in Sysmac Studio or CX-Programmer.
In Sysmac Studio
Creating A New VB.NET Project
We can now get started with VB.NET. Create a new project and give it a name:
The first time you use CX-Compolet with Visual Studio, you'll need to add the CX-Compolet .NET Controls to the Toolbox, right click the toolbox and click 'Choose Items...' Then select the Compolet Controls you want (in this Tutorial you'll need CJ2Compolet or NJCompolet)
Adding and Configuring the Control
Now you can double click the control in the toolbox to add it into your form. If you are using an NJ Controller, you will need to use NJCompolet, if you are using CJ2 Controller, you'll need CJ2Compolet. Also, for this example, add a Label which will contain the controller name or an error message when we initialise.
You must now configure the control, this is the same regardless of controller type used.
Double click the title bar of the form and this will start you editing the function that is run when you first open the form.
At the top of the code you need to add the following:
Inside the Form1_Load you must declare a new Object to work with. Remember to use NJCompolet if you are using an NJ Controller
Now we can initialise the connection to the device. This needs 4 things to be configured
UseRoutePath - Used when gateways are needed to route to a Contoller - will normally be false for a simple setup
PeerAddress - The IP address of the Controller you wish to connect with.
LocalPort - This is the communication port in Sysmac Gateway - for Ethernet this will be 2 - you can see in Sysmac Gateway Console.
Active - This causes CX-Compolet to connect to the device and have an active connection, this must be done before any communications with the device can occur.
With all .NET applications, you must program using Try and Catch blocks to ensure that any problems that occur are handled correctly by your application. In this example we will set the contents of a Label to indicate the result of connection, at other times you should use Try Catch blocks around all Controller access although this example does not to make the code smaller for the example.
Your Form1_Load should now look like:
Try running your application by pressing the Green 'Start' button at the top of the screen. You will see that the Label is populated with the name of your controller - or an error message. In case of problems, check the Ethernet connection to the controller, can you 'ping' it? Also double check the settings for the Ethernet port in Sysmac Gateway console - is it Open and using the correct Ethernet adaptor in your PC with the correct IP address?
Getting List of Tags and Reading Values
Add a ListBox, a textbox and a Button control to your form. We will now make the button populate the list box with the tags you created on the controller. We'll also make the Listbox populate the Textbox with the value of the tag on the controller. Your form should now look like this:
You can populate the list box by adding the following code to your button (double click object to add code):
Note the use of myCJ2.VariableNames to get the list of network tags directly from the Controller.
Double clicking on the list box allows you to add code when an item is selected in the list box.
You can now add code to read the currently selected Tag:
You can change the value on the controller using Sysmac Studio or CX-Programmer and see this new value reflected in your new CX-Compolet application.
For more complex types, structures or arrays you may need to perform more complex conversion to work in your application.
Hint: You can add a ReadVariable to a timer in VB to get a regular update of a value. CX-Compolet even lets you get multiple values in one request.
Writing a value to the Controller
You can write a value to the controller using the WriteValue method:
This code will write the value in the text box to the currently selected item in the list box.
Note there are no checks here on data type of validity of the value, your application must ensure that the correct value is written as you expect!
This has been a simple introduction the basic reading and writing of named tags using CX-Compolet. CX-Compolet has many features which you can explore. This was just a taster to get you started. Visual Studio shows you available methods as you type and you can look at the different controls offered in the toolbox. CX-Compolet also installs online help. You can access the help for CJCompolet and NJCompolet components by opening the SYSMAC Compolet Help from the start menu - installed under CX-Compolet: