FrmLineIO.cs 12.8 KB
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 System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using OnlineStore.DeviceLibrary;
using log4net;
using System.Reflection;
using UserFromControl;
using OnlineStore.LoadCSVLibrary;
using OnlineStore.Common;


namespace OnlineStore.ACSingleStore
{
    internal partial class FrmLineIO : FrmBase
    {
        internal FrmLineIO()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int count = 18;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = count;

            foreach (ConfigIO ioValue in EquipManager.Config.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName, 230, 28);

                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);

                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }
            count = 19;
            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = count;
            roleindex = 0;
            foreach (ConfigIO ioValue in EquipManager.Config.DOList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName, 220, 28);
                control.Click += Control_Click;

                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);

                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(EquipManager.Config.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
        }
        private void Control_Click(object sender, EventArgs e)
        {
            IOTextControl control = (IOTextControl)sender;
            string name = control.Name.Substring(3, control.Name.Length - 3);
            List<string> keyList = new List<string>(DOControlList.Keys);
            int index = keyList.IndexOf(name);
            if (index >= 0)
            {
                cmbWriteIO.SelectedIndex = index;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        { 
            if (this.Visible)
            { 
                ReadIOList();
                ReadBtnDO();
            }
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        { 
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error("", ex);
            }
        }


        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (EquipManager.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = EquipManager.Config.DOList[text];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
        private void WriteDO(IO_VALUE value)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            int time = FormUtil.GetIntValue(txtWriteTime);

            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value);
            }
        }
 
        private void cmbWriteIO_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
            {
                return;
            }
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (cmbWriteIO.Items.Count > e.Index)
            {
                ConfigIO io = (ConfigIO)cmbWriteIO.Items[e.Index];
                e.Graphics.DrawString(io.DisplayStr, e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
            }
        }

        private void btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOManager.IOValue(key, 0);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)IOManager.DOValue(key, 0);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }
        protected List<Control> doList = new List<Control>();
        protected void LoadDOBtn(GroupBox group)
        {
            doList = new List<Control>();
            foreach (Control c in group.Controls)
            {
                if (c is Button)
                {
                    if (EquipManager.Config.DOList.ContainsKey(c.Name))
                    {
                        doList.Add(c);
                    }
                    else if (c.Name.ToUpper().StartsWith("BTN"))
                    {
                        c.Visible = true;
                    }
                    else
                    {
                        c.Visible = false;
                    }
                }
            }
        }
        protected void ReadBtnDO()
        {
            foreach (Control labl in doList)
            {
                IO_VALUE value = IOManager.DOValue(labl.Name,0 );
                if (value.Equals(IO_VALUE.HIGH))
                {
                    labl.BackColor = Color.Lime;
                }
                else
                //else if (value.Equals(IO_VALUE.LOW))
                {
                    labl.BackColor = labl.Parent.BackColor;
                }
            }
        }
        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {
            timer1.Start();
        }
       


        private void btnCloseAll_Click(object sender, EventArgs e)
        {
            IOManager.instance.CloseAllDO();
        }

        private void btnOpenDo_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }

        private void btnCloseDO_Click(object sender, EventArgs e)
        {

            WriteDO(IO_VALUE.LOW);
        }

        private void cmbWriteIO_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbWriteIO.SelectedIndex >= 0)
            {
                ConfigIO io = GetSelectDO();
                if (io != null)
                {
                    // txtIp.Text = io.DeviceName;
                    txtDOIndex.Text = io.GetIOAddr().ToString();
                    txtDoName.Text = io.IO_IP;
                    lblAddr.Text = txtDoName.Text + "_" + txtDOIndex.Text;
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null)
                    {
                        selectControl.BorderStyle = BorderStyle.None;
                        selectControl.BackColor = Color.White;
                    }
                    newControl.BorderStyle = BorderStyle.FixedSingle;
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                }
            }
        }

        private void FrmLineIO_Load(object sender, EventArgs e)
        {
            LoadIOList();
            LoadDOBtn(groupBox1);
        }

        private void S1_LineRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S1_LineRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S1_LineBackRun, IO_Type.S1_LineRun);
        }

        private void S1_LineBackRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S1_LineBackRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S1_LineRun, IO_Type.S1_LineBackRun);
        }

        private void btnLineStopS1_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + btnLineStopS1.Text);
            EquipManager.Equip.IOMove(IO_Type.S1_LineRun, IO_VALUE.LOW);
            EquipManager.Equip.IOMove(IO_Type.S1_LineBackRun, IO_VALUE.LOW);
        }

        private void S1_LocationCylinder_Up_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S1_LocationCylinder_Up.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S1_LocationCylinder_Down, IO_Type.S1_LocationCylinder_Up);
        }

        private void S1_LocationCylinder_Down_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S1_LocationCylinder_Down.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S1_LocationCylinder_Up, IO_Type.S1_LocationCylinder_Down);
        }


        private void S2_LineRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S2_LineRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S2_LineBackRun, IO_Type.S2_LineRun);
        }

        private void S2_LineBackRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S2_LineBackRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S2_LineRun, IO_Type.S2_LineBackRun);
        }

        private void btnLineStopS2_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + btnLineStopS2.Text);
            EquipManager.Equip.IOMove(IO_Type.S2_LineRun, IO_VALUE.LOW);
            EquipManager.Equip.IOMove(IO_Type.S2_LineBackRun, IO_VALUE.LOW);
        }

        private void S2_LocationCylinder_Up_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S2_LocationCylinder_Up.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S2_LocationCylinder_Down, IO_Type.S2_LocationCylinder_Up);
        }

        private void S2_LocationCylinder_Down_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S2_LocationCylinder_Down.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S2_LocationCylinder_Up, IO_Type.S2_LocationCylinder_Down);
        }


        private void S3_LineRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S3_LineRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S3_LineBackRun, IO_Type.S3_LineRun);
        }

        private void S3_LineBackRun_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S3_LineBackRun.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S3_LineRun, IO_Type.S3_LineBackRun);
        }

        private void btnLineStopS3_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + btnLineStopS3.Text);
            EquipManager.Equip.IOMove(IO_Type.S3_LineRun, IO_VALUE.LOW);
            EquipManager.Equip.IOMove(IO_Type.S3_LineBackRun, IO_VALUE.LOW);
        }

        private void S3_LocationCylinder_Up_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S3_LocationCylinder_Up.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S3_LocationCylinder_Down, IO_Type.S3_LocationCylinder_Up);
        }

        private void S3_LocationCylinder_Down_Click(object sender, EventArgs e)
        {
            LogUtil.info("界面点击:" + S3_LocationCylinder_Down.Text);
            EquipManager.Equip.CylinderMove(IO_Type.S3_LocationCylinder_Up, IO_Type.S3_LocationCylinder_Down);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }


}