第一種:用utgard連接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("gl"); 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()))); } }
第二種:使用jOpc連接 opc服務器,只支持32位的
package com.kaiya.svg.opc; import javafish.clients.opc.JOpc; import javafish.clients.opc.asynch.AsynchEvent; import javafish.clients.opc.asynch.OpcAsynchGroupListener; import javafish.clients.opc.component.OpcGroup; import javafish.clients.opc.component.OpcItem; import javafish.clients.opc.exception.Asynch10ReadException; import javafish.clients.opc.exception.Asynch10UnadviseException; import javafish.clients.opc.exception.CoInitializeException; import javafish.clients.opc.exception.ComponentNotFoundException; import javafish.clients.opc.exception.ConnectivityException; import javafish.clients.opc.exception.CoUninitializeException; import javafish.clients.opc.exception.GroupActivityException; import javafish.clients.opc.exception.GroupUpdateTimeException; import javafish.clients.opc.exception.UnableAddGroupException; import javafish.clients.opc.exception.UnableAddItemException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Iterator; public class AsynchReadAndGroupActivityExample { /** * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { AsynchReadAndGroupActivityExample test = new AsynchReadAndGroupActivityExample(); try { /**初始化服務**/ JOpc.coInitialize(); } catch (CoInitializeException e1) { e1.printStackTrace(); } /**建立server對象jopc(IP ,OPCServer名稱 ,任意)**/ JOpc jopc = new JOpc("localhost", "Matrikon.OPC.Simulation.1", "JOPC1"); /**建立一個組((用戶組的標識名稱),開始活動的組(true 開始 false 不開始) 默認true,刷新組的時間 毫秒,默認 0.0f)**/ OpcGroup group = new OpcGroup("test", true, 20, 0.0f); /**name屬性表示OPC服務器中的ItemID**/ OpcItem item1 = new OpcItem("Bucket Brigade.Int1", true, ""); OpcItem item2 = new OpcItem("Bucket Brigade.Int4", true, ""); /**添加到組**/ group.addItem(item1); group.addItem(item2); jopc.addGroup(group); try { /**調用JCustomOpc.connect()連接服務器**/ jopc.connect(); System.out.println("OPC client is connected..."); System.out.println("OPC 服務器連接成功..."); /** 調用JOpc.registerGroups()註冊所有的組使用registerGroups()方法註冊則OpcItem不用單獨註冊。如果調用registerGroup(OpcGroup)註冊OpcGroup,則還需調用registerItem(OpcGroup,OpcItem)**/ jopc.registerGroups(); System.out.println("OPC groups are registered..."); System.out.println("OPC groups 註冊成功..."); /** 調用JOpc.asynch10Read()異步讀數據**/ jopc.asynch10Read(group); System.out.println("OPC asynchronous reading is applied..."); System.out.println("OPC 正在異步讀取數據..."); OpcGroup downGroup; /**當前時間秒數**/ long start = System.currentTimeMillis(); while ((System.currentTimeMillis() - start) < 10000) { jopc.ping(); downGroup = jopc.getDownloadGroup(); if (downGroup != null) { System.out.println(downGroup); } if ((System.currentTimeMillis() - start) >= 6000) { jopc.setGroupActivity(group, false); } synchronized(test) { /**間隔50毫秒**/ test.wait(1000); } } // change activity jopc.setGroupActivity(group, true); // change updateTime jopc.setGroupUpdateTime(group, 100); start = System.currentTimeMillis(); while ((System.currentTimeMillis() - start) < 10000) { jopc.ping(); downGroup = jopc.getDownloadGroup(); if (downGroup != null) { System.out.println(downGroup); } synchronized(test) { test.wait(2000); } } /**斷開異步讀取**/ jopc.asynch10Unadvise(group); System.out.println("OPC asynchronous reading is unadvise..."); System.out.println("OPC 斷開異步讀取..."); /**斷開連接**/ JOpc.coUninitialize(); System.out.println("Program terminated..."); System.out.println("斷開服務器連接..."); } catch (ConnectivityException e) { e.printStackTrace(); } catch (UnableAddGroupException e) { e.printStackTrace(); } catch (UnableAddItemException e) { e.printStackTrace(); } catch (ComponentNotFoundException e) { e.printStackTrace(); } catch (Asynch10ReadException e) { e.printStackTrace(); } catch (Asynch10UnadviseException e) { e.printStackTrace(); } catch (GroupUpdateTimeException e) { e.printStackTrace(); } catch (GroupActivityException e) { e.printStackTrace(); } catch (CoUninitializeException e) { e.printStackTrace(); } } }
如果報這樣的錯:
Property file javafish.clients.opc.JCustomOpc doesn't exist. System terminated那你就建立
javafish.clients.opc
這個文件夾,把JCustomOpc.properties 文件放入下面,文件內容如下
在與src同目錄下建立lib文件夾,放入JCustomOpc.dll 文件(自己下吧),當然你還需要jeasyopc.jar文件。
難得寫還是給你個鏈接吧: https://download.csdn.net/download/qq_37838223/10465135