简体   繁体   English

Java连接到OPC服务器:NotConnectedException(AutoReconnectController)

[英]Java Connecting to OPC Server: NotConnectedException (AutoReconnectController)

I run into an issue connecting to an OPC Server through Java with openScada, Utgard, and Jinterop. 我遇到一个问题,该问题是通过Java使用openScada,Utgard和Jinterop通过Java连接到OPC服务器的。

I was previously using Matrikon's OPC Server (everything worked perfectly) and attempted to switch to Kepware Server Ex. 我以前使用的是Matrikon的OPC服务器(一切运行正常),并试图切换到Kepware Server Ex。 The Kepware demo ran out and I didn't want to purchase it - so I decided to switch back to Matrikon's OPC Server. Kepware演示用完了,我不想购买它-因此,我决定切换回Matrikon的OPC服务器。 I completely uninstalled Kepware, and without changing any code I ran into "org.openscada.opc.lib.common.NotConnectedException" when running my program. 我完全卸载了Kepware,并且在不更改任何代码的情况下,运行程序时遇到了“ org.openscada.opc.lib.common.NotConnectedException”。

I have found a "workaround" for this issue. 我已经找到了解决该问题的方法。 But it hinders the original functionality of the program: Originally a AutoReconnectController was used: 但这阻碍了程序的原始功能: 最初使用的是AutoReconnectController:

  public void createOPCConnection( String host, String domain, String progID, String clsid, List<String>tagNames) throws OPCException {   

  this.conn = new ConnectionInformation();
    conn.setHost(this.host);
    conn.setDomain(this.domain);
    conn.setUser(this.user);
    conn.setPassword(this.pass);
    conn.setProgId(this.progID);
    conn.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");


    server = new Server(conn, Executors.newSingleThreadScheduledExecutor());        
    AutoReconnectController autoReconnectController = new AutoReconnectController ( server );
    // disable GC for COM objects to prevent the socket from being closed
    JISystem.setJavaCoClassAutoCollection(false);

    try {
        // connect to server
        autoReconnectController.connect();
        createOPCGroup(tagNames);

    } catch (IllegalArgumentException e) {
        throw new OPCException(e.getMessage());

    }

When testing the server state using 使用以下命令测试服务器状态时

  server.getServerState() 

I get a value of Null. 我得到的值为Null。 So it's obvious the server wasn't connecting so I tried removing the AutoReconnectController. 因此很明显服务器未连接,因此我尝试删除了AutoReconnectController。

When deleting AutoReconnectController, as such: 删除AutoReconnectController时,如下所示:

  server = new Server(conn, Executors.newSingleThreadScheduledExecutor());
   try{
        server.connect();
        createOPCGroup(tagNames);
    }catch (Exception e){
    e.printStackTrace(System.out);
    }

The NotConnectedException does not trigger, and the program runs successfully. NotConnectedException不会触发,并且程序成功运行。 However, I need the reconnect functionality. 但是,我需要重新连接功能。 Does anyone have any thoughts as to what might be going on here? 是否有人对这里可能发生的事情有任何想法?

Since no code was changed I figured it was DCOM settings or something of the like; 由于没有更改任何代码,因此我认为这是DCOM设置或类似的设置; but again nothing has changed since Matrikon was changed to Kepware, and back again. 但是自从Matrikon更改为Kepware之后,一切都没有改变,然后又回来了。

The AutoReconnectController does trigger the connection process asynchronously. AutoReconnectController确实会异步触发连接过程。 The Server class works synchronously. Server类同步工作。 Since you are not synchronizing to the connection state, the state may, or may not be, "null". 由于您没有同步到连接状态,因此该状态可能为“ null”,也可能不是“ null”。

However the "addListener" method of AutoReconnectController does allow you to add a listener on the connection state. 但是,AutoReconnectController的“ addListener”方法确实允许您在连接状态上添加侦听器。 Something like: 就像是:

autoReconnectController.addListener ( new AutoReconnectListener () {
   public void stateChanged ( AutoReconnectState state ) {
       if ( state == AutoReconnectState.CONNECTED ) {
           createOPCGroup(tagNames);
       }
   }
} );
autoReconnectController.connect ();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM