坑逼opc
<dependency> <groupId>org.openscada.external</groupId> <artifactId>org.openscada.external.jcifs</artifactId> <version>1.2.25</version> </dependency> <dependency> <groupId>org.openscada.jinterop</groupId> <artifactId>org.openscada.jinterop.core</artifactId> <version>2.1.8</version> </dependency> <dependency> <groupId>org.openscada.jinterop</groupId> <artifactId>org.openscada.jinterop.deps</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.openscada.utgard</groupId> <artifactId>org.openscada.opc.dcom</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.openscada.utgard</groupId> <artifactId>org.openscada.opc.lib</artifactId> <version>1.5.0</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.59</version> </dependency>
public static void main(String[] args) throws Exception{ final ConnectionInformation ci = new ConnectionInformation(); ci.setHost("localhost"); ci.setUser("Administrator"); ci.setPassword("gelin"); ci.setProgId("Matrikon.OPC.Simulation.1"); final String itemId = "Bucket Brigade.Int4"; // create a new server final Server server = new Server(ci, Executors.newSingleThreadScheduledExecutor()); try { // connect to server server.connect(); System.out.println("连接成功"); final AccessBase access = new SyncAccess(server, 500); access.addItem(itemId, new DataCallback() { @Override public void changed(Item item, ItemState state) { // also dump value try { if (state.getValue().getType() == JIVariant.VT_UI4) { System.out.println("<<< " + state + " / value = " + state.getValue().getObjectAsUnsigned().getValue()); } else { System.out.println("<<< " + state + " / ------value = " + state.getValue().getObject()); } } catch (JIException e) { e.printStackTrace(); } } }); // Add a new group final Group group = server.addGroup("test"); // Add a new item to the group final Item item = group.addItem(itemId); // start reading access.bind(); // add a thread for writing a value every 3 seconds ScheduledExecutorService writeThread = Executors.newSingleThreadScheduledExecutor(); final AtomicInteger i = new AtomicInteger(0); writeThread.scheduleWithFixedDelay(new Runnable() { @Override public void run() { final JIVariant value = new JIVariant(i.incrementAndGet()); try { System.out.println(">>> " + "writing value " + i.get()); item.write(value); } catch (JIException e) { e.printStackTrace(); } } }, 5, 3, TimeUnit.SECONDS); // wait a little bit Thread.sleep(20 * 1000); writeThread.shutdownNow(); // stop reading access.unbind(); } catch (final JIException e) { System.out.println(String.format("%08X: %s", e.getErrorCode(), server.getErrorMessage(e.getErrorCode()))); } }