Accessing an OLE for Process Control (OPC) Server from Java |
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.
This example demonstrates how to talk to Matrikon OPC Simulation Server (version 1.2.4.1) from Java using J-Integra®.
This example assumes that you have installed
the JDK under C:\j2sdk1.4.2_01
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
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.
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.
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).
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); } } |
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
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)
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:
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.