cl
2024-01-23 ff13e6159eab48d53bb528a10d0ab8850fdf7ad5
提交 | 用户 | 时间
71e81e 1 package cn.stylefeng.guns.plcserver;
2
3
4 import org.jinterop.dcom.common.JIErrorCodes;
5 import org.jinterop.dcom.common.JIException;
6 import org.jinterop.dcom.core.JIVariant;
7 import org.openscada.opc.lib.common.ConnectionInformation;
8 import org.openscada.opc.lib.da.Group;
9 import org.openscada.opc.lib.da.Item;
10 import org.openscada.opc.lib.da.Server;
11
12 public class test {
13
14
15     private static String HOST = "192.168.0.234";
16     //private static String HOST = "192.168.1.100";
17     private static String USER = "OPCUser";
18     private static String PASSWORD = "123456";
19     private static String CLSID= "7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729";
20     private static String PROGID = "";
21
22
23         public static void main(String[] args) throws Exception {
24             // create connection information
25             final ConnectionInformation ci = new ConnectionInformation();
26             ci.setHost("127.0.0.1");
27             ci.setDomain("");
28             ci.setUser("OPCServer");
29             ci.setPassword("admin@123");
30             ci.setClsid("7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729");
31             //ci.setProgId("7BC0CC8E");
32             // ci.setClsid("680DFBF7-C92D-484D-84BE-06DC3DECCD68"); // if ProgId is not working, try it using the Clsid instead
33             final String itemId = "_System._Time_Second";
34             // create a new server
35             Item item = null;
36             Server server = new Server(ci, null);
37             //server = new Server(ci, Executors.newSingleThreadScheduledExecutor());
38
39             try {
40                 // connect to server
41                 server.connect();
42                 while(true) {
43
44                     System.out.println("111111==");
45                     Thread.sleep(1000);
46                 }
47
48             } catch (final JIException e) {
49                 System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode())));
50             }
51         }
52     /**
53      * 获取value
54      * @param var
55      * @return
56      * @throws JIException
57      */
58     private static Object getVal(JIVariant var) throws JIException {
59         Object value;
60         int type = var.getType();
61         switch (type) {
62             case JIVariant.VT_I2:
63                 value = var.getObjectAsShort();
64                 break;
65             case JIVariant.VT_I4:
66                 value = var.getObjectAsInt();
67                 break;
68             case JIVariant.VT_I8:
69                 value = var.getObjectAsLong();
70                 break;
71             case JIVariant.VT_R4:
72                 value = var.getObjectAsFloat();
73                 break;
74             case JIVariant.VT_R8:
75                 value = var.getObjectAsDouble();
76                 break;
77             case JIVariant.VT_BSTR:
78                 value = var.getObjectAsString2();
79                 break;
80             case JIVariant.VT_BOOL:
81                 value = var.getObjectAsBoolean();
82                 break;
83             case JIVariant.VT_UI2:
84             case JIVariant.VT_UI4:
85                 value = var.getObjectAsUnsigned().getValue();
86                 break;
87             case JIVariant.VT_EMPTY:
88                 throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is Empty.");
89             case JIVariant.VT_NULL:
90                 throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Variant is null.");
91             default:
92                 throw new JIException(JIErrorCodes.JI_VARIANT_IS_NULL, "Unknown Type.");
93         }
94
95         return value;
96     }
97
98
99 }