Java Connecting to OPC Server: NotConnectedException (AutoReconnectController)

919 views Asked by At

I run into an issue connecting to an OPC Server through Java with openScada, Utgard, and Jinterop.

I was previously using Matrikon's OPC Server (everything worked perfectly) and attempted to switch to 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. I completely uninstalled Kepware, and without changing any code I ran into "org.openscada.opc.lib.common.NotConnectedException" when running my program.

I have found a "workaround" for this issue. But it hinders the original functionality of the program: Originally a AutoReconnectController was used:

  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. So it's obvious the server wasn't connecting so I tried removing the AutoReconnectController.

When deleting AutoReconnectController, as such:

  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. 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; but again nothing has changed since Matrikon was changed to Kepware, and back again.

1

There are 1 answers

1
ctron On BEST ANSWER

The AutoReconnectController does trigger the connection process asynchronously. The Server class works synchronously. Since you are not synchronizing to the connection state, the state may, or may not be, "null".

However the "addListener" method of AutoReconnectController does allow you to add a listener on the connection state. Something like:

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