Frm_OPCTest.xaml.cs 1.71 KB
using OPCHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HHWCSHost.View
{
    /// <summary>
    /// Frm_OPCTest.xaml 的交互逻辑
    /// </summary>
    public partial class Frm_OPCTest : Window
    {
        public Frm_OPCTest()
        {
            InitializeComponent();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void btn_Read_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int[] serverHandle = new int[1];
                string address = txt_Adress.Text;
                OPCHelp help = new OPCHelp("192.168.10.100");
                if (help.OpenConn() == false)
                {
                    MessageBox.Show("打开PLC连接失败");
                    return;
                }
                help.CreateGroup("test");
                serverHandle[0] = help.AddAddr(address, 1);
                Array temp_Array = help.ReadData(serverHandle);
                //按照 OPC 规范,数组始终以索引 1 开始。
                lab_Value.Content = temp_Array.GetValue(1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btn_Write_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}