1决乎、OPC解釋
-一系列接口违柏、方法和屬性的標準集
基于OLE吱型、COM存筏、DCOM技術碍岔、XML痴施,采用客戶端/服務器結構
將通訊協(xié)議與設備/應用隔離
2擎厢、使用openscada連接OPC
-OPC組成對象:
服務器 OPC Server
組對象 Group
項對象 Item
-openscada開源項目
ConnectionInformation中:
*Host 本地主機/網絡主機IP
*Domain 域(默認localhost)
*User 用戶名
*Password 用戶登錄密碼
*Clsid 應用在注冊表中相對應的CLSID值
*Grogid 應用在注冊表中對應的程序名稱
Clsid和Grogid只需設置一個,優(yōu)先Clsid
3辣吃、開發(fā)過程
1)設置服務器信息
private static String host = "172.16.31.2";
private static String user = "admin";
private static String passwd = "123456";
private static String progId = "S7300ET200M station_1"
2)通過ServerList類獲取服務端所有OPCServer
ServerList serverList = new ServerList(host,user,passwd);
showAllOPCServer(serverList);
3)創(chuàng)建ConnectionInformation類
final ConnectionInformation ci = new ConnectionInformation();
ci.setHost(host);
ci.setClsid(serverList.getClsidFromProgId("S7300ET200M station_1");
ci.setUser(user);
ci.setPassword(passwd);
4)創(chuàng)建Server類并連接动遭,需要處理connect方法可能拋出的異常
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
Server server = new Server(ci, exec);
server.connect();
假如兩次訪問間隔較長,可以通過AutoReconnectController類來自動重新創(chuàng)立連接
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
Server server = new Server(ci, exec);
AutoReconnectController autoReconnectController = new AutoReconnectController ( server );
autoReconnectController.connect();
5)Utgard的數(shù)據(jù)訪問方式
直接通過item的read/write方法或者使用AccessBase(讀取數(shù)據(jù))
需要注意的是神得,不管采用哪一種方式厘惦,返回結果都是通過ItemState
類來獲取,通過調用ItemState的getValue方法可以獲得訪問結果哩簿,
返回結果是JIVarant類型的宵蕉。
A:通過item的read/write方法
/**
* 使用Item類write方法寫入數(shù)據(jù),并直接通過Item的read方法同步讀數(shù)據(jù)
* @throws Exception
*/
public static void syncWrite(Server server) throws Exception{
final String itemId="Bucket Brigade.Int4";
Group group = server.addGroup("test");
Item item = group.addItem(itemId); //get item for writing
//第一次讀
ItemState itemState = item.read(true);
System.out.println("<<< first read: " + itemState.getValue());
final JIVariant value = new JIVariant(100);
try {
System.out.println(">>> writing value: " + value.getObjectAsInt());
item.write(value);
} catch (JIException e) {
e.printStackTrace();
}
itemState = item.read(true);
System.out.println("<<< after writing: " + itemState.getValue());
}
B:使用AccesBase類(讀取數(shù)據(jù))
AccesBase接口有兩個實現(xiàn)類:SyncAccess類和Async20Access類
a)同步訪問SyncAccess
/**
* 使用SyncAccess類隔時間段地進行同步讀取數(shù)據(jù)
* SyncAccess實現(xiàn)了Runnable接口节榜,實際上通過另一個線程進行同步讀
* @throws Exception
*/
public static void syncRead(Server server) throws Exception{
final String itemId="Random.Int4";
//每隔1秒同步讀
AccessBase access = new SyncAccess(server,1000);
access.addItem(itemId, new DataCallback() {
@Override
public void changed(Item item, ItemState itemState) {
System.out.println(itemState);
}
});
// start reading
access.bind();
// wait a little bit
Thread.sleep(5*1000);
// stop reading
access.unbind();
}
b)異步訪問Async20Access類
/**
* 使用Async20Access類隔時間段地進行異步讀取數(shù)據(jù)
* Async20Access實現(xiàn)了IOPCDataCallback接口羡玛,基于事件回調的實現(xiàn)
* @throws Exception
*/
public static void asyncRead(Server server) throws Exception{
final String itemId = "Random.Int4";
//第三個參數(shù)用于設置初始化時是否執(zhí)行訪問
AccessBase access = new Async20Access(server, 1000, false);
access.addItem(itemId, new DataCallback(){
@Override
public void changed(Item item, ItemState itemState) {
System.out.println(">>> Asynchronized read: value="
+ itemState.getValue());
}
});
access.bind();
Thread.sleep(5*1000);
access.unbind();
}