utgard是一个开源的项目,基于j-interop做的,用于和OPC SERVER通讯。
j-interop是纯java封装的用于COM/DCOM通讯的开源项目,这样就不必使用JNI
首先说下环境:
我的电脑是WIN7 64操作系统
eclipse3.3
OPC SERVER采用 samtatic net
本文目的:通过utgard与OPC SERVER通讯,从而达到控制PLC的功能。
下面是程序配置:
1、导入jar包
2、测试main程序(附件中的Test.java)
?
class="java" name="code">import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; import org.jinterop.dcom.common.JIException; import org.jinterop.dcom.core.JIVariant; import org.openscada.opc.lib.common.ConnectionInformation; import org.openscada.opc.lib.da.Group; import org.openscada.opc.lib.da.Item; import org.openscada.opc.lib.da.ItemState; import org.openscada.opc.lib.da.Server; import org.openscada.opc.lib.da.browser.Branch; import org.openscada.opc.lib.da.browser.Leaf; public class Test { public static void main ( final String[] args ) throws Throwable { // create connection information final ConnectionInformation ci = new ConnectionInformation (); ci.setHost("127.0.0.1"); ci.setDomain(""); // ci.setProgId("Matrikon.OPC.Simulation.1"); ci.setProgId("OPC.SimaticNET"); //s7 注意 使用progId必须要求dcom配置正确 //ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");//other //ci.setClsid("B6EACB30-42D5-11D0-9517-0020AFAA4B3C");// simatic S7 ci.setUser("Administrator"); ci.setPassword("asd3839729"); // create a new server final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () ); try { server.connect (); Group group = server.addGroup ( "group" ); // group is initially active ... just for demonstration group.setActive ( true ); // Add a new item to the group final Item item = group.addItem ( "S7:[@LOCALSERVER]DB1,W0" ); String[] test = new String[]{"S7:[@LOCALSERVER]DB1,W2","S7:[@LOCALSERVER]DB1,W1"}; Map<String,Item> map = new HashMap<String,Item>(); map = group.addItems(test); System.out.println(map.keySet()); for(String s : map.keySet()){ Item it = map.get(s); System.out.println(s + " =============== " + it.read(true).getValue().getObjectAsUnsigned().getValue()); } // Items are initially active ... just for demonstration item.setActive ( true ); item.write(new JIVariant("121")); System.out.println(item.read(true).getValue().getObjectAsUnsigned().getValue() + " <------=="); // Add some more items ... including one that is already existing // final Map<String, Item> items = group.addItems ( "Saw-toothed Waves.Int2", "Saw-toothed Waves.Int4" ); } catch ( final JIException e ) { System.out.println ( String.format ( "%08X: %s", e.getErrorCode (), server.getErrorMessage ( e.getErrorCode () ) ) ); } } }
?
?
注:
a:诸如S7:[@LOCALSERVER]DB1,W0是我通过在本地OPC SERVER中定义的PLC地址,通过OPC SCOUT可以定义、查看
b:ProgId 其实就是注册表中的名称,对应的值就是Clsid 这些可以在注册表中找到,这里我的progid是OPC.SimaticNET,就是通过这个名称找到了它的值B6EACB30-42D5-11D0-9517-0020AFAA4B3C
需要注意的是,如果使用OPC.SimaticNet,你电脑的OPC SERVER的DCOM必须配置成功,否则连接不上,也或者直接用 setClsid("B6EACB30-42D5-11D0-9517-0020AFAA4B3C"),效果一样,WIN7 64位设置OPC SERVER DCOM与XP不太一样,附件? 《Win7和Win7_SP1网络OPC配置.pdf》是详细配置步骤。
?
最近在研究J-INTEROP与DCOM的通讯,也是刚刚接触,希望有兴趣的一起探讨探讨。
?
?
?
?
?
网友 2014/9/23 11:27:12 发表
最近用utgard连不上matrikon希望出个手把手图文教程 谢谢