c# opc客户端开发
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCAutomation;
using ;
namespace Opc_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region私?有®D变À?量¢?
OPCServer SyServer;
OPCGroups SyGroups;
OPCGroup SyGroup;
OPCItems SyItems;
OPCItem SyItem;
string strHostIP = string.Empty;
string strHostName = string.Empty;
bool opc_connected = false; //连¢?接¨®状Á¡ä态¬?
int itmHandleClient = 0; //客¨ª户¡ì端?句?柄À¨²
int itmHandleServer = 0; //服¤t务?端?句?柄À¨²
#endregion
#region方¤?法¤¡§
private void GetLocalServer()
{
IPHostEntry IPHost = Dns.GetHostEntry(Dns.GetHostName());
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[4].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 (string 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,"提¬¨¢示º?信?息¡é",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
private bool CreatGroup()
{
try
{
SyGroups = SyServer.OPCGroups;
SyGroup = SyGroups.Add("OPCDOTNETGROUP");
SetGroupProperty();
SyGroup.DataChange += new
DIOPCGroupEvent_DataChangeEventHandler(SyGroup_DataChange);
SyGroup.AsyncWriteComplete += new
DIOPCGroupEvent_AsyncWriteCompleteEventHandler(SyGroup_AsyncWriteComplete);
SyItems = SyGroup.OPCItems;
}
catch (Exception err)
{
MessageBox.Show("创ä¡ä建¡§组Á¨¦出?现?错䨪误¨®"+err.Message,"提¬¨¢示º?信?息¡é",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return false;
}
return true;
}
private void LoadDataToTree(OPCBrowser oPCBrowser, TreeNodeCollection treeNodeCollection)
{
anization.ToString();
oPCBrowser.ShowBranches();
foreach (object turn in oPCBrowser)
{
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());
}