




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using OPCAutomation;using System.Net;namespace Opc_Client public partial class Form1 : Form public Form1() InitializeCompone
2、nt(); #region 私?有®D變À?量¢? OPCServer SyServer; OPCGroups SyGroups; OPCGroup SyGroup; OPCItems SyItems; OPCItem SyItem; string strHostIP = string.Empty; string strHostName = string.Empty; bool opc_connected = false; /連¢?接¨®狀Á¡ä態(tài)¬? int itmHandleClient =
3、 0; /客¨ª戶¡ì端?句?柄À¨² int itmHandleServer = 0; /服¤t務?端?句?柄À¨² #endregion #region 方¤?法¤¡§ private void GetLocalServer() IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName(); if (IPHost.AddressList.Length > 0) strHostIP = I
4、PHost.AddressList4.ToString(); this.tboxServerIP.Text = strHostIP.ToString(); else return; IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP); strHostName = ipHostEntry.HostName.ToString(); try SyServer = new OPCServer(); object serverList = SyServer.GetOPCServers(strHostName); foreach (strin
5、g turn in (Array)serverList) this.cmbServerName.Items.Add(turn); cmbServerName.SelectedIndex = 0; btnConnServer.Enabled = true; catch (Exception err) MessageBox.Show("枚?舉¨´本À?地Ì?OPC服¤t務?器¡Â出?錯䨪:êo"+err.Message,"提¬¨
6、2;示º?信?息¡é",MessageBoxButtons.OK,MessageBoxIcon.Warning); private bool CreatGroup() try SyGroups = SyServer.OPCGroups; SyGroup = SyGroups.Add("OPCDOTNETGROUP"); SetGroupProperty(); SyGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(SyGroup_DataChange); SyG
7、roup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(SyGroup_AsyncWriteComplete); SyItems = SyGroup.OPCItems; catch (Exception err) MessageBox.Show("創(chuàng)ä¡ä建¡§組Á¨¦出?現(xiàn)?錯䨪誤¨®"+err.Message,"提¬
8、168;¢示º?信?息¡é",MessageBoxButtons.OK,MessageBoxIcon.Warning); return false; return true; private void LoadDataToTree(OPCBrowser oPCBrowser, TreeNodeCollection treeNodeCollection) oPCBrowser.Organization.ToString(); oPCBrowser.ShowBranches(); foreach (object turn in oPCBrowser
9、) TreeNode node = treeNodeCollection.Add(turn.ToString(); treeView1.SelectedNode = node; oPCBrowser.MoveDown(turn.ToString(); LoadDataToTree(oPCBrowser, node.Nodes); oPCBrowser.MoveUp(); oPCBrowser.ShowLeafs(false); foreach (object turn in oPCBrowser) treeView1.SelectedNode.Nodes.Add(turn.ToString()
10、; private void RecurBrows(OPCBrowser oPCBrowser) /string i = oPCBrowser.Organization LoadDataToTree(oPCBrowser, treeView1.Nodes0.Nodes); oPCBrowser.ShowBranches(); oPCBrowser.ShowLeafs(true); foreach (object turn in oPCBrowser) listBox1.Items.Add(turn.ToString(); private void GetServerInfo() this.to
11、olStripStatusLabel1.Text = "開a始º?時º¡À間?:êo" + SyServer.StartTime.ToString() + " " this.toolStripStatusLabel2.Text = "版ã?本À?:êo" + SyServer.MajorVersion.ToString() + "." + SyServer.MinorVersion.ToString() + "."
12、+ SyServer.BuildNumber.ToString(); private Boolean ConnectRemoteServer(string remoteServerIP, string remoteServerName) try SyServer.Connect( remoteServerName,remoteServerIP); if (SyServer.ServerState = (int)OPCServerState.OPCRunning) this.toolStripStatusLabel3.Text = "已°?連¢?接¨
13、74;到Ì?-" + SyServer.ServerName + " " else this.toolStripStatusLabel3.Text = "狀Á¡ä態(tài)¬?:êo" + SyServer.ServerState.ToString() + " " catch (Exception err) MessageBox.Show("連¢?接¨®遠?程¨¬服¤t務?器¡Â出?錯
14、䨪:êo" + err.Message, "提¬¨¢示º?信?息¡é", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; return true; private void SetGroupProperty() SyServer.OPCGroups.DefaultGroupIsActive = Convert.ToBoolean(boBox1.SelectedItem); SyServer
15、.OPCGroups.DefaultGroupDeadband = Convert.ToInt32(textBox1.Text); SyGroup.UpdateRate = Convert.ToInt32(textBox2.Text); SyGroup.IsActive = Convert.ToBoolean(comboBox2.SelectedItem); SyGroup.IsSubscribed = Convert.ToBoolean(comboBox3.SelectedItem); #endregion #region 事º?件t void SyGroup_AsyncWrite
16、Complete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors) this.lblState.Text = "" for (int i = 1; i <= NumItems; i+) this.lblState.Text += "Tran:êo" + TransactionID.ToString() + " CH:êo" + ClientHandles.GetValue(i).ToString() + &q
17、uot; Error:êo" + Errors.GetValue(i).ToString(); void SyGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) for (int i = 1; i <= NumItems; i+) this.tboxcurrvalue.Text = ItemValues.GetValue(i).ToString
18、(); this.tboxcurrquality.Text = Qualities.GetValue(i).ToString(); this.tboxcurrtime.Text = TimeStamps.GetValue(i).ToString(); #endregion private void Form1_Load(object sender, EventArgs e) btnConnServer.Enabled = false; btnServerBrush.Enabled = false; btnsetproparty.Enabled = false; btnwritevalue.En
19、abled = false; treeView1.Nodes.Add("Root"); GetLocalServer(); /SyServer.OPCGroups.DefaultGroupIsActive = Convert.ToBoolean(boBox1.SelectedItem); private void listBox1_SelectedIndexChanged(object sender, EventArgs e) try if (itmHandleClient != 0) this.tboxcurrvalue.Text = "" this.
20、tboxcurrquality.Text = "" this.tboxcurrtime.Text = "" Array Errors; OPCItem bItem = SyItems.GetOPCItem(itmHandleServer); int temp = new int2 0, bItem.ServerHandle ; Array serverHandle = (Array)temp; SyItems.Remove(SyItems.Count, ref serverHandle, out Errors); /int cancelID; itmHa
21、ndleClient = 1234; SyItem = SyItems.AddItem(listBox1.SelectedItem.ToString(), itmHandleClient); itmHandleServer = SyItem.ServerHandle; /SyItem.Read(listBox1.SelectedItem.ToString(), out Value, out Quality, out TimeStamp); /SyGroup.AsyncRead(1, ref serverHandle, out Errors, 2016, out cancelID); catch
22、 (Exception err) itmHandleClient = 0; this.tboxcurrvalue.Text = "Error ox" this.tboxcurrquality.Text = "Error ox" this.tboxcurrtime.Text = "Error ox" MessageBox.Show("此ä?項?為a系¦Ì統(tǒng)ª3保À¡ê留¢?項?:êo"+err.Message,"提
23、172;¨¢示º?信?息¡é"); private void Form1_FormClosing(object sender, FormClosingEventArgs e) if (!opc_connected) return; if (SyGroup != null) SyGroup.DataChange -=new DIOPCGroupEvent_DataChangeEventHandler(SyGroup_DataChange); if (SyServer != null) SyServer.Disconnect(); SyS
24、erver = null; opc_connected = false; private void btnsetproparty_Click(object sender, EventArgs e) SetGroupProperty(); private void btnConnServer_Click(object sender, EventArgs e) try if (!ConnectRemoteServer(tboxServerIP.Text, cmbServerName.Text) return; btnsetproparty.Enabled = true; opc_connected
25、 = true; GetServerInfo(); RecurBrows(SyServer.CreateBrowser(); if (!CreatGroup() return; catch (Exception err) MessageBox.Show("初?始º?化¡¥出?錯䨪:êo"+err.Message,"提¬¨¢示º?信?息¡é",MessageBoxButtons.OK,MessageBoxIcon.Warni
26、ng); private void btnwritevalue_Click(object sender, EventArgs e) OPCItem bItem = SyItems.GetOPCItem(itmHandleServer); int temp = new int2 0,bItem.ServerHandle ; Array serverHandle = (Array)temp; object valueTemp = new object2 "", tboxvaluewrite.Text ; Array values = (Array)valueTemp; Array Erro
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年環(huán)境檢測與評估技能考試題及答案
- 導游業(yè)務試題及答案電大
- 時鐘測試題目大全圖片及答案
- float面試題及答案
- 三體名著試題及答案
- 焊接加工考試題及答案
- 2025年歷史文化與博物館管理考試試題及答案
- 借款咨詢服務協(xié)議書
- 機電工程決策支持試題及答案
- 軟件設計師考試學習策略分享試題及答案
- 干部履歷表填寫范本(中共中央組織部1999年)
- 勞動教育視角下高職院校學生工匠精神培育研究
- 最簡單封陽臺安全免責協(xié)議書
- SH/T 3533-2024 石油化工給水排水管道工程施工及驗收規(guī)范(正式版)
- 用友人力資源管理HR解決方案樣本
- 北京市西城區(qū)三帆中學2023-2024學年七年級下學期期中數(shù)學試題(無答案)
- 藥物殘留溶劑分析報告書
- 腫瘤醫(yī)院推廣方案
- 動物出血性肺炎預防與治療
- 公路工程安全風險辨識與防控手冊
- 研究生開題報告評審表
評論
0/150
提交評論