FrmIoManager.cs 11.7 KB
using log4net;

using URSoldering.Common;
using URSoldering.DeviceLibrary;
using System;
using System.Collections.Generic; 
using System.Reflection; 
using System.Windows.Forms;
using UserFromControl;
using URSoldering.LoadCSVLibrary;

namespace URSoldering.Client
{
    public partial class FrmIoManager : FrmBase
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public FrmIoManager()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        } 
        private void FrmMain_Load(object sender, EventArgs e)
        { 
            LoadIOList();
            timer1.Enabled = true;  
        }
        
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
          
        } 

        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private void LoadIOList()
        {
            int roleindex = 0;
            this.tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.RowCount = RobotManager.robotConfig.RobotDIList.Count;
            foreach (ConfigIO ioValue in RobotManager.robotConfig.RobotDIList.Values)
            {
                this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
                IOTextControl control = new IOTextControl();
                control.IOName = ioValue.ElectricalDefinition + "_" + ioValue.Explain;
                control.IOValue = 0;
                control.isCanClick = false;
                control.Name = "IO_" + ioValue.DisplayStr;
                control.Size = new System.Drawing.Size(200, 28);
                control.TabIndex = 0;
                this.tableLayoutPanel1.Controls.Add(control, 0, roleindex);
                roleindex++;
                DIControlList.Add(ioValue.ProName, control);
            }

            tableLayoutPanel2.RowStyles.Clear();
            this.tableLayoutPanel2.RowCount = RobotManager.robotConfig.RobotDOList.Count;
            roleindex = 0;
            foreach (ConfigIO ioValue in RobotManager.robotConfig.RobotDOList.Values)
            {
                this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
                IOTextControl control = new IOTextControl();
                control.IOName = ioValue.ElectricalDefinition + "_" + ioValue.Explain;
                control.IOValue = 0;
                control.isCanClick = true;
                //control.Location = new System.Drawing.Point(0, 25*roleindex);
                control.Name = "IO_" + ioValue.DisplayStr;
                control.Size = new System.Drawing.Size(200, 28);
                control.TabIndex = 0;
                this.tableLayoutPanel2.Controls.Add(control, 0, roleindex);
                roleindex++;
                DOControlList.Add(ioValue.ProName, control);
            }
            this.SuspendLayout();    //此处为不闪屏,一定要有的! 

            cmbWriteIO.DataSource = new List<ConfigIO>(RobotManager.robotConfig.RobotDOList.Values);
            cmbWriteIO.ValueMember = "ProName";
            cmbWriteIO.DisplayMember = "DisplayStr";
          
        }
        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)KNDManager.GetIOValue(RobotManager.robotConfig.RobotDIList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            } foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)KNDManager.GetIOValue(RobotManager.robotConfig.RobotDOList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }
        private void btnWriteSingleDO_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;
            int index = FormUtil.GetIntValue(txtDOIndex);
            IO_VALUE value = IO_VALUE.LOW;
            if (rdoHigh.Checked)
            {
                value = IO_VALUE.HIGH;
            }
            int time = FormUtil.GetIntValue(txtWriteTime);
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            if (time > 0)
            {
                KNDManager.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value, time);
            }
            else
            {
                KNDManager.WriteSingleDO(deviceName, (byte)slaveId, (ushort)index, (IO_VALUE)value);
            }
        }

        private ConfigIO GetSelectDO()
        {
            string text = cmbWriteIO.SelectedValue.ToString();
            if (RobotManager.robotConfig.RobotDOList.ContainsKey(text))
            {
                ConfigIO io = RobotManager.robotConfig.RobotDOList[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.ProVale.ToString();
                    txtDoName.Text = io.DeviceName;
                    txtSlaveId.Text = io.SlaveID.ToString();
                    if (selectControl != null)
                    {
                        selectControl.BorderStyle = System.Windows.Forms.BorderStyle.None;
                    }
                    IOTextControl newControl = DOControlList[io.ProName];
                    newControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    selectControl = newControl;
                }
            }
        }

        private void btnReadAllDi_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text; 
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            KNDManager.ReadMultipleDI(deviceName, (byte)slaveId, (ushort)KNDManager.DIStartAddress, 16);
        }

        private void btnReadAllDo_Click(object sender, EventArgs e)
        {
            string deviceName = txtDoName.Text;  
            int slaveId = FormUtil.GetIntValue(txtSlaveId);
            KNDManager.ReadMultipleDO(deviceName, (byte)slaveId, (ushort)KNDManager.DoStartAddress, 16);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (RobotBean.ShuddenOK().Equals(false))
            {
                lblMsg.Text = "急停未开";
                if (this.btnSendWireStop.Enabled)
                {
                    FormStatus(false); 
                }
            }
            else if (RobotBean.IsFixtureClamp().Equals(false))
            {

                lblMsg.Text = "夹具未夹紧";
                if (this.btnSendWireStop.Enabled)
                {
                    FormStatus(false);
                }
            }
            else
            {
                lblMsg.Text = "";
                if (this.btnSendWireStop.Enabled == false)
                {
                    FormStatus(true);
                }
            }
            if (chbAutoRead.Checked)
            {
                ReadIOList();
            } 
        }

        private void FormStatus(bool isOpen)
        {
            this.btnSendWireStop.Enabled = isOpen;

            this.btnWDown.Enabled = isOpen;
            this.btnWUp.Enabled = isOpen;
            this.btnTestSend.Enabled = isOpen;
            this.btnSendWire.Enabled = isOpen;
            this.btnStopSend.Enabled = isOpen;
            this.btnWStop.Enabled = isOpen;
            groupBox4.Enabled = isOpen;
            btnFixtureStop.Enabled = isOpen;
            btnWireWork.Enabled = isOpen;
        }

        private void btnSendWire_Click(object sender, EventArgs e)
        { 
            double speed = FormUtil.getDoubleValue(txtSpeed);
            double length = FormUtil.getDoubleValue(txtSendWireLength);
            SendWireManager.setLength(length);
            SendWireManager.setSpeed(speed);
            SendWireManager.StartFSend(); 
        }

        private void btnStopSend_Click(object sender, EventArgs e)
        {
            SendWireManager.StopSend();
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        } 
        private void btnTestSend_Click(object sender, EventArgs e)
        {
            int speed = FormUtil.GetIntValue(txtSpeed);
            int length = FormUtil.GetIntValue(txtSendWireLength);
            SendWireManager.setLength(length);
            SendWireManager.setSpeed(speed);
            SendWireManager.StartBSend();
        }
         

        private void btnCloseForm_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            SendWireCylinderMove(IO_VALUE.LOW, IO_VALUE.LOW);
        }
        
        private void SolderingCylinderMove(IO_VALUE down,IO_VALUE up)
        {
            RobotBean.KNDIOMove(IO_Type.Soldering_Down, down);
            RobotBean.KNDIOMove(IO_Type.Soldering_Up, up);
        }
        private void FixtureCylinderMove(IO_VALUE clamp, IO_VALUE relax)
        {
            RobotBean.KNDIOMove(IO_Type.Fixture_Clamp, clamp);
            RobotBean.KNDIOMove(IO_Type.Fixture_Relax, relax);
        }
        private void  SendWireCylinderMove(IO_VALUE work, IO_VALUE clear)
        {
            RobotBean.KNDIOMove(IO_Type.SendWire_Work, work);
            RobotBean.KNDIOMove(IO_Type.SendWire_Clear, clear);
        }
        private void btnWDown_Click(object sender, EventArgs e)
        {
            SolderingCylinderMove(IO_VALUE.HIGH, IO_VALUE.LOW);
        }

        private void btnWUp_Click(object sender, EventArgs e)
        {
            SolderingCylinderMove(IO_VALUE.LOW, IO_VALUE.HIGH); 
        }

        private void btnWStop_Click(object sender, EventArgs e)
        {
            SolderingCylinderMove(IO_VALUE.LOW, IO_VALUE.LOW); 
        }

        private void btnFixtureClamp_Click(object sender, EventArgs e)
        {
            FixtureCylinderMove(IO_VALUE.HIGH, IO_VALUE.LOW);
        }

        private void btnFixtureReleax_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定执行夹具放松操作?","提示",MessageBoxButtons.OKCancel);
            if (result.Equals(DialogResult.OK))
            {
                FixtureCylinderMove(IO_VALUE.LOW, IO_VALUE.HIGH);
            }
        }

        private void btnFixtureStop_Click(object sender, EventArgs e)
        { DialogResult result = MessageBox.Show("确定执行夹具停止输出操作?", "提示", MessageBoxButtons.OKCancel);
            if (result.Equals(DialogResult.OK))
            {
                FixtureCylinderMove(IO_VALUE.LOW, IO_VALUE.LOW);
            }
        }

        private void btnWireWork_Click(object sender, EventArgs e)
        {
            SendWireCylinderMove(IO_VALUE.HIGH, IO_VALUE.LOW);
        }

        private void btnWireClear_Click(object sender, EventArgs e)
        {
            SendWireCylinderMove(IO_VALUE.LOW, IO_VALUE.HIGH);
        }
    }
}