



版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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務(wù)?端?句?柄à¨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務(wù) ?器?出 ?錯(cuò)?¨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)?錯(cuò)?¨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(jì)?時(shí)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(" 連?接¨?遠(yuǎn)?程¨?服¤t
11、務(wù)?器?出?錯(cuò)?¨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(" 此 ?項(xiàng)?為 a系| ì統(tǒng)a3保à?留ê ?項(xiàng)?: ê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(jì)?化 ?¥出?錯(cuò)?¨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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 服裝設(shè)計(jì)師與時(shí)尚公司長(zhǎng)期合作協(xié)議
- 智能門鎖集成安裝與定期檢修服務(wù)協(xié)議
- 灘涂貝類養(yǎng)殖權(quán)委托經(jīng)營(yíng)管理全面合作協(xié)議
- 康養(yǎng)中心護(hù)理服務(wù)委托專業(yè)運(yùn)營(yíng)協(xié)議
- 跨界腦機(jī)接口技術(shù)研發(fā)與市場(chǎng)推廣合作協(xié)議
- 留學(xué)中介服務(wù)及海外院校錄取通知書獲取及簽證申請(qǐng)輔導(dǎo)及生活適應(yīng)輔導(dǎo)協(xié)議
- 耕地流轉(zhuǎn)規(guī)?;N植項(xiàng)目委托管理合同
- 班組長(zhǎng)現(xiàn)場(chǎng)管理
- 全身人物繪畫美術(shù)課件
- 癱瘓人員護(hù)理要點(diǎn)與規(guī)范
- 小班語(yǔ)言《水珠寶寶》課件
- 加油站職業(yè)危害防治計(jì)劃和實(shí)施方案
- 路面彎沉溫度修正系數(shù)
- 山東省濟(jì)南市槐蔭區(qū)2024屆中考聯(lián)考化學(xué)試題含解析
- (完整版)xx中學(xué)“雙積雙評(píng)”積分入團(tuán)實(shí)施方案
- 拖拉機(jī)和聯(lián)合收割機(jī)安全技術(shù)檢驗(yàn)合格證明
- 文化產(chǎn)業(yè)政策與法規(guī)
- 第四講嬰幼兒情緒情感及社會(huì)功能的發(fā)育
- (完整word版)餐券模板
- 平野蒼流:打漁張引黃灌區(qū)開灌60周年紀(jì)念專輯
- 電梯安全風(fēng)險(xiǎn)管控清單
評(píng)論
0/150
提交評(píng)論