Getting Started With CX-Compolet Using Visual Studio .NET

Introduction

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 (v1.3.1.2 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:

  • Connect to a device using CX-Compolet
  • Read a list of tags
  • Get the value from a selected tag
  • Write a value to a selected tag

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.

  • Integer
  • String (Text)
  • Floating Point (Real)

In CX-Programmer

     

Note that to use these symbols the key step is to define as a Network Variable, for example as Output or for Publication

 

In Sysmac Studio

 

Note that to use these variables the key step is to define Network Publish, for example as Output or for Publication

 

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:

Imports OMRON.Compolet.CIP

Inside the Form1_Load you must declare a new Object to work with.  Remember to use NJCompolet if you are using an NJ Controller

Private myCJ2 As CJ2Compolet = New CJ2Compolet

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:

______________________

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        myCJ2.UseRoutePath = False
        myCJ2.PeerAddress = "10.59.50.59"
        myCJ2.LocalPort = 2
        myCJ2.Active =
True

        Label2.Text = myCJ2.UnitName

    Catch ex As Exception
        Label2.Text = ex.Message
    End Try
End Sub

______________________
 

Note: Before running your project SYSMAC Gateway Runtime needs to be correctly configured and running. See How To Setup SYSMAC Gateway Runtime for details.

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):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim variableNames() As String

    variableNames = myCJ2.VariableNames()

     ListBox1.Items.Clear()

    For Each name As String In variableNames
        ListBox2.Items.Add(name)
    Next
End Sub

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:

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
      TextBox1.Text = CStr(myCJ2.ReadVariable(ListBox1.SelectedItem))
End Sub

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:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
     myCJ2.WriteVariable(ListBox1.SelectedItem, TextBox1.Text)
End Sub

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!

And Finally...

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:

 

Note that early versions of NJ Firmware may need to be updated for CX-Compolet to support use of network tags. Contact your local Sales Centre to help with this.

 


Link:
http://www.myomron.com/index.php?action=kb&print=1561



Comments  

Translate: