终于还有一个就写完了,这次是opc,同样需要模拟器,这里使用的是MatrikonOPCSimulation,其实具体怎么做参考网上的内容完全足够https://openscada.atlassian.net/wiki/display/OP/HowToStartWithUtgard,只不过想记录一下自己学过的东西,所以还是自己写一下。
使用opc协议不仅需要导入jar包,还要关闭防火墙,否则会出现错误,不知道有没有更好的办法,有待以后去解决。
import java.net.UnknownHostException;
import java.util.concurrent.Executors;import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.core.JIVariant;
import org.openscada.opc.lib.common.AlreadyConnectedException;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.common.NotConnectedException;
import org.openscada.opc.lib.da.AccessBase;
import org.openscada.opc.lib.da.AddFailedException;
import org.openscada.opc.lib.da.DataCallback;
import org.openscada.opc.lib.da.DuplicateGroupException;
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 opcUtil {public void read() {ConnectionInformation conn = new ConnectionInformation();conn.setHost("127.0.0.1");conn.setDomain("WORKGROUP");conn.setUser("计算机的用户名");conn.setPassword("计算机的密码");conn.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");conn.setProgId("Matrikon.OPC.Simulation.1");String itemId = "Bucket Brigade.Int4";Server server = new Server(conn,Executors.newSingleThreadScheduledExecutor());try {server.connect();AccessBase access;access = new SyncAccess(server, 500);access.addItem(itemId, new DataCallback() {@Overridepublic void changed(Item item, ItemState state) {try {System.out.println(state.getValue().getObjectAsInt());} catch (Exception e) {e.printStackTrace();}}});access.bind();Thread.sleep(500);access.unbind();} catch (JIException e) {System.out.println(String.format("%08X: %s", e.getErrorCode(),server.getErrorMessage(e.getErrorCode())));} catch (IllegalArgumentException e1) {e1.printStackTrace();} catch (UnknownHostException e1) {e1.printStackTrace();} catch (AlreadyConnectedException e1) {e1.printStackTrace();} catch (NotConnectedException e) {e.printStackTrace();} catch (DuplicateGroupException e) {e.printStackTrace();} catch (AddFailedException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}private class DataCallbackDump implements DataCallback {Object value;public DataCallbackDump(Object value) {this.value = value;}@Overridepublic void changed(Item item, ItemState state) {JIVariant write = new JIVariant((Integer) value);try {item.write(write);} catch (JIException e) {e.printStackTrace();}}}public void write() {ConnectionInformation conn = new ConnectionInformation();conn.setHost("127.0.0.1");conn.setDomain("WORKGROUP");conn.setUser("计算机的用户名");conn.setPassword("计算机的密码");conn.setClsid("F8582CF2-88FB-11D0-B850-00C0F0104305");conn.setProgId("Matrikon.OPC.Simulation.1");String itemId = "Bucket Brigade.Int4";Server server = new Server(conn,Executors.newSingleThreadScheduledExecutor());final Object object = 5;// final JIVariant value = new JIVariant("6");try {server.connect();AccessBase access;access = new SyncAccess(server, 500);DataCallbackDump dcd = new DataCallbackDump(object);access.addItem(itemId, dcd);access.bind();Thread.sleep(500);access.unbind();} catch (JIException e) {System.out.println(String.format("%08X: %s", e.getErrorCode(),server.getErrorMessage(e.getErrorCode())));} catch (IllegalArgumentException e1) {e1.printStackTrace();} catch (UnknownHostException e1) {e1.printStackTrace();} catch (AlreadyConnectedException e1) {e1.printStackTrace();} catch (NotConnectedException e) {e.printStackTrace();} catch (DuplicateGroupException e) {e.printStackTrace();} catch (AddFailedException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}public static void main(String[] args) {opcUtil util = new opcUtil();util.write();util.read();}}