FrmXRay.cs 13.1 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.AutoCountClient
{
    internal partial class FrmXRay : FrmEquipBase
    {
        private bool IsLoad = false;
  
        private X_RAY_Equip equipBean;
        internal FrmXRay(X_RAY_Equip moveEquip)
        {
            equipBase = moveEquip;
            this.equipBean = moveEquip;
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            LoadIOList();
        }
        private void FrmStoreIOStatus_Load(object sender, EventArgs e)
        {
            chbDebug.Checked = equipBean.IsDebug;

            lblCom.Text = "X射线端口号:" + equipBean.Config.XRay_Port;
            lblDianl.Text = "电流值:" + equipBean.Config.XRay_Electricity;
            lblDiany.Text = "电压值:" + equipBean.Config.XRay_Voltage;
            lblData.Text = "最后使用时间:" + ConfigAppSettings.GetValue(Setting_Init.XRay_Data);
            lblData.Text = equipBean.LastXRayState;
            lblName.Text = equipBean.Name;
            IsLoad = true;
        }

        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);
                control.Click += Control_Click;
                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 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)
        {
            try
            {
                if (!this.Visible)
                {
                    return;
                }
                ReadIOList();
                ReadBtnDO();
                lblData.Text = equipBean.LastXRayState;
                if (equipBean.runStatus > RobotRunStatus.Wait)
                {
                    lblName.BackColor = Color.Green;
                    if (btnStart.Enabled)
                    {
                        FormStatus(true);
                    }
                }
                else
                {
                    lblName.BackColor = System.Drawing.Color.DodgerBlue;
                    if (!btnStart.Enabled)
                    {
                        FormStatus(false);
                    }
                }
                if (equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Working) || equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Labelling))
                {
                    lblInoutInfo.Text = (equipBean.MoveInfo.MoveType.Equals(RobotMoveType.Working) ? "工作中:" : "空闲中") + equipBean.MoveInfo.MoveParam.ToStr();
                }
                else
                {
                    lblInoutInfo.Text = "";
                }
                lblMoveInfo.Text = equipBean.GetMoveStr();
                lblStoreStatus.Text = RobotBase.GetRunStr(equipBean.runStatus);
                lblThisSta.Text = equipBean.WarnMsg;
            }
            catch (Exception ex)
            {
                LogUtil.error(equipBean.Name + "界面定时器出错:", ex);
            }
        }

        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("",ex);
            }
        }
        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 IP = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);

            ConfigIO io = GetSelectDO();
            if (io.ProName.Equals(IO_Type.X_Lock_On)&&value.Equals(IO_VALUE.HIGH))
            {
                if (!equipBean.LockOnCanMove())
                {
                    MessageBox.Show("X光机互锁信号不能打开, 请先检查 急停/门禁/防护门 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            //   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(IP, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                IOManager.instance.WriteSingleDO(IP, (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.BorderStyle = BorderStyle.None; }
                    newControl.BorderStyle = BorderStyle.FixedSingle;
                   // newControl.BackColor = Color.SkyBlue;
                    selectControl = newControl;
                }
            }
        }



        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 override void FormStatus(bool isStart)
        {
            btnStart.Enabled = true;
            btnStop.Enabled = true;
            if (isStart)
            {
                btnStart.Text = "复位";
            }
            else
            {
                btnStart.Text = "启动调试";
            } 
        }


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

        private void btnStart_Click(object sender, EventArgs e)
        { 
            StartDebug(btnStart);
        }
        private void btnStop_Click(object sender, EventArgs e)
        { 
            StopRun(btnStop);
        } 
   

        private void chbDebug_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }
            if (!chbDebug.Checked.Equals(equipBean.IsDebug))
            {
                bool IsDebug = chbDebug.Checked;
                string tarStr = IsDebug ? "调试状态" : "非调试状态";

                DialogResult result = MessageBox.Show("确定切换到" + tarStr, "调试状态更改确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (result.Equals(DialogResult.Yes))
                {
                    equipBean.IsDebug = IsDebug;
                    equipBean.Config.IsDebug = IsDebug ? 1 : 0;
                    RobotManager.SaveXRayConfig(equipBean.Config);
                    equipBean.ChangeDebug(IsDebug);
                    LogUtil.info(equipBean.Name + "用户切换 调试状态 =" + IsDebug);
                }
            }
        }
 
    

        private void btnInDoorUp_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_InDoor_Down, IO_Type.X_InDoor_Up);
        } 

        private void btnInDoor_Down_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_InDoor_Up, IO_Type.X_InDoor_Down);
        }

        private void btnOutDoor_Up_Click(object sender, EventArgs e)
        { 
            equipBean.CylinderMove(null, IO_Type.X_OutDoor_Down, IO_Type.X_OutDoor_Up);
        }

        private void btnOutDoor_Down_Click(object sender, EventArgs e)
        { 
            equipBean.CylinderMove(null, IO_Type.X_OutDoor_Up, IO_Type.X_OutDoor_Down);
        } 
        private void btnCloseAll_Click(object sender, EventArgs e)
        {
             
            foreach (Control con in groupBox5.Controls)
            {
                if (con is Button)
                {
                    con.BackColor = BackColor = Color.White;
                }
            }
            foreach (ConfigIO io in equipBase.baseConfig.DOList.Values)
            {
                IOManager.instance.WriteSingleDO(io.IO_IP, io.SlaveID, io.GetIOAddr(), IO_VALUE.LOW);
            }
        }

        private void btnStopCylinder_Up_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_StopCylinder_Down, IO_Type.X_StopCylinder_Up);
        }

        private void btnStopCylinder_Down_Click(object sender, EventArgs e)
        {
            equipBean.CylinderMove(null, IO_Type.X_StopCylinder_Up, IO_Type.X_StopCylinder_Down);
        }

        private void X_Lock_On_Click(object sender, EventArgs e)
        {
            Control c = (Control)sender;
            string ioType = c.Name;
            if (ioType.Equals(IO_Type.X_Lock_On) &&( !equipBean.IOValue(IO_Type.X_Lock_On).Equals(IO_VALUE.HIGH)))
            {
                if (!equipBean.LockOnCanMove())
                {
                    MessageBox.Show("X光机互锁信号不能打开, 请先检查 急停/门禁/防护门 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            DoBtnClick(sender);
        }

        private void chbMoveStop_CheckedChanged(object sender, EventArgs e)
        {
            if (!IsLoad)
            {
                return;
            }
            equipBean.MoveStop = chbMoveStop.Checked;
            LogUtil.info(equipBean.Name + "用户切换是否暂停: " + equipBean.MoveStop);
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }
    }

}