技术论坛

作者 主题
HUO1921
侠圣

经验值: 2391
发帖数: 152
精华帖: 6
楼主    2015-11-11 15:48:52
主题:【分享】C#中实现OPC数据访问(学习笔记二)C#编程

测试环境:SIMATIC_NET_8.2

          Visual studio 2010
          Step7 V5.5

系统:Windows Win7旗舰版

测试结果:同步读写数据成功

          异步读写数据成功

          订阅式读取并分解数据成功

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;


using OPCAutomation;


namespace _4_Automation_RW

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        OPCServer MyOpcServer; //定义OPCSe

        OPCGroup MyOpcGroup; //定义组



        const int ItemCount = 6;

        OPCItem[] MyOpcItem = new OPCItem[ItemCount+1]; //Item

        int[] ServerHandle = new int[ItemCount];//服务器端注册句柄


        //连接OPCServer,建立Group,添加Item

        private void btn_Conn_Click(object sender, EventArgs e)

        {

            try

            {

                MyOpcServer = new OPCServer();

                MyOpcServer.Connect("OPC.SimaticNet", txt_IP.Text.Trim());//OPCServer

                MyOpcGroup = MyOpcServer.OPCGroups.Add("MyGroup1");

                MyOpcGroup.IsActive = true;  //OPCGroup的IsActive属性:用以控制 OPC 组的活动状态。只有活动状态的 OPC 组才进行定期的数据更新。

                MyOpcGroup.IsSubscribed = false;//是否异步, 在采用异步读写, 订阅等方式下都需要

                MyOpcGroup.DeadBand = 0;  //新添加的 OPC 组的不敏感带的默认值(%) 。初期值是 0%。

                MyOpcGroup.UpdateRate = 1;//OPCGroup的UpdateRate属性:数据更新周期(毫秒) 。


                MyOpcItem[0] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,INT0", 1);

                MyOpcItem[1] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,INT2", 2);

                MyOpcItem[2] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,W4", 3);

                MyOpcItem[3] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,B6", 4);

                MyOpcItem[4] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,REAL8", 5);

                MyOpcItem[5] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,REAL12", 6);

                MyOpcItem[6] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]MX100.0", 7);


                ServerHandle[0] = MyOpcItem[0].ServerHandle;

                ServerHandle[1] = MyOpcItem[1].ServerHandle;

                ServerHandle[2] = MyOpcItem[2].ServerHandle;

                ServerHandle[3] = MyOpcItem[3].ServerHandle;

                ServerHandle[4] = MyOpcItem[4].ServerHandle;

                ServerHandle[5] = MyOpcItem[5].ServerHandle;


                MyOpcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(MyOpcGroup_AsyncWriteComplete);

                MyOpcGroup.AsyncReadComplete += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(MyOpcGroup_AsyncReadComplete);

                MyOpcGroup.AsyncCancelComplete += new DIOPCGroupEvent_AsyncCancelCompleteEventHandler(MyOpcGroup_AsyncCancelComplete);

                MyOpcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(MyOpcGroup_DataChange);

            }

            catch (System.Exception error)

            {

                MessageBox.Show(error.Message, "Result - connect server", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }


        //同步读数据

        private void btn_Read_Sync_Click(object sender, EventArgs e)

        {

            if (MyOpcServer == null)

            {

                MessageBox.Show("请连接OPCServer");

                return;

            }

            object ItemValues;

            object Qualities;

            object TimeStamps;

            try

            {

                MyOpcItem[0].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R1_Value.Text = String.Format("{0}", ItemValues);

                txt_R1_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R1_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp


                MyOpcItem[1].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R2_Value.Text = String.Format("{0}", ItemValues);

                txt_R2_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R2_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp

                

                MyOpcItem[2].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R3_Value.Text = String.Format("{0}", ItemValues);

                txt_R3_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R3_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp

                

                MyOpcItem[3].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R4_Value.Text = String.Format("{0}", ItemValues);

                int temp = Convert.ToInt32(txt_R4_Value.Text);

                bool[] temp2 = ByteToBool(temp);

                btn_LED1.BackColor = temp2[0] ? Color.Green : Color.White;

                btn_LED2.BackColor = temp2[1] ? Color.Green : Color.White;

                btn_LED3.BackColor = temp2[2] ? Color.Green : Color.White;

                btn_LED4.BackColor = temp2[3] ? Color.Green : Color.White;

                btn_LED5.BackColor = temp2[4] ? Color.Green : Color.White;

                btn_LED6.BackColor = temp2[5] ? Color.Green : Color.White;

                btn_LED7.BackColor = temp2[6] ? Color.Green : Color.White;

                btn_LED8.BackColor = temp2[7] ? Color.Green : Color.White;

                txt_R4_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R4_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp

                

                MyOpcItem[4].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R5_Value.Text = String.Format("{0}", ItemValues);

                txt_R5_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R5_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp


                MyOpcItem[5].Read(1, out ItemValues, out Qualities, out TimeStamps);

                txt_R6_Value.Text = String.Format("{0}", ItemValues);

                txt_R6_Quality.Text = String.Format("{0}", Qualities);  // Quality

                txt_R6_TimeStamp.Text = String.Format("{0}", TimeStamps);   // Timestamp

            }

            catch (System.Exception error)

            {

                MessageBox.Show(error.Message, "Result - 同步读", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }


        //同步写数据

        private void btn_Write_Sync_Click(object sender, EventArgs e)

        {

            if (MyOpcServer == null)

            {

                MessageBox.Show("请连接OPCServer");

                return;

            }

            try

            {

                MyOpcItem[0].Write(txt_W1.Text);

                MyOpcItem[1].Write(txt_W2.Text);

                MyOpcItem[2].Write(txt_W3.Text);

                MyOpcItem[3].Write(txt_W4.Text);

                MyOpcItem[4].Write(txt_W5.Text);

                MyOpcItem[5].Write(txt_W6.Text);

            }

            catch (System.Exception error)

            {

                MessageBox.Show(error.Message, "Result - 同步写", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }


hongxi-002@qq.com
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。