FrmIoManager.cs 11.1 KB
using log4net;

using URSoldering.Common;
using URSoldering.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.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";
            cmbWriteValue.SelectedIndex = 0;
            cmbWriteIO.SelectedIndex = 0;
            cmbWriteIO_SelectedIndexChanged(null, null);
        }
        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)cmbWriteValue.SelectedIndex;
            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;

            IO_VALUE value = (IO_VALUE)cmbWriteValue.SelectedIndex;
            int time = FormUtil.GetIntValue(txtWriteTime);
            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;
            IO_VALUE value = (IO_VALUE)cmbWriteValue.SelectedIndex;
            int time = FormUtil.GetIntValue(txtWriteTime);
            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.btnClean.Enabled)
                {
                    FormStatus(false);
           
                }
            }
            else
            {
                lblMsg.Text = "";
                if (this.btnClean.Enabled == false)
                {
                    FormStatus(true);
                }
            }
            if (chbAutoRead.Checked)
            {
                ReadIOList();
            } 
        }

        private void FormStatus(bool isOpen)
        {
            this.btnClean.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;
            btnStopStop.Enabled = isOpen;
            btnLineMove.Enabled = isOpen;
        }

        private void btnSendWire_Click(object sender, EventArgs e)
        {
            //匀速运动
            int speed = FormUtil.GetIntValue(txtSpeed) * WeldRobotBean.SendWireXiShu;
            ShuoKeControls.VolMove(WeldRobotBean.RobotConfig.SendWire_Slv, speed);
        }

        private void btnStopSend_Click(object sender, EventArgs e)
        {
            ShuoKeControls.SuddownStop(WeldRobotBean.RobotConfig.SendWire_Slv);
        }
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (!IsHandleCreated)
            {
                this.Close();
            }
        } 
        private void btnTestSend_Click(object sender, EventArgs e)
        {
            int position = FormUtil.GetIntValue(txtSendWirePosition) * WeldRobotBean.SendWireXiShu;
            ShuoKeControls.RelativeMove(WeldRobotBean.RobotConfig.SendWire_Slv, position);
        }
         

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

        private void btnClean_Click(object sender, EventArgs e)
        {
            this.btnClean.Enabled = false;
            RobotBean.KNDIOMove(IO_Type.ClearWeldingSQL, IO_VALUE.HIGH);
            Thread.Sleep(WeldRobotBean.RobotConfig.ClearMSenconds);
            RobotBean.KNDIOMove(IO_Type.ClearWeldingSQL, IO_VALUE.LOW);
            this.btnClean.Enabled = true;
        }
        
        private void btnWDown_Click(object sender, EventArgs e)
        { 
            RobotBean.KNDIOMove(IO_Type.SendWire_Down, IO_VALUE.HIGH);
            RobotBean.KNDIOMove(IO_Type.SendWire_Up, IO_VALUE.LOW);
        }

        private void btnWUp_Click(object sender, EventArgs e)
        { 
            RobotBean.KNDIOMove(IO_Type.SendWire_Down, IO_VALUE.LOW);
            RobotBean.KNDIOMove(IO_Type.SendWire_Up, IO_VALUE.HIGH);
        }

        private void btnWStop_Click(object sender, EventArgs e)
        { 
            RobotBean.KNDIOMove(IO_Type.SendWire_Down, IO_VALUE.LOW);
            RobotBean.KNDIOMove(IO_Type.SendWire_Up, IO_VALUE.LOW);
        }

        private void btnStopDown_Click(object sender, EventArgs e)
        {

            RobotBean.KNDIOMove(IO_Type.StopCylinder_Down, IO_VALUE.HIGH);
            RobotBean.KNDIOMove(IO_Type.StopCylinder_Up, IO_VALUE.LOW);
        }

        private void btnStopUp_Click(object sender, EventArgs e)
        {

            RobotBean.KNDIOMove(IO_Type.StopCylinder_Down, IO_VALUE.LOW);
            RobotBean.KNDIOMove(IO_Type.StopCylinder_Up, IO_VALUE.HIGH);
        }

        private void btnStopStop_Click(object sender, EventArgs e)
        {
            RobotBean.KNDIOMove(IO_Type.StopCylinder_Down, IO_VALUE.LOW);
            RobotBean.KNDIOMove(IO_Type.StopCylinder_Up, IO_VALUE.LOW);

        }
    }
}