package cn.stylefeng.guns.plcserver; import org.jinterop.dcom.common.JIErrorCodes; 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.Server; public class test { private static String HOST = "192.168.0.234"; //private static String HOST = "192.168.1.100"; private static String USER = "OPCUser"; private static String PASSWORD = "123456"; private static String CLSID= "7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"; private static String PROGID = ""; public static void main(String[] args) throws Exception { // create connection information final ConnectionInformation ci = new ConnectionInformation(); ci.setHost("127.0.0.1"); ci.setDomain(""); ci.setUser("OPCServer"); ci.setPassword("admin@123"); ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729"); //ci.setProgId("7BC0CC8E"); // ci.setClsid("680DFBF7-C92D-484D-84BE-06DC3DECCD68"); // if ProgId is not working, try it using the Clsid instead final String itemId = "_System._Time_Second"; // create a new server Item item = null; Server server = new Server(ci, null); //server = new Server(ci, Executors.newSingleThreadScheduledExecutor()); try { // connect to server server.connect(); while(true) { System.out.println("111111=="); Thread.sleep(1000); } } catch (final JIException e) { System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode()))); } } /** * 获取value * @param var * @return * @throws JIException */ private static Object getVal(JIVariant var) throws JIException { Object value; int type = var.getType(); switch (type) { case JIVariant.VT_I2: value = var.getObjectAsShort(); break; case JIVariant.VT_I4: value = var.getObjectAsInt(); break; case JIVariant.VT_I8: value = var.getObjectAsLong(); break; case JIVariant.VT_R4: value = var.getObjectAsFloat(); break; case JIVariant.VT_R8: value = var.getObjectAsDouble(); break; case JIVariant.VT_BSTR: value = var.getObjectAsString2(); break; case JIVariant.VT_BOOL: value = var.getObjectAsBoolean(); break; case JIVariant.VT_UI2: case JIVariant.VT_UI4: value = var.getObjectAsUnsigned().getValue(); break; case JIVariant.VT_EMPTY: throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is Empty."); case JIVariant.VT_NULL: throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is null."); default: throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Unknown Type."); } return value; } }