0

I have this code which is to read values from the kepware OPC Server

package opcread;
import java.util.concurrent.Executors;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.AccessBase;
import org.openscada.opc.lib.da.DataCallback;
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.SyncAccess;

public class OPCRead {


    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        final ConnectionInformation ci = new ConnectionInformation();
        ci.setHost("localhost");
        ci.setDomain("MYDOMAIN");
        ci.setUser("MY_COMPUTER_USERNAME");
        ci.setPassword("MY_COMPUTER_PASSWORD");
        ci.setProgId("Kepware.KEPServerEX.V5\\MP.ANC1._System._Mode");
        ci.setClsid("B3AF0BF6-4C0C-4804-A122-6F3B160F4397");
        final String itemId = "_System._Time_Second";

        final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor());

        try
        {
            server.connect();

            final AccessBase access = new SyncAccess(server, 500);
            access.addItem(itemId, new DataCallback(){
                @Override
                public void changed(Item item, ItemState state){
                    System.out.println("Data change " + item + " : " + state);
                }

            });

            access.bind();

            Thread.sleep(10*1000);

            access.unbind();
        }
        catch( final JIException e)
        {
            System.out.println("Errorrrrrrrr : " + String.format("%08X: %s", e.getErrorCode(),server.getErrorMessage(e.getErrorCode())));
        }
        catch(Exception ex)
        {
            System.out.println("Errorrrrrrrr : " + ex.getMessage());
        }
    }

}

I want to read value present on _Mode tag. I have also given the full path above Kepware.KEPServerEX.V5\\MP.ANC1._System._Mode. But in result, it doesnt shows the values present on the tag i.e. userRate, rather it is giving the following message

Sep 12, 2015 9:10:57 PM rpc.DefaultConnection processOutgoing
INFO: 
 Sending REQUEST
Sep 12, 2015 9:10:57 PM rpc.DefaultConnection processIncoming
INFO: 
 Recieved RESPONSE
Data change org.openscada.opc.lib.da.Item@11d7dda : Value: [[org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e]], Timestamp: Sat Sep 12 21:10:57 IST 2015, Quality: 192, ErrorCode: 00000000

In place of org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e, it should print the value, but getting this message. What wrong I am doing here ?

John Tracid
  • 3,836
  • 3
  • 22
  • 33
Harshit
  • 5,147
  • 9
  • 46
  • 93

1 Answers1

1

You need to get value from variant explicitly, like this:

access.addItem(itemId, new DataCallback() {
    @Override
    public void changed(Item item, ItemState state) {
        System.out.println("Data change " + state.getObjectAsUnsigned().getValue() + " : " + state);
    }
});
John Tracid
  • 3,836
  • 3
  • 22
  • 33
  • I am getting error when using `state.getObjectAsUnsigned().getValue()` but not getting error when using `state.getValue().getObjectAsUnsigned()` . Now also getting response as `Data change org.openscada.opc.lib.da.Item@11d7dda : Value: org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e` – Harshit Sep 14 '15 at 03:34
  • After change to `state.getValue().getObjectAsUnsigned().getValue().toString()` I am not getiing `org.jinterop.dcom.core.JIUnsignedInteger@11d4b2e` but instead I am getting numbers like 23,24,25. But the `Kepware.KEPServerEX.V5\\MP.ANC1._System._Mode` contains the string value. I was expecting that. Why I am getting numbers ? – Harshit Sep 14 '15 at 03:44
  • You getting numbers because you use `addItem` with item ID `_System._Time_Second`. So you get seconds. – John Tracid Sep 14 '15 at 07:06