`
elfasd
  • 浏览: 98341 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

utgard访问OPC server

阅读更多

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)

 

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的通讯,也是刚刚接触,希望有兴趣的一起探讨探讨。

 

 

 

 

 

  • 大小: 57.1 KB
2
0
分享到:
评论
9 楼 ron.luo 2016-11-22  
你好,报错如下,是什么原因?
org.openscada.opc.lib.da.Server - Failed to connect to server
org.jinterop.dcom.common.JIException: Unable to access Windows Registry, please check whether the SERVER service is running on the Target Workstation. [0x00001031]
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(Unknown Source) ~[org.openscada.jinterop.core_2.0.8.201303051454.jar:na]
at org.jinterop.dcom.core.JIProgId.getIdFromWinReg(Unknown Source) ~[org.openscada.jinterop.core_2.0.8.201303051454.jar:na]
at org.jinterop.dcom.core.JIProgId.getCorrespondingCLSID(Unknown Source) ~[org.openscada.jinterop.core_2.0.8.201303051454.jar:na]
at org.jinterop.dcom.core.JIComServer.<init>(Unknown Source) ~[org.openscada.jinterop.core_2.0.8.201303051454.jar:na]
at org.openscada.opc.lib.da.Server.connect(Server.java:123) ~[org.openscada.opc.lib_1.0.0.201303051455.jar:na]
at com.test.Test.main(Test.java:36) [classes/:na]
Caused by: java.net.MalformedURLException: For input string: "ronreypc9@127.0.0.1"


代码部分:
final ConnectionInformation ci = new ConnectionInformation ();
        ci.setHost("127.0.0.1");
        ci.setDomain("");
        ci.setProgId("Matrikon.OPC.Simulation.1");
//        ci.setProgId("ProcessIT.SimulationSvr"); //s7 注意 使用progId必须要求dcom配置正确
        //ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");//other
        //ci.setClsid("B6EACB30-42D5-11D0-9517-0020AFAA4B3C");// simatic S7
//        ci.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");// Ronrey Add
        ci.setUser("cluolin@xnmsn.cn");
        ci.setPassword("ronreypc9");

        // create a new server
        final Server server = new Server ( ci, Executors.newSingleThreadScheduledExecutor () );
8 楼 讨厌淡紫色 2016-04-26  
我这边总是提示org.openscada.opc.lib.da.Group.addItems报错,opc server已经连接上了,请问下是什么原因
7 楼 tandemon 2015-05-04  
你好,我用的是Matrikon OPC的server,用你的代码总是添加不进数据,请问是什么原因?
6 楼 LeoLi_1221 2014-11-18  
请教楼主!
// add sync access采用监听模式
final AccessBase access = new Async20Access(server, 6000, true);
for(final String itemId : items){
access.addItem(itemId, new DataCallback(){
public void changed(Item item, ItemState state){
System.out.println(item.getId()+"-----------11-------"+state);
}
});
}
采用订阅方式与OPC Server连接,为什么读取一段时间的数据后,会报如下的错误呢?

Recieved RESPONSE
WARN - JIComOxidRuntimeHelper$2$1.run(164) | JIComOxidRuntimeHelper RemUnknownThread (not listener)jI_RemUnknown[00000000-0000-0000-c000-000000000046 , L(55292):R(55293)] is purposefully closed by interruption.
java.nio.channels.ClosedByInterruptException
at java.nio.channels.spi.AbstractInterruptibleChannel.end(AbstractInterruptibleChannel.java:184)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:272)
at sun.nio.ch.SocketAdaptor$SocketInputStream.read(SocketAdaptor.java:176)
at sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:86)
at org.jinterop.dcom.transport.JIComRuntimeTransport.receive(JIComRuntimeTransport.java:147)
at rpc.DefaultConnection.receiveFragment(DefaultConnection.java:215)
at rpc.DefaultConnection.receive(DefaultConnection.java:87)
at rpc.ConnectionOrientedEndpoint.receive(ConnectionOrientedEndpoint.java:250)
at org.jinterop.dcom.transport.JIComRuntimeEndpoint.processRequests(JIComRuntimeEndpoint.java:94)
at org.jinterop.dcom.core.JIComOxidRuntimeHelper$2$1.run(JIComOxidRuntimeHelper.java:150)
at java.lang.Thread.run(Thread.java:662)

WARN - JIComOxidRuntimeHelper$1.run(92) | Oxid Resolver Thread/run
java.io.IOException: Socket Closed
at rpc.DefaultConnection.receiveFragment(DefaultConnection.java:393)
at rpc.DefaultConnection.receive(DefaultConnection.java:87)
at rpc.ConnectionOrientedEndpoint.receive(ConnectionOrientedEndpoint.java:250)
at org.jinterop.dcom.transport.JIComRuntimeEndpoint.processRequests(JIComRuntimeEndpoint.java:94)
at org.jinterop.dcom.core.JIComOxidRuntimeHelper$1.run(JIComOxidRuntimeHelper.java:88)
at java.lang.Thread.run(Thread.java:662)
5 楼 fanwisebrave 2014-06-06  
我连接的是ABB的DCS系统。谢谢你的解答。我按你的提示再试试。谢谢!
4 楼 elfasd 2014-05-12  
fanwisebrave 写道
信息: progIdVsClsidDB: {Word.Application=000209ff-0000-0000-c000-000000000046, TestCOM123.TestServer2=92a065a9-106a-4cc3-8d67-43e3a1e73df3, SYSINFO.SysInfo=6fba474b-43ac-11ce-9a0e-00aa0062bb4c, ADODB.Connection=00000514-0000-0010-8000-00aa006d2ea4, InternetExplorer.Application=0002df01-0000-0000-c000-000000000046, Excel.Application=00024500-0000-0000-c000-000000000046, StdCollection.VBCollection=4b738074-ea47-11d2-b25a-00105a022091, Shell.Application=13709620-c279-11ce-a49e-444553540000, Icecream.IceCreamOrder=fa11decf-7660-11d2-9c43-006008ad8bc0}
22:05:00.843 [main] INFO  org.openscada.opc.lib.da.Server - Failed to connect to server


我的总报没有联结到OPC服务器,请问需要注意哪些问题吗?

估计是没有给对clsid,先试试吧,或者把代码贴出来看看,截止到连接opcserver那就行
3 楼 elfasd 2014-05-12  
fanwisebrave 写道
我以前用的JeasyOPc ,现在准备用UTGARD,请问一下在本机联OPC ,也要配置DCOM吗?
我的没有调 通。
本机连接可以不用配置,但是不能是用Clsid,只能使用progID
如 ci.setProgId("OPC.SimaticNET");就不能用,必须使用Clsid方式://ci.setClsid("B6EACB30-42D5-11D0-9517-0020AFAA4B3C");// simatic S7
上面的clsid是SimaticNET的,不知道你用的是什么OPC SERVER
2 楼 fanwisebrave 2014-05-10  
信息: progIdVsClsidDB: {Word.Application=000209ff-0000-0000-c000-000000000046, TestCOM123.TestServer2=92a065a9-106a-4cc3-8d67-43e3a1e73df3, SYSINFO.SysInfo=6fba474b-43ac-11ce-9a0e-00aa0062bb4c, ADODB.Connection=00000514-0000-0010-8000-00aa006d2ea4, InternetExplorer.Application=0002df01-0000-0000-c000-000000000046, Excel.Application=00024500-0000-0000-c000-000000000046, StdCollection.VBCollection=4b738074-ea47-11d2-b25a-00105a022091, Shell.Application=13709620-c279-11ce-a49e-444553540000, Icecream.IceCreamOrder=fa11decf-7660-11d2-9c43-006008ad8bc0}
22:05:00.843 [main] INFO  org.openscada.opc.lib.da.Server - Failed to connect to server


我的总报没有联结到OPC服务器,请问需要注意哪些问题吗?
1 楼 fanwisebrave 2014-05-10  
我以前用的JeasyOPc ,现在准备用UTGARD,请问一下在本机联OPC ,也要配置DCOM吗?
我的没有调 通。

相关推荐

Global site tag (gtag.js) - Google Analytics