FrmIoManager.cs 11.9 KB
using log4net;

using TSA_V.Common;
using TSA_V.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserFromControl;

namespace TSA_V
{
    public partial class FrmIoManager : FrmBase
    {
        public static FrmIoManager instance = new FrmIoManager();
        private FrmIoManager()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            LanguageProcess();
            //if (IOManager.ISConnection() == false)
            //{
            //    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.IOConnectError,"IO模块连接失败!")); 
            //}

            LoadIOList();
            timer1.Enabled = true;
            SetScreen();

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            FrmMaintenance.instance.Visible = true;
            e.Cancel = true; 
        }

        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            DIControlList = new Dictionary<string, IOTextControl>();
            DOControlList = new Dictionary<string, IOTextControl>();
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = IOManager.DIList.Count;
            foreach (ConfigIO ioValue in IOManager.DIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
                IOTextControl control = new IOTextControl(ioValue.ProName + "_" + ioValue.Explain, ioValue.ProName);
                control.Click += Control_Click;
                //IOTextControl control = new IOTextControl();
                //if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                //{
                //    control.IOName = ioValue.IOIndex + "_" + ioValue.ProName;
                //}
                //else
                //{
                //    control.IOName = ioValue.IOIndex + "_" + ioValue.Explain;
                //}
                //control.IoType = ioValue.ProName;
                //control.IOValue = 0;
                //control.isCanClick = false;
                //control.Name = "IO_" + ioValue.ProName;
                //control.Size = new System.Drawing.Size(300, 25);
                //control.TabIndex = 0;
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = IOManager.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in IOManager.DOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
                IOTextControl control = new IOTextControl(ioValue.ProName + "_" + ioValue.Explain, ioValue.ProName);
                control.Click += Control_Click;
                //IOTextControl control = new IOTextControl);
                //if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                //{
                //    control.IOName = ioValue.IOIndex + "_" + ioValue.ProName;
                //}
                //else
                //{(
                //    control.IOName = ioValue.IOIndex + "_" + ioValue.Explain;
                //}
                //control.IoType = ioValue.ProName;
                //control.IOValue = 0;
                //control.isCanClick = true;
                ////control.Location = new System.Drawing.Point(0, 25*roleindex);
                //control.Name = "IO_" + ioValue.ProName;
                //control.Size = new System.Drawing.Size(300, 25);
                //control.TabIndex = 0;
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 
            LoadComList(); 
        }

        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 btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)IOBase.instance.GetIOValue(IOManager.DIList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            } foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)IOBase.instance.GetIOValue(IOManager.DOList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }
    
        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (IOManager.DOList.ContainsKey(text))
            {
                ConfigIO io = IOManager.DOList[text];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
       
   

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (chbAutoRead.Checked)
            {
                ReadIOList();
            }
        }
         
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        } 
        private void btnCloseForm_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         

     

        private void FrmIoManager_Shown(object sender, EventArgs e)
        {
            SetSkin(this); 
            lblMsg.ForeColor = Color.Red;
        }

     
       
  
        private void FrmIoManager_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                try
                {
                    IOProcess();
                }catch(Exception ex)
                {

                }
            }
        }

        private void IOProcess()
        {
            foreach (ConfigIO ioValue in IOManager.DIList.Values)
            {
                if (DIControlList.ContainsKey(ioValue.ProName))
                {
                    IOTextControl control = DIControlList[ioValue.ProName];
                    if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                    {
                        control.IOName = ioValue.Explain.Substring(0,3) + "_" + ioValue.ProName;
                    }
                    else
                    {
                        control.IOName =   ioValue.Explain;
                    }
                    control.ShowData();
                }
            } 
            foreach (ConfigIO ioValue in IOManager.DOList.Values)
            {
                if (DOControlList.ContainsKey(ioValue.ProName))
                {
                    IOTextControl control = DOControlList[ioValue.ProName];
                    if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
                    {
                        control.IOName = ioValue.Explain.Substring(0, 3) + "_" + ioValue.ProName;
                    }
                    else
                    {
                        control.IOName =   ioValue.Explain;
                    }
                    control.ShowData();
                }
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 
            LoadComList();
        }

        private void LoadComList()
        {
            cmbWriteIO.DataSource = null;
            cmbWriteIO.Items.Clear();
            cmbWriteIO.DataSource = new List<ConfigIO>(IOManager.DOList.Values);
            cmbWriteIO.ValueMember = "ProName"; 
            cmbWriteIO.DisplayMember = "DisplayStr"; 
            cmbWriteIO.SelectedIndex = 0; 
        }

   

        private void cmbWriteIO_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (cmbWriteIO.SelectedIndex >= 0)
            {
                ConfigIO io = GetSelectDO();
                if (io != null)
                {
                    // txtIp.Text = io.DeviceName;
                    txtDOIndex.Text = io.IOIndex.ToString();
                    txtDoName.Text = io.DeviceName; 
                    if (selectControl != null)
                    {
                        selectControl.BackColor=Color.White;
                        selectControl.BorderStyle = System.Windows.Forms.BorderStyle.None;
                    }
                    IOTextControl newControl = DOControlList[io.ProName];
                    newControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    newControl.BackColor = Color.LightBlue;
                    selectControl = newControl;
                }
            }
        }
        private void WriteDO(IO_VALUE value)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            int time = FormUtil.GetIntValue(txtWriteTime);

            if (time > 0)
            {
                IOBase.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value, time);
            }
            else
            {
                IOBase.instance.WriteSingleDO(deviceName, (byte)0, (ushort)index, value);
            }
        }

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

        private void button1_Click(object sender, EventArgs e)
        {

            WriteDO(IO_VALUE.LOW);
        }

        private void btnSideForward_Click(object sender, EventArgs e)
        {
            TSAVBean.SideCyliderForward();
        }

        private void btnSideBack_Click(object sender, EventArgs e)
        {
            TSAVBean.SideCyliderBack();
        }

        private void btnBottomUp_Click(object sender, EventArgs e)
        { 
            TSAVBean.BottomCylinderUp();
        }

        private void btnBottomDown_Click(object sender, EventArgs e)
        { 
            TSAVBean.BottomCylinderDown();
        }
        private void btnLineF_Click(object sender, EventArgs e)
        {
            IOManager.IOMove(IOManager.Line_FStart, IO_VALUE.HIGH);
        }
        private void btnLineStop_Click(object sender, EventArgs e)
        {
            IOManager.IOMove(IOManager.Line_FStart, IO_VALUE.LOW);
        }

        private void btnStopUp_Click(object sender, EventArgs e)
        {
            TSAVBean.CylinderMove(IOManager.StopCylinder_Down, IOManager.StopCylinder_Up); 
        }

        private void btnStopDown_Click(object sender, EventArgs e)
        { 
            TSAVBean.CylinderMove(IOManager.StopCylinder_Up, IOManager.StopCylinder_Down); 
        }

    }
}