c#写opc服务器,有点问题,多点指教
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using OPCAutomation;
using System.Net;
namespace OPC通讯
{
public partial class Form1 : Form
{
#region 私有变量
OPCServer myOPCServer;//OPC服务器变量声明
OPCGroups myOPCGroups;//OPC服务器组集合
OPCGroup myOPCGroup;//OPCGroups 是OPCGroup对象的集合,外加创建,移除,管理他们的方法。
OPCItems myOPCItems;//OPCItems OPCItems OPCItem对象的集合这是OPCgroup对象的默认属性。
OPCItem myOPCItem;
string strHostIP = "";
string strHostName = "";
bool opc_connected = false;
int itmHandleClient = 0;//客户端句柄
int itmHandleServer = 0;//服务端句柄
#endregion
public Form1()
{
InitializeComponent();
}
#region 加载Form1_Load
private void Form1_Load(object sender, EventArgs e)
{
//获取本地计算机IP、计算机名称
IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[0].ToString();
textBox1.Text = strHostIP;
}
else
{
return;
}
//通过IP来获取计算机名称,可用在局域网内
IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
strHostName = ipHostEntry.HostName.ToString();
//获取本地计算机上的OPCServerName
try
{
myOPCServer = new OPCServer();
object serverList = myOPCServer.GetOPCServers(strHostName);
foreach (string turn in (Array)serverList)
{
comboBox1.Items.Add(turn);
}
comboBox1.SelectedIndex = 0;
button1.Enabled = true;
}
catch (Exception err)
{
MessageBox.Show("枚举本地OPC服务器出错:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
#endregion
#region 建立连接
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!ConnectRemoteServer(textBox1.Text, comboBox1.Text))
{
return;
}
btnSetGroupPro.Enabled = true;
opc_connected = true;
GetServerInfo();
RecurBrowse(myOPCServer.CreateBrowser());
if (!CreateGroup())
{
return;
}
}
catch (Exception err)
{
MessageBox.Show("初始化出错:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
[ 此帖被sakura12345在2018-11-13 17:01重新编辑 ]