HMI.cs 5.47 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Net;
7
using System.IO;
潘栩锋's avatar
潘栩锋 committed
8 9 10

namespace FLY.Simulation.Blowing
{
11
    public class HMI: IPlcLink
潘栩锋's avatar
潘栩锋 committed
12 13 14 15 16 17 18 19 20 21 22
    {
        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
23 24 25
        public UInt16 HeatUpdate { get; set; }

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


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

        /// <summary>
        /// 风机是否启动?
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
36
        public bool HasFan { get; set; } = true;
37
        public string PlcAddr { get; set; } = "127.0.0.1";
潘栩锋's avatar
潘栩锋 committed
38 39 40 41 42 43 44 45 46 47 48 49

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



        public HMI() 
        {
50 51 52

            if (!Load())
                Save();
潘栩锋's avatar
潘栩锋 committed
53 54

            heats = new UInt16[100];
55

潘栩锋's avatar
潘栩锋 committed
56 57
            Array.Clear(heats, 0, 100);

58 59


潘栩锋's avatar
潘栩锋 committed
60 61 62 63 64
            heatupdate = 1;
            heatupdate_last = 1;
            currupdate = 1;


65

潘栩锋's avatar
潘栩锋 committed
66
        }
67 68 69 70 71
        public void Init(Blowing.AirRing mAirRing) 
        {
            this.mAirRing = mAirRing;

            mbclient = new FLY.ModbusModule.ClientTCP(IPAddress.Parse(PlcAddr));
潘栩锋's avatar
潘栩锋 committed
72

73 74 75 76 77
            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));
        }
潘栩锋's avatar
潘栩锋 committed
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
        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();

146
                HasElectricCurrent = mAirRing.HasElectricCurrent;
潘栩锋's avatar
潘栩锋 committed
147 148 149 150 151 152
                CurrUpdate++;
                SendUpdate();

            }
            SendStatue();
        }
153 154 155 156 157 158 159 160 161
        string filePath = "airRingPlc.json";
        bool Load()
        {
            return HmiJsonDb.Load(filePath, this);
        }
        public bool Save()
        {
            return HmiJsonDb.Save(filePath, this);
        }
潘栩锋's avatar
潘栩锋 committed
162

潘栩锋's avatar
潘栩锋 committed
163 164
        public event PropertyChangedEventHandler PropertyChanged;
    }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    public class HmiJsonDb
    {
        public string PlcAddr = "127.0.0.1";
        public static bool Save(string filePath, HMI plcLink)
        {
            try
            {
                HmiJsonDb jsondb = new HmiJsonDb()
                {
                    PlcAddr = plcLink.PlcAddr
                };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(jsondb, Newtonsoft.Json.Formatting.Indented);
                File.WriteAllText(filePath, json);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Load(string filePath, HMI plcLink)
        {
            try
            {
                if (!File.Exists(filePath))
                    return false;
                string json = File.ReadAllText(filePath);
                var jsonDb = Newtonsoft.Json.JsonConvert.DeserializeObject<HmiJsonDb>(json);
                plcLink.PlcAddr = jsonDb.PlcAddr;
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
潘栩锋's avatar
潘栩锋 committed
203
}