本人实测通信成功,欢迎下载 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;namespace TestOPC{  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();    }    OPC_Interface _opcItf;    private void button1_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        _opcItf = new OPC_Interface(textBox1.Text, textBox2.Text);        _opcItf.OpenServer();        MessageBox.Show("连接成功");      }      catch (Exception exp)      {        MessageBox.Show("连接失败"  exp.Message);      }    }    private void Form1_Load(object sender, EventArgs e)    {          }    private void button2_Click(object sender, EventArgs e)    {      try      {        _opcItf.Close();        MessageBox.Show("已断开");      }      catch (Exception exp)      {        MessageBox.Show("断开异常:"  exp.Message);      }    }    private void button3_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox3.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        textBox4.Text = _opcItf.Read(textBox3.Text).ToString();        MessageBox.Show("获取值正常返回");      }      catch (Exception exp)      {        MessageBox.Show("获取值异常" exp.Message);      }    }    private void button4_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox3.Text) && string.IsNullOrEmpty(textBox4.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        _opcItf.Write(textBox3.Text,textBox4.Text);        MessageBox.Show("设置值正常");      }      catch (Exception exp)      {        MessageBox.Show("设置值异常"  exp.Message);      }    }    private void timer1_Tick(object sender, EventArgs e) //增加定时器定时自动读取    {      if (string.IsNullOrEmpty(textBox3.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        textBox4.Text = _opcItf.Read(textBox3.Text).ToString();        MessageBox.Show("获取值正常返回");      }      catch (Exception exp)      {        MessageBox.Show("获取值异常"  exp.Message);      }    }  }}
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;namespace TestOPC{  public partial class Form1 : Form  {    public Form1()    {      InitializeComponent();    }    OPC_Interface _opcItf;    private void button1_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        _opcItf = new OPC_Interface(textBox1.Text, textBox2.Text);        _opcItf.OpenServer();        MessageBox.Show("连接成功");      }      catch (Exception exp)      {        MessageBox.Show("连接失败"  exp.Message);      }    }    private void Form1_Load(object sender, EventArgs e)    {          }    private void button2_Click(object sender, EventArgs e)    {      try      {        _opcItf.Close();        MessageBox.Show("已断开");      }      catch (Exception exp)      {        MessageBox.Show("断开异常:"  exp.Message);      }    }    private void button3_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox3.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        textBox4.Text = _opcItf.Read(textBox3.Text).ToString();        MessageBox.Show("获取值正常返回");      }      catch (Exception exp)      {        MessageBox.Show("获取值异常" exp.Message);      }    }    private void button4_Click(object sender, EventArgs e)    {      if (string.IsNullOrEmpty(textBox3.Text) && string.IsNullOrEmpty(textBox4.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        _opcItf.Write(textBox3.Text,textBox4.Text);        MessageBox.Show("设置值正常");      }      catch (Exception exp)      {        MessageBox.Show("设置值异常"  exp.Message);      }    }    private void timer1_Tick(object sender, EventArgs e) //增加定时器定时自动读取    {      if (string.IsNullOrEmpty(textBox3.Text))      {        MessageBox.Show("必填项不能为空");        return;      }      try      {        textBox4.Text = _opcItf.Read(textBox3.Text).ToString();        MessageBox.Show("获取值正常返回");      }      catch (Exception exp)      {        MessageBox.Show("获取值异常"  exp.Message);      }    }  }}

 
  
					
				
评论