>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using GodSharp.Opc.Da; using OPCAutomation;//引用OPC驱动 namespace 泵站控制系统 { public partial class Form1 : Form { public Form1()
{ InitializeComponent(); this.button1_Click(null,null); } private void Form1_Load(object sender, EventArgs e) { ServerNode.Text = "127.0.0.1"; } private OPCServer KepServer; #region 刷新节点 private void button1_Click(object sender, EventArgs e)//刷新节点 { this.ServerNode.Items.Clear(); string computerName = Environment.MachineName; IPHostEntry IPhost = Dns.GetHostEntry(System.Environment.MachineName); if (IPhost.AddressList.Length>0) { for(int i=0;i<IPhost.AddressList.Length;i++) { // string Host = Dns.GetHostEntry(IPhost.AddressList).HostName; //获取电脑主机名称 string Host = "localhost"; if (!this.ServerNode.Items.Contains(Host)) { this.ServerNode.Items.Add(Host); } } } #endregion
} #region 获取OPC列表 private void ServerNode_SelectedIndexChanged(object sender, EventArgs e) { if(KepServer==null) { KepServer = new OPCServer(); } Object serverlist=KepServer.GetOPCServers(this.servername.Text);
foreach(var item in (Array)serverlist) { if (!this.servername.Items.Contains(item)) { this.servername.Items.Add(item); }
}
} #endregion #region 链接OPC private void button2_Click(object sender, EventArgs e) { if (KepServer != null)//判断OPC是否存在 { KepServer = new OPCServer(); } try { KepServer.Connect(this.servername.Text, this.ServerNode.Text); MessageBox.Show("连接成功"); } catch (Exception ex) { MessageBox.Show("链接失败" + ex.Message); return;//连接失败,退出运行,后面代码不执行 } #endregion } }
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |