基本信息
源码名称:C# OPC通信成功
源码大小:0.35M
文件格式:.zip
开发语言:C#
更新时间:2019-03-21
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

本人实测通信成功,欢迎下载

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);
            }

        }
    }
}