FrmIOStatus.cs 14.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.AssemblyLine
{
    public partial class FrmIOStatus : FrmBase
    {  
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmIOStatus()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        } 
        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int count = 20;
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = count;
            this.tableLayoutPanel3.RowStyles.Clear();
            if (LineManager.Config.DIList.Count > count)
            {
                this.tableLayoutPanel3.RowCount = LineManager.Config.DIList.Count - count;
            }
            else
            { 
                this.tableLayoutPanel3.RowCount =0  ;
            }
            foreach (ConfigIO ioValue in LineManager.Config.DIList.Values)
            {
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                if (DIControlList.Count > count)
                {
                    roleindex = DIControlList.Count - count-1;
                    this.tableLayoutPanel3.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                    this.tableLayoutPanel3.Controls.Add(control, 0, roleindex);                  
                }
                else
                {
                    this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28)); 
                    this.tableLayoutPanel1.Controls.Add(control, 0, roleindex); 
                }
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = LineManager.Config.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in LineManager.Config.DOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain,ioValue.ProName);
              
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(LineManager.Config.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
             
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (chbAutoRead.Checked && this.Visible)
            {
                ReadIOList();
                lblTrayNum.Text = "托盘编码:" + TrayManager.GetNum(0); 
            }
        }

        private void FrmTest_FormClosing(object sender, FormClosingEventArgs e)
        {
           // KNDAIManager.NeedShow = false;
            try
            {
                if (this.timer1.Enabled)
                {
                    this.timer1.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, "Exception(异常)", MessageBoxButtons.OK, MessageBoxIcon.Error);
                LogUtil.error(LOGGER, ex.StackTrace);
            }
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            IO_VALUE value =  checkBox1.Checked ? IO_VALUE.HIGH:IO_VALUE.LOW ;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            if (time > 0)
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (LineManager.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = LineManager.Config.DOList[text];
                return io;
            }
            return null;
        }
        IOTextControl selectControl = null;
        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.DeviceName;
                    txtSlaveId.Text = io.SlaveID.ToString();
                    IOTextControl newControl = DOControlList[io.ProName];
                    if (selectControl != null) { selectControl.BackColor = Color.White; }
                    newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                    
                }
            }
        }

        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
           // KNDAIManager.NeedShow = true;
        }

        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();
                }
            } 
        }
         
        private void btnReadAllDi_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text; 
            IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            IOManager.instance.ReadAllDI(deviceName, (byte)slaveId );
        } 
        private void btnReadAllDo_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text; 

            IO_VALUE value = checkBox1.Checked ? IO_VALUE.HIGH : IO_VALUE.LOW;
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            IOManager.instance.ReadAllDO(deviceName, (byte)slaveId);
        } 
     
        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {  
            timer1.Start();
        }

        private void btnDriveMotor_Run_Click(object sender, EventArgs e)
        {
            if (btnDriveMotor_Run.Text.Equals("驱动电机正转"))
            {
                btnDriveMotor_Run.Text = "驱动电机停止";
                btnDriveMotor_Run.BackColor = Color.Aqua;
                IOManager.IOMove(IO_Type.DriveMotor_Run, IO_VALUE.HIGH );
            }
            else
            {
                IOManager.IOMove(IO_Type.DriveMotor_Run, IO_VALUE.LOW );
                btnDriveMotor_Run.Text = "驱动电机正转";
                btnDriveMotor_Run.BackColor = Color.White;
            }
        }
         
        private void btnInTopCylinderUp_Click(object sender, EventArgs e)
        {
            if (btnInTopCylinderUp.Text.Equals("上料工位顶升气缸上升"))
            {
                LineManager.Line.CylinderMove(null, IO_Type.InStore_TopCylinder_Down, IO_Type.InStore_TopCylinder_Up);
                btnInTopCylinderUp.Text = "上料工位顶升气缸下降";
                btnInTopCylinderUp.BackColor = Color.Aqua;
            }
            else
            {
                LineManager.Line.CylinderMove(null, IO_Type.InStore_TopCylinder_Up, IO_Type.InStore_TopCylinder_Down);
                btnInTopCylinderUp.Text = "上料工位顶升气缸上升";
                btnInTopCylinderUp.BackColor = Color.SkyBlue;
            }
        } 

        private void btn12Top_Click(object sender, EventArgs e)
        {
            if (btn12Top.Text.Equals("横移轨道1.2顶升上升"))
            {
                btn12Top.Text = "横移轨道1.2顶升下降";
                btn12Top.BackColor = Color.Aqua;
                LineManager.Line.SideWay12TopCylinderMove(IO_VALUE.LOW, IO_VALUE.HIGH, null);
            }
            else
            {
                btn12Top.Text = "横移轨道1.2顶升上升"; 
                LineManager.Line.SideWay12TopCylinderMove(IO_VALUE.HIGH, IO_VALUE.LOW, null);
                btn12Top.BackColor = Color.SkyBlue;
            }
        }

        private void btn34Top_Click(object sender, EventArgs e)
        {
            if (btn34Top.Text.Equals("横移轨道3.4顶升上升"))
            {
                btn34Top.Text = "横移轨道3.4顶升下降";
                btn34Top.BackColor = Color.Aqua;
                LineManager.Line.SideWay34TopCylinderMove(IO_VALUE.LOW, IO_VALUE.HIGH, null);
            }
            else
            {
                btn34Top.Text = "横移轨道3.4顶升上升";
                btn34Top.BackColor = Color.SkyBlue;
                LineManager.Line.SideWay34TopCylinderMove(IO_VALUE.HIGH, IO_VALUE.LOW, null);
            }
        }

        private void btnSw12Run_Click(object sender, EventArgs e)
        {
            if (btnSw12Run.Text.Equals("横移轨道1.2驱动"))
            {
                IOManager.IOMove(IO_Type.SidesWay12_DriveMotor, IO_VALUE.HIGH);
                btnSw12Run.Text = "横移轨道1.2停止";
                btnSw12Run.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.SidesWay12_DriveMotor, IO_VALUE.LOW); 
                btnSw12Run.Text = "横移轨道1.2驱动";
                btnSw12Run.BackColor = Color.White;
            }
        }

       
        private void btnSw34Run_Click(object sender, EventArgs e)
        {

            if (btnSw34Run.Text.Equals("横移轨道3.4驱动"))
            {
                IOManager.IOMove(IO_Type.SidesWay34_DriveMotor, IO_VALUE.HIGH);
                btnSw34Run.Text = "横移轨道3.4停止";
                btnSw34Run.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.SidesWay34_DriveMotor, IO_VALUE.LOW);
                btnSw34Run.Text = "横移轨道3.4驱动";
                btnSw34Run.BackColor = Color.White;
            }
        }

        private void btnStop01_Click(object sender, EventArgs e)
        {
            if (btnStop01.Text.Equals("阻挡气缸0-1下降"))
            {
                IOManager.IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.HIGH);
                btnStop01.Text = "阻挡气缸0-1上升";
                btnStop01.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.StopCylinder_Down1, IO_VALUE.LOW);
                btnStop01.Text = "阻挡气缸0-1下降";
                btnStop01.BackColor = Color.White;
            }
        }

        private void btnStop02_Click(object sender, EventArgs e)
        {
            if (btnStop02.Text.Equals("阻挡气缸0-2下降"))
            {
                IOManager.IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.HIGH);
                btnStop02.Text = "阻挡气缸0-2上升";
                btnStop02.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.StopCylinder_Down2, IO_VALUE.LOW);
                btnStop02.Text = "阻挡气缸0-2下降";
                btnStop02.BackColor = Color.White;
            }

        }

        private void btnSwStop1_Click(object sender, EventArgs e)
        {
            if (btnSwStop1.Text.Equals("横移轨道1阻挡下降"))
            {
                IOManager.IOMove(IO_Type.SidesWay1_StopCylinderDown, IO_VALUE.HIGH);
                btnSwStop1.Text = "横移轨道1阻挡上升";
                btnSwStop1.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.SidesWay1_StopCylinderDown, IO_VALUE.LOW);
                btnSwStop1.Text = "横移轨道1阻挡下降";
                btnSwStop1.BackColor = Color.SkyBlue;
            }
        }

        private void btnSwStop3_Click(object sender, EventArgs e)
        {
            if (btnSwStop3.Text.Equals("横移轨道3阻挡下降"))
            {
                IOManager.IOMove(IO_Type.SidesWay3_StopCylinderDown, IO_VALUE.HIGH);
                btnSwStop3.Text = "横移轨道3阻挡上升";
                btnSwStop3.BackColor = Color.Aqua;
            }
            else
            {
                IOManager.IOMove(IO_Type.SidesWay3_StopCylinderDown, IO_VALUE.LOW);
                btnSwStop3.Text = "横移轨道3阻挡下降";
                btnSwStop3.BackColor = Color.SkyBlue;
            }
        }

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