FrmDischargeLine.cs 12.0 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
{
    internal partial class FrmDischargeLine : FrmEquipBase
    {
        private bool IsLoad = false; 
        private DischargeLine equipBean; 
        internal FrmDischargeLine(DischargeLine moveEquip)
        {
            equipBase = moveEquip;
            this.equipBean = moveEquip;
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        }
       
        private void LoadIOList()
        {
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = equipBean.Config.DIList.Count;
            foreach (ConfigIO ioValue in equipBean.Config.DIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
                IOTextControl control = new IOTextControl(ioValue.ElectricalDefinition + "_" + ioValue.Explain, ioValue.ProName);
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = equipBean.Config.DOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in equipBean.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>(equipBean.Config.DOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                if (!this.Visible)
                {
                    return;
                }
                if (chbAutoRead.Checked && this.Visible)
                {
                    ReadIOList();
                    lblTrayNum.Text = "托盘编码:" + TrayManager.GetNum(equipBean.DeviceID);
                } 
               
                string canOut = LineManager.Line.CanOutStore(equipBean.DeviceID) ? "可出库":"不可出库" ;
                lblStoreStatus.Text = KTK_Store.GetRunStr(equipBean.lineStatus, equipBean.runStatus)+"("+canOut+")";
                lblThisSta.Text = equipBean.WarnMsg;
                string text = "";
                List<InOutParam> ps = new List<InOutParam>(equipBean.waitOutStoreList);
                if (ps.Count > 0)
                {
                    text = "等待出料列表:\r";
                    foreach (InOutParam param in ps)
                    {
                        text += param.ToStr() + "\r";
                    }
                }
                if (!lblInstoreList.Text.Equals(text))
                {
                    lblInstoreList.Text = text;
                }
            }catch(Exception ex)
            {
                LogUtil.error(equipBean.Name+ "界面定时器出错:"+ex.ToString());
            }

        }

        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 btnOpenDo_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.HIGH);
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            WriteDO(IO_VALUE.LOW);
        }
        private void WriteDO(IO_VALUE value)
        {
            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 (equipBean.Config.DOList.ContainsKey(text))
            {
                ConfigIO io = equipBean.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.IO_IP;
                    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)
        {
            chbDebug.Checked = equipBean.IsDebug;
           
            lblStoreStatus.Text = KTK_Store.GetRunStr(equipBean.lineStatus, equipBean.runStatus);
            lblThisSta.Text = equipBean.WarnMsg;
            
            IsLoad = 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();
        } 
         
        internal void FormStatus(bool isStart)
        {
            btnStart.Enabled = !isStart;
            btnStop.Enabled = true;
            btnReset.Enabled = isStart;
            //btnInStore.Enabled = isStart;
            btnOutStore.Enabled = isStart;
        }

     

        private void FrmIOStatus_Shown(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (equipBean.runStatus.Equals(LineRunStatus.Wait))
            {
                bool result = equipBean.StartRun(true);
                if (result)
                {
                    FormStatus(true);
                }
            }
        }

        private void chbDebug_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }

            if (chbDebug.Checked)
            {
                if (!equipBean.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到调试状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        equipBean.IsDebug = true;
                        equipBean.Config.IsDebug = 1; 
                        LineManager.SaveDischargeLineConfig(equipBean.Config);
                        equipBean.ChangeDebug(true);
                        LogUtil.info(equipBean.Name+ "用户切换到调试状态 ");
                    }
                }
            }
            else
            {
                if (equipBean.IsDebug)
                {
                    DialogResult result = MessageBox.Show("是否切换到正常工作状态?", "是否确认切换", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (result.Equals(DialogResult.Yes))
                    {
                        equipBean.IsDebug = false;
                        equipBean.Config.IsDebug = 0;
                        LineManager.SaveDischargeLineConfig(equipBean.Config);
                        equipBean.ChangeDebug(false);
                        LogUtil.info(equipBean.Name + "用户切换到正常工作状态 ");
                    }
                }
            }
        }

        private void btnOutStore_Click(object sender, EventArgs e)
        {
            if (equipBean.runStatus .Equals(LineRunStatus.Runing))
            { 
                equipBean.StartOutStoreMove(new InOutParam( ));
            }
            else
            {
                MessageBox.Show("未启动或不在空闲中,无法入库测试!");
            }
        }

      

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                equipBean.StopMove();
            }
            catch (Exception ex)
            {
                LogUtil.error( "出错:" + ex.StackTrace);
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (equipBean.runStatus>(LineRunStatus.Wait))
            {
                equipBean.Reset();
            }
        }
     
       
        private void btnCloseAll_Click(object sender, EventArgs e)
        { 
            IOManager.CloseDeviceDO(new List<ConfigIO>(equipBean.Config.DOList.Values));
            foreach (Control con in groupBox5.Controls)
            {
                if (con is Button)
                {
                    con.BackColor = BackColor = Color.White;
                }
            }
        }
        private void btnStop1Up_Click(object sender, EventArgs e)
        {
            BtnMove(btnStop1Up, "定位气缸上升", "定位气缸下降", IO_Type.SeparateDevice_Down, IO_Type.SeparateDevice_Up); 
        }
        private void btnRun1_Click(object sender, EventArgs e)
        { 
            BtnMove(btnRun1, "皮带线1运转", "皮带线1停止", IO_Type.DLine_Run1);
        }

        private void btnRun2_Click(object sender, EventArgs e)
        {
            BtnMove(btnRun2, "皮带线2运转", "皮带线2停止", IO_Type.DLine_Run2); 
        }

        private void btnRun3_Click(object sender, EventArgs e)
        {
            BtnMove(btnRun3, "皮带线3运转", "皮带线3停止", IO_Type.DLine_Run3); 
        }

        private void btnSRun_Click(object sender, EventArgs e)
        { 
            BtnMove(btnRun3, "分盘装置电机1驱动", "分盘装置电机1停止", IO_Type.SeparateDevice_Run);
        }
    }

}