Accessing an OLE for Process Control (OPC) Server from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access an OLE for Process Control (OPC) Server from Java. J-Integra® for COM is a Java interoperability component that bridges Java and OPC. It provides bi-directional access of Java objects and COM components.

Contents

  1. Introduction
  2. Configure Your Environment
  3. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
    4. Notes
  4. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc
  5. More about OPC Programming

1 Introduction

This example demonstrates how to talk to Matrikon OPC Simulation Server (version 1.2.4.1) from Java using J-Integra®.

2 Configure your environment

This example assumes that you have installed

We will be performing this example under C:\pure. Create that directory, and an opcauto directory under it. Update your path environment variable to include the JDK and J-Integra® bin directories, and update your CLASSPATH environment variable to include the J-Integra® runtime:

C:\>mkdir pure
C:\>cd pure
C:\pure>mkdir opcauto
C:\pure>set PATH=C:\jintegra\bin;C:\j2sdk1.4.2_01\bin;C:\j2sdk1.4.2_01\jre\bin;
C:\pure>set CLASSPATH=.;C:\jintegra\lib\jintegra.jar

Accessing an OLE for Process Control (OPC) Server from Java: Set environment variables

3 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access OPC server from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access OPC server on another Windows machine.

3.1 Generate the Java proxies for Matrikon OPC Server

Type Libraries contain information about COM Components. We will be using the information contained in the OPC Server's type library to generate pure Java proxies which can be used to access the OPC Server from a standard JVM.

  1. Start the com2java tool:

    Accessing an OLE for Process Control (OPC) Server from Java: Start com2java tool

    If you get an error about the command not being found, check that you set your path environment variable correctly above.
     
  2. Click Options... to check the options and click OK..

    Accessing an OLE for Process Control (OPC) Server from Java: Check com2java options
  3. Click Select... to select C:\Program Files\Common Files\MatrikonOPC\Common\OPCAuto.dll  (Note: select C:\Program Files\Matrikon\ProcessX\Common\PSTAuto.dll if you are using the old version of ProcessX OPC Simulation Server.)
  4. Enter c:\pure into the Output Dir field
  5. Enter opcauto into the Java Package field
  6. Click on Generate Proxies. The proxy files should be generated:

    Take a look at some of the generated files to get a feel for what is being generated.

    You will notice that comments are included in the generated files in order to make them easier to understand. You may wish to use the javadoc documentation generation tool on the files (the comments are compatible with that tool).
    Accessing an OLE for Process Control (OPC) Server from Java: Generate proxies

3.2 Create the Example

Create the file c:\pure\OpcAutoClient.java, by cutting and pasting from your Web browser.

public class OpcAutoClient { 
 
  public static void main(String[] args) throws Exception { 

    try {
      com.linar.jintegra.Log.logImmediately(3, "jintegra.log");

      // DCOM authentication: Make sure Nt Domain, Nt User, Nt PassOPC are valid credentials.
      // Uncomment this line if OpcAutoClient.java remotely accesses OPC server:
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSOPC"); 

      // Specify host name or IP address of OPC server machine as parameter if 
      // OpcAutoClient.java remotely accesses OPC server. 
      // opcauto.OPCServer server = new opcauto.OPCServer("123.456.789.0"); 
      opcauto.OPCServer server = new opcauto.OPCServer(); 
 
      System.out.println("These servers are accessible:"); 
      String[] servers = (String[]) server.getOPCServers(null); 
      for (int i = 0; i < servers.length; i++) { 
        System.out.println("  " + servers[i]); 
      } 

      // Use this line if your OPC server is an older version of ProcessX OPC Simulation Server:
      // String programID = "ProcessX.OPC.Simulation.1";
      String programID = "Matrikon.OPC.Simulation.1";
      System.out.println("Connecting to " + programID);
      server.connect(programID, null);

      System.out.println("  Server Version: " + server.getMinorVersion() + "." + 
                         server.getMinorVersion()); 
      System.out.println("  Vendor information: \"" + server.getVendorInfo() + "\""); 
 
      // Browse to list groups/tags available 
      System.out.println("Browsing ..."); 
      opcauto.OPCBrowser browser = server.createBrowser(); 
      browse(browser, 1); // Recursive method call, see below 
 
      // Add a group 
      System.out.println("Adding a group ..."); 
      opcauto.OPCGroups groups = server.getOPCGroups(); 
      opcauto.OPCGroup group = groups.add("one"); 
      group.setUpdateRate(1000); 
      group.setDeadBand(10.0F); 
      group.setIsActive(true); 
 
      // Add an item -- one of the predefined items 
      System.out.println("Adding an item ..."); 
      opcauto.OPCItems items = group.getOPCItems(); 
      opcauto.OPCItem item = items.addItem("Random.String", 1); 
      item.setIsActive(true); 
      int itemsCount = items.getCount();
      System.out.println("Number of existing OPCItems: " + itemsCount); 
      for (int i = 1; i <= itemsCount; i++) { 
        System.out.println(" " + items.item(new Integer(i)).getItemID());
      } 

      // Add an event listener.  Notice the standard Java idiom.  GroupListener 
      // is defined below. 
      System.out.println("Subscribing to events ..."); 
      GroupListener listener = new GroupListener(); 
      group.addDIOPCGroupEventListener(listener); 
      group.setIsSubscribed(true); 
 
      // Sleep for 15 seconds, to allow time for some events to be received 
      Thread.sleep(15000); 
 
      // Tidy up 
      System.out.println("Unsubscribing ..."); 
      group.setIsSubscribed(false); 
      group.removeDIOPCGroupEventListener(listener); 
 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      // Release COM object references that have not been released through GC 
      System.out.println("Releasing remaining COM object references ..."); 
      com.linar.jintegra.Cleaner.releaseAll(); 
    } 
    System.out.println("Exiting JVM ..."); 
  } 
  
  // Recursive method used to browse the server tag space 
  private static void browse 
    (opcauto.OPCBrowser browser, int depth) throws java.io.IOException { 
    // Display all leafs 
    browser.showLeafs(new Boolean(false)); // only current position 
    int count = browser.getCount(); 
    // Yes - J-Integra® maps COM _Enums to Java Enumerations! 
    java.util.Enumeration enumBrowser = browser.get_NewEnum(); 
    for (int i = 0; i < count; i++) { 
      for (int s = 0; s < depth; s++) System.out.print("  "); 
      System.out.println("Item: " + enumBrowser.nextElement()); 
    } 
 
    // Get all branch names 
    browser.showBranches(); 
    count = browser.getCount(); 
    String groupNames[] = new String[count]; 
    for (int i = 1; i <= count; i++) { 
      groupNames[i - 1] = browser.item(new Integer(i)); 
    } 
 
    // Display and browse each branch 
    for (int i = 0; i < count; i++) { 
      for (int s = 0; s < depth; s++) System.out.print("  "); 
      System.out.println("Branch: " + groupNames[i]); 
      browser.moveDown(groupNames[i]); 
      browse(browser, depth + 1); 
      browser.moveUp(); 
    } 
  } 
} 
 
// Event listener.  J-Integra® generates event adapters to make life easier 
 
class GroupListener extends opcauto.DIOPCGroupEventAdapter { 
  public void dataChange(opcauto.DIOPCGroupEventDataChangeEvent theEvent) { 
    String eventString = "  Data change event. TxId=" + theEvent.getTransactionID(); 
    for (int i = 0; i < theEvent.getItemValues().length; i++) { 
      eventString += " { '" + theEvent.getItemValues()[i] + "' at " + 
        theEvent.getTimeStamps()[i] + " } "; 
    } 
    System.out.println(eventString); 
  } 
}

3.3 Compile and Run the Example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing OPC server):

javac OpcAutoClient.java
java -DJINTEGRA_NATIVE_MODE OpcAutoClient

Accessing an OLE for Process Control (OPC) Server from Java: Run example

If you get a The name specified is not recognized as an internal or external command ... error, then it is likely that you did not set you path environment variable correctly (see the first section of this example)

3.4 Notes

4 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from OPC server, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use setdllhost to configure a surrogate for OPC server on the Windows machine:
    C:\Program Files\Matrikon\OPC\Common>setdllhost OPCAuto.dll "Matrikon OPCAuto.dll"
  4. Use DCOMCNFG to configure OPC server on the Windows machine.
  5. Pass the IP address or computer name of the OPC server machine to the constructor of OPCServer object:
    opcauto.OPCServer server = new opcauto.OPCServer("123.456.789.0");
  6. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault(domain, adminUser, adminPassOPC);
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  7. Move OpcAutoClient.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java OpcAutoClient

5 More about OPC programming

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about Matrikon's OPC server API, please refer to Matrikon's website. The quickest way to start programming Java-to-OPC applications is to find a VB example first, and then map the VB code to Java.