懒羊羊
2023-08-30 71e81ed1d12e4d69f53c8ad9e066650ad4186293
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
    }
 
 
}