



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、.using System;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using OPCAutomation;using System.Net;namespace Opc_ClientpublicpartialclassForm1 :FormpublicForm1()InitializeComponent();#region私?有?D變à?量?OPCServer SyServer;OPCGroups SyGroups;OP
2、CGroup SyGroup;OPCItems SyItems;OPCItem SyItem;stringstrHostIP =string.Empty;1 / 9.stringstrHostName =string.Empty;boolopc_connected =false ;/ 連?接¨?狀á?態(tài)?intitmHandleClient = 0;/ 客¨a戶?ì端?句 ?柄à¨2intitmHandleServer = 0;/ 服¤t務?端?句?柄à¨2#endregion#region方¤
3、?法 ¤?§privatevoid GetLocalServer()strHostIP = IPHost.AddressList4.ToString();this .tboxServerIP.Text = strHostIP.ToString();elsereturn ;trySyServer =new OPCServer();objectserverList = SyServer.GetOPCServers(strHostName);foreach ( stringturnin ( Array )serverList)cmbServerName.SelectedIndex
4、 = 0;btnConnServer.Enabled =true ;catch ( Exceptionerr)MessageBox.Show(" 枚 ?舉¨本à?地ì?OPC服¤t務 ?器?出 ?錯?¨a:ê o"+err.Message, " 提?¨示o?信?息?é",MessageBoxButtons .OK, MessageBoxIcon.Warning);2 / 9.privatebool CreatGroup()trySyGroups = SyServer.OPCG
5、roups;SyGroup = SyGroups.Add("OPCDOTNETGROUP");SetGroupProperty();SyGroup.DataChange +=newDIOPCGroupEvent_DataChangeEventHandler(SyGroup_DataChange);SyGroup.AsyncWriteComplete +=newDIOPCGroupEvent_AsyncWriteCompleteEventHandler(SyGroup_AsyncWriteComplete); SyItems = SyGroup.OPCItems;catch
6、( Exceptionerr)MessageBox.Show(" 創(chuàng) ?建?§ 組á¨|出?現(xiàn)?錯?¨a誤¨?" +err.Message, " 提?¨示o?信?息?é",MessageBoxButtons .OK, MessageBoxIcon.Warning);returnfalse ;returntrue ;private void LoadDataToTree(OPCBrowser oPCBrowser, TreeNodeCollection treeNodeCollectio
7、n)oPCBrowser.ShowBranches();foreach ( objectturnin oPCBrowser)TreeNode node = treeNodeCollection.Add(turn.ToString();treeView1.SelectedNode = node;oPCBrowser.MoveDown(turn.ToString();LoadDataToTree(oPCBrowser, node.Nodes);oPCBrowser.MoveUp();oPCBrowser.ShowLeafs(false );foreach ( objectturnin oPCBro
8、wser)3 / 9.privatevoid RecurBrows(OPCBrowser oPCBrowser)/string i = oPCBrowser.OrganizationLoadDataToTree(oPCBrowser, treeView1.Nodes0.Nodes);oPCBrowser.ShowBranches();oPCBrowser.ShowLeafs(true );foreach ( objectturnin oPCBrowser)privatevoid GetServerInfo()this .toolStripStatusLabel1.Text =" 開a
9、始o?時o?à間?:êo"+""this .toolStripStatusLabel2.Text =" 版?本à?:êo" +".""."+private Boolean ConnectRemoteServer( string remoteServerIP, string remoteServerName)trySyServer.Connect( remoteServerName,remoteServerIP);if(SyServer.ServerState = (
10、int )OPCServerState.OPCRunning)this .toolStripStatusLabel3.Text =" 已°?連?接¨?到ì?-" +SyServer.ServerName +""elsethis .toolStripStatusLabel3.Text =" 狀á?態(tài)?:êo"+""catch ( Exceptionerr)4 / 9.MessageBox.Show(" 連?接¨?遠?程¨?服¤t
11、務?器?出?錯?¨a:êo" + err.Message, " 提 ?¨示o?信?息?é", MessageBoxButtons .OK, MessageBoxIcon.Warning);returnfalse ;returntrue ;privatevoid SetGroupProperty()Convert .ToBoolean( this .comboBox1.SelectedItem);Convert .ToInt32(textBox1.Text);SyGroup.UpdateRate =Convert .ToInt
12、32(textBox2.Text);SyGroup.IsActive =Convert .ToBoolean(comboBox2.SelectedItem);SyGroup.IsSubscribed =Convert .ToBoolean(comboBox3.SelectedItem);#endregion#region事o?件tvoid SyGroup_AsyncWriteComplete( int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)this .lblState.Text ="
13、;" ;for ( inti = 1; i <= NumItems; i+)this .lblState.Text +="Tran :êo"+ TransactionID.ToString() +"CH:êo"+ ClientHandles.GetValue(i).ToString() +"Error:êo"+Errors.GetValue(i).ToString();void SyGroup_DataChange(intTransactionID,intNumItems, refArra
14、y ClientHandles,refArray ItemValues,refArray Qualities,refArray TimeStamps)for ( inti = 1; i <= NumItems; i+)this .tboxcurrvalue.Text = ItemValues.GetValue(i).ToString();this .tboxcurrquality.Text = Qualities.GetValue(i).ToString();this .tboxcurrtime.Text = TimeStamps.GetValue(i).ToString();5 / 9
15、.#endregionprivatevoid Form1_Load( objectsender,EventArgs e)btnConnServer.Enabled =false ;btnServerBrush.Enabled =false ;btnsetproparty.Enabled =false ;btnwritevalue.Enabled =false ;"Root" );GetLocalServer();privatevoid listBox1_SelectedIndexChanged(objectsender,EventArgs e)tryif(itmHandle
16、Client != 0)this .tboxcurrvalue.Text ="" ;this .tboxcurrquality.Text ="" ;this .tboxcurrtime.Text ="" ;Array Errors;OPCItem bItem = SyItems.GetOPCItem(itmHandleServer);int temp =new int 2 0, bItem.ServerHandle ;Array serverHandle = (Array )temp;SyItems.Remove(SyItems.Co
17、unt,ref serverHandle,out Errors);/int cancelID;itmHandleClient = 1234;itmHandleServer = SyItem.ServerHandle;out TimeStamp);/SyGroup.AsyncRead(1,refserverHandle,out Errors,2016, out cancelID);catch ( Exceptionerr)itmHandleClient = 0;6 / 9.this.tboxcurrvalue.Text ="Error ox"this.tboxcurrqual
18、ity.Text ="Error ox"this.tboxcurrtime.Text ="Error ox" ;MessageBox.Show(" 此 ?項?為 a系| ì統(tǒng)a3保à?留ê ?項?: êo"+err.Message, " 提?¨示o?信?息?é");privatevoid Form1_FormClosing( objectsender,FormClosingEventArgse)if(!opc_connected)return ;if(Sy
19、Group !=null )SyGroup.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(SyGroup_DataChange);if(SyServer !=null )SyServer.Disconnect();SyServer =null ;opc_connected =false ;privatevoid btnsetproparty_Click(objectsender,EventArgs e)SetGroupProperty();privatevoid btnConnServer_Click(objectsender
20、,EventArgs e)tryif(!ConnectRemoteServer(tboxServerIP.Text, cmbServerName.Text)return ;btnsetproparty.Enabled =true ;opc_connected =true ;GetServerInfo();RecurBrows(SyServer.CreateBrowser();if(!CreatGroup()7 / 9.return ;catch ( Exceptionerr)MessageBox.Show(" 初 ?始o?化 ?¥出?錯?¨a:êo"+e
21、rr.Message, " 提?¨示o?信 ?息?é",MessageBoxButtons .OK, MessageBoxIcon.Warning);privatevoid btnwritevalue_Click(objectsender,EventArgs e)OPCItem bItem = SyItems.GetOPCItem(itmHandleServer);int temp =new int 2 0,bItem.ServerHandle ;Array serverHandle = (Array )temp;object valueTemp =new object 2 "" , t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 生物化學分子基礎(chǔ)概念題庫及解析
- 珠寶首飾行業(yè)設(shè)計大賽試題
- 藥品進口代理協(xié)議
- 2025年經(jīng)濟師備考方法分享試題及答案
- 人員管理與績效考核試題及答案
- 項目擴展及合作策略試題及答案
- 信陽市學法用法考試試題及答案
- 防釣魚測試題及答案
- 急診精神科的合作模式計劃
- 購房貸款協(xié)議書
- MOOC 信號與系統(tǒng)-西安郵電大學 中國大學慕課答案
- 《電動汽車充電站設(shè)施與服務分級評價規(guī)范》
- MOOC 生物化學-代謝與營養(yǎng)-南京醫(yī)科大學 中國大學慕課答案
- (正式版)SHT 3225-2024 石油化工安全儀表系統(tǒng)安全完整性等級設(shè)計規(guī)范
- 小班語言《水珠寶寶》課件
- 加油站職業(yè)危害防治計劃和實施方案
- 路面彎沉溫度修正系數(shù)
- 山東省濟南市槐蔭區(qū)2024屆中考聯(lián)考化學試題含解析
- (完整版)xx中學“雙積雙評”積分入團實施方案
- 拖拉機和聯(lián)合收割機安全技術(shù)檢驗合格證明
- 文化產(chǎn)業(yè)政策與法規(guī)
評論
0/150
提交評論