c#opc客戶端開發(fā)_第1頁
c#opc客戶端開發(fā)_第2頁
c#opc客戶端開發(fā)_第3頁
c#opc客戶端開發(fā)_第4頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(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務(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ù) ?器?出 ?錯?¨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、務(wù)?器?出?錯?¨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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。