MainWindow.xaml.cs 4.07 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Net;
using FLY.ModbusMapper;
using System.Reflection;

namespace WpfApplication_ModbusMapper_Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ClientTCP clienttcp;
        ModbusMapper_Client mapper;
        ABC abc;
        public MainWindow()
        {
            InitializeComponent();
            Init();
        }
        void Init() 
        {
            abc = new ABC();
            clienttcp = new FLY.ModbusMapper.ClientTCP(new IPEndPoint(IPAddress.Parse("192.168.1.21"), 502));
            mapper = new FLY.ModbusMapper.ModbusMapper_Client(clienttcp);
            
            
            //建立关系表
            mapper.MapDataToRegs(PLCAddressArea.Register, 0, REG_TYPE.FLOAT, 1, 
                abc, "A");

            mapper.MapDataToRegs(PLCAddressArea.Register, 0, REG_TYPE.BOOL, 1,
                abc, "B");

            mapper.MapDataToRegs(PLCAddressArea.Register, 2, REG_TYPE.UINT16, 1,
                abc, "C");

            

            abc.PropertyChanged += new PropertyChangedEventHandler(abc_PropertyChanged);

            mapper.NameDataChanged += new NameDataChangedEventHandler(mapper_NameDataChanged);

            this.DataContext = abc;
            this.textblock_connect.DataContext = clienttcp;

            FObjBase.PollModule.Current.Start();

            mapper.PlanAdd(this, mapper.FindDataMap(abc, "A"));
            mapper.PlanAdd(this, mapper.FindDataMap(abc, "B"));
            mapper.PlanAdd(this, mapper.FindDataMap(abc, "C"));
            mapper.PlanMake();

        }

        void mapper_NameDataChanged(object sender, NameDataChangedEventArgs e)
        {
            if (e.Owener != abc)
                return;

            //这是从HMI 更新过来的,不需要向HMI写入
            abc.PropertyChanged -= abc_PropertyChanged;

            Type t = abc.GetType();
            PropertyInfo pt = t.GetProperty(e.PropertyName);
            pt.SetValue(abc, mapper.GetNameData(abc, e.PropertyName),null);
            abc.PropertyChanged += abc_PropertyChanged;
        }

        void abc_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Type t = sender.GetType();
            PropertyInfo pt = t.GetProperty(e.PropertyName);
            object val = pt.GetValue(sender, null);
            mapper.SetNameData(sender, e.PropertyName, val);
        }

    }
    public class ABC:INotifyPropertyChanged
    {
        private float a;
        public float A 
        {
            get {
                return a;
            }
            set {
                if (a != value)
                {
                    a = value;
                    NotifyPropertyChanged("A");
                }
            }
        }

        private bool b;
        public bool B
        {
            get
            {
                return b;
            }
            set
            {
                if (b != value)
                {
                    b = value;
                    NotifyPropertyChanged("B");
                }
            }
        }

        private UInt16 c;
        public UInt16 C
        {
            get
            {
                return c;
            }
            set
            {
                if (c != value)
                {
                    c = value;
                    NotifyPropertyChanged("C");
                }
            }
        }

        protected void NotifyPropertyChanged(string propertyname) 
        {
            if (PropertyChanged != null) {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}