HMI.cs 4.36 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;

namespace FLY.Simulation.Blowing
{
    public class HMI:INotifyPropertyChanged
    {
        const int ADDR_M_HasElectricCurrent = 0;//bit 三相电 电流
        const int ADDR_M_HasFan = 1;//bit 风机启动
        
        const int ADDR_D_ChannelCnt = 0;
        const int ADDR_D_HeatUpdate = 1;
        const int ADDR_D_CurrUpdate = 2;
        
        const UInt16 ADDR_D_Heats = 100;
        const UInt16 ADDR_D_Currs = 300;

潘栩锋's avatar
潘栩锋 committed
22 23 24
        public UInt16 HeatUpdate { get; set; }

        public UInt16 CurrUpdate { get; set; }
潘栩锋's avatar
潘栩锋 committed
25 26 27 28 29


        /// <summary>
        /// 当前电流 有没?
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
30
        public bool HasElectricCurrent { get; set; }
潘栩锋's avatar
潘栩锋 committed
31 32 33 34

        /// <summary>
        /// 风机是否启动?
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
35
        public bool HasFan { get; set; } = true;
潘栩锋's avatar
潘栩锋 committed
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 152

        UInt16 heatupdate = 1;
        UInt16 currupdate = 1;
        UInt16 heatupdate_last = 1;
        UInt16[] heats;
        bool[] Bads;
        public Blowing.AirRing mAirRing;
        FLY.ModbusModule.ClientTCP mbclient;



        public HMI() 
        {
            mbclient = new FLY.ModbusModule.ClientTCP(IPAddress.Parse("127.0.0.1"));

            heats = new UInt16[100];
            Bads = new bool[100];
            Array.Clear(heats, 0, 100);
            Array.Clear(Bads, 0, 100);

            Bads[2] = true;
            Bads[30] = true;
            heatupdate = 1;
            heatupdate_last = 1;
            currupdate = 1;

            mbclient.PropertyChanged += new PropertyChangedEventHandler(mbclient_PropertyChanged);

            //每1秒读取一次
            FObjBase.PollModule.Current.Poll_Config(FObjBase.PollModule.POLL_CONFIG.ADD, 
                new FObjBase.PollModule.PollHandler(OnPoll),TimeSpan.FromMilliseconds(200) );
        }

        void mbclient_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsConnected") 
            {
                if (mbclient.IsConnected)
                {
                    step = 0;
                }
                else 
                {
                    step = -1;
                }

            }
        }
        int step = -1;
        void OnPoll() 
        {
            switch (step)
            {
                case 0:
                    mbclient.Do_03(ADDR_D_HeatUpdate, 1, delegate(UInt16[] datas, object AsyncState)
                    {
                        HeatUpdate = datas[0];
                        step = 2;
                    }, null);
                    step = 1;
                    break;
                case 2:
                    mbclient.Do_03(ADDR_D_Heats, 100, delegate(UInt16[] datas, object AsyncState)
                    {
                        Array.Copy(datas, heats, 100);                        
                        step = 4;
                    }, null);
                    step = 3;
                    break;
                case 4:
                    OnPoll_send();
                    step = 0;
                    break;
            }
        }
        
        void SendHeats() 
        {
            mbclient.Do_10(ADDR_D_Currs,heats); 
        }
        void SendUpdate() 
        {
            mbclient.Do_10(ADDR_D_CurrUpdate, new ushort[]{ CurrUpdate});
        }
        void SendStatue() 
        {
            mbclient.Do_0F(ADDR_M_HasElectricCurrent, new bool[] { HasElectricCurrent, HasFan });
        }
        void OnPoll_send() 
        {
            if (heatupdate_last != HeatUpdate) 
            {
                heatupdate_last = HeatUpdate;

                for (int i = 0; i < mAirRing.ChannelCnt; i++) 
                {
                    mAirRing.Heats[i] = heats[i];
                }
                mAirRing.HeatApply();
                SendHeats();

                bool current = false;
                for (int i = 0; i < mAirRing.ChannelCnt; i++)
                {
                    if ( (Bads[i]==false) &&  (mAirRing.Heats[i]>0))
                    {
                        current = true;
                        break;
                    }
                }
                HasElectricCurrent = current;
                CurrUpdate++;
                SendUpdate();

            }
            SendStatue();
        }
潘栩锋's avatar
潘栩锋 committed
153

潘栩锋's avatar
潘栩锋 committed
154 155 156
        public event PropertyChangedEventHandler PropertyChanged;
    }
}