c#寫opc服務(wù)器,有點(diǎn)問題,多點(diǎn)指教
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服務(wù)器變量聲明
OPCGroups myOPCGroups;//OPC服務(wù)器組集合
OPCGroup myOPCGroup;//OPCGroups 是OPCGroup對象的集合,外加創(chuàng)建,移除,管理他們的方法。
OPCItems myOPCItems;//OPCItems OPCItems OPCItem對象的集合這是OPCgroup對象的默認(rèn)屬性。
OPCItem myOPCItem;
string strHostIP = "";
string strHostName = "";
bool opc_connected = false;
int itmHandleClient = 0;//客戶端句柄
int itmHandleServer = 0;//服務(wù)端句柄
#endregion
public Form1()
{
InitializeComponent();
}
#region 加載Form1_Load
private void Form1_Load(object sender, EventArgs e)
{
//獲取本地計(jì)算機(jī)IP、計(jì)算機(jī)名稱
IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[0].ToString();
textBox1.Text = strHostIP;
}
else
{
return;
}
//通過IP來獲取計(jì)算機(jī)名稱,可用在局域網(wǎng)內(nèi)
IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
strHostName = ipHostEntry.HostName.ToString();
//獲取本地計(jì)算機(jī)上的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服務(wù)器出錯(cuò):" + 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("初始化出錯(cuò):" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
[ 此帖被sakura12345在2018-11-13 17:01重新編輯 ]