FrmAbbRobot.cs 11.4 KB
using ABB.Robotics.Controllers;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.DoubleLineClient
{
    public partial class FrmAbbRobot : FrmBase
    {
        public FrmAbbRobot()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }
        private string SelConIp = "";
        private void FrmRobotMain_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            LoadCom();
            LoadListView();
            LoadController();
            timer1.Start();
            ABBControl.ControllerAddEvent += ABBRobotManager_ControllerAddEvent;
            //LogUtil.logBox = this.richTextBox1;
        }

        private void ABBRobotManager_ControllerAddEvent(ControllerInfo controller)
        {
            LogUtil.info("ControllerAddEvent 新机器人:" + controller.IPAddress);
            int index = listView1.Items.Count;
            AddConInfo(controller, index);
        }

        private void LoadCom()
        {
            comMoveCmd.Items.Clear();
            comMoveCmd.Items.Add(ABBControl.Cmd_movep);
            comMoveCmd.Items.Add(ABBControl.Cmd_moveget);
            comMoveCmd.Items.Add(ABBControl.Cmd_moveput);
            comMoveCmd.Items.Add(ABBControl.Cmd_validateP);

            comMoveP.Items.Clear();
            for (int i = 1; i <= 30; i++)
            {
                comMoveP.Items.Add("p" + i);
            }
            comMoveP.SelectedIndex = 0;
            comMoveCmd.SelectedIndex = 0;

            radioButton1.Checked = true;

        }

        private void LoadController()
        {
            this.listView1.Items.Clear();
            ABBControl.Load();
            int i = 0;
            foreach (ControllerInfo con in ABBControl.controllerInfoMap.Values)
            {
                AddConInfo(con, i);
                i++;
            }
        }

        private void AddConInfo(ControllerInfo con, int i)
        {
            ListViewItem item = new ListViewItem(con.IPAddress.ToString());
            item.SubItems.Add(con.Id.ToString());
            item.SubItems.Add(con.Availability.ToString());
            item.SubItems.Add(con.IsVirtual.ToString());
            item.SubItems.Add(con.SystemName);
            item.SubItems.Add(con.Version.ToString());
            item.SubItems.Add(con.ControllerName);

            item.Tag = con.IPAddress;
            string state = ABBControl.GetRobotState(con.IPAddress.ToString()).ToString();
            item.SubItems.Add(state);
            this.listView1.Items.Add(item);
            this.listView1.Items[i].Selected = true;
            SelConIp = con.IPAddress.ToString();
        }
        private void LoadListView()
        {
            this.listView1.Columns.Clear();
            AddHealder("IPAddress", 150);
            AddHealder("ID", 180);
            //   AddHealder("设备状态", 100);
            AddHealder("Availability", 150);
            AddHealder("IsVirtual", 150);
            AddHealder("SystemName", 150);
            AddHealder("Version", 150);
            AddHealder("ControllerName", 180);
            AddHealder("Statue", 180);
        }


        private void AddHealder(string name, int widht)
        {
            ColumnHeader preSendwire = new ColumnHeader();
            preSendwire.Text = name;   //设置列标题
            preSendwire.Width = widht;    //设置列宽度
            preSendwire.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listView1.Columns.Add(preSendwire);    //将列头添加到ListView控件。
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!Visible)
            {
                return;
            }
            int i = 0;
            List<string> conList = new List<string>(ABBControl.controllerMap.Keys);
            foreach (string con in conList)
            {
                string result = ABBControl.GetRobotState(con).ToString();
                listView1.Items[i].SubItems[7].Text = result;
                i++;
            }

            int esValue = ABBControl.GetSingalState(SelConIp);
            if (ABBControl.IsAutoMove)
            {
                lblCurrMove.Text = "连续运动中:" + ABBControl.LastAutoP;
                btnStartAuto.Text = "停止连续运动";
            }
            else
            {
                lblCurrMove.Text = ABBControl.getLastCmd(SelConIp);
                btnStartAuto.Text = "开始连续运动";
            }
            if (String.IsNullOrEmpty(SelConIp))
            {
                lblCurrMove.Text = "";
                lblState.Text = "";
                return;
            }
            bool isOk = false;
            if (esValue.Equals(1))
            {
                SetMsg("机器人[" + SelConIp + "]急停中", Color.Red);
            }
            else
            {
                lblState.Text = "";
                int isRun = ABBControl.GetSingalState(SelConIp, ABBControl.DO_IsRun);
                if (isRun.Equals(1))
                {
                    isOk = true;
                    SetMsg("机器人程序正常运行中", Color.Green);
                }
                else
                {
                    SetMsg("机器人[" + SelConIp + "]程序未运行", Color.Red);
                }
            }
            if (isOk && ABBControl.IsConnected(SelConIp) && chbUseAbb.Checked)
            {
                if (!groupMove.Enabled)
                {
                    groupMove.Enabled = true;
                }
            }
            else if (groupMove.Enabled)
            {
                groupMove.Enabled = false;
            }
        }

        private void SetMsg(string msg, Color foreColor)
        {
            lblState.Text = msg;
            lblState.ForeColor = foreColor;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(SelConIp))
            {
                return;
            }
            ABBControl.StartABBControl(SelConIp);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(SelConIp))
            {
                return;
            }
            ABBControl.StopABBControl(SelConIp);
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems != null && listView1.SelectedItems.Count > 0)
            {
                int index = listView1.SelectedItems[0].Index;
                string ip = listView1.Items[index].SubItems[0].Text.ToString();
                if (!String.IsNullOrEmpty(ip))
                {
                    SelConIp = ip;
                    if (ABBControl.IsConnected(SelConIp))
                    {
                        groupMove.Enabled = true;
                    }
                    else
                    {
                        groupMove.Enabled = false;
                    }
                }
            }
        }

        private void FrmRobotMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            ABBControl.Dispose();
            LogUtil.logBox = null;
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            if (SelConIp.Equals(""))
            {
                MessageBox.Show("请先选择一个机器人", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string moveCmd = comMoveCmd.Text;
            string PPoint = comMoveP.Text;
            string MType = "L";
            if (radioButton2.Checked)
            {
                MType = "J";
            }
            int speed = (int)numSpeed.Value;
            //if (speed > 500)
            //{
            //    speed = 100;
            //}
            ABBControl.SendCmd(SelConIp, moveCmd, PPoint, MType, speed);
        }
        private void MoveEnd(string msg)
        {
            LogUtil.info("机器人运动完成:" + msg);
        }
        private string lastCmd = "";
        private bool lastCheck = true;
        private void comMoveCmd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comMoveCmd.Text.Equals(lastCmd) && checkBox1.Checked.Equals(lastCheck))
            {
                return;
            }
            lastCheck = checkBox1.Checked;
            lastCmd = comMoveCmd.Text;
            if (lastCmd.Equals(ABBControl.Cmd_moveget))
            {
                comMoveP.Items.Clear();
                for (int i = 1; i <= 4; i++)
                {
                    comMoveP.Items.Add("p" + i);
                }
            }
            else if (lastCmd.Equals(ABBControl.Cmd_moveput))
            {
                comMoveP.Items.Clear();
                int max = 92;
                string p = "dp";
                if (checkBox1.Checked.Equals(false))
                {
                    max = 12;
                    p = "cp";
                }
                for (int i = 1; i <= max; i++)
                {
                    comMoveP.Items.Add(p + i);
                }
            }
            if (comMoveP.Items.Count > 0)
            {
                comMoveP.SelectedIndex = 0;
            }
        }

        private void btnStartAuto_Click(object sender, EventArgs e)
        {
            if (ABBControl.IsAutoMove)
            {
                ABBControl.StopConMove();
                lblCurrMove.Text = ABBControl.getLastCmd(SelConIp);
                btnStartAuto.Text = "开始连续运动";
            }
            else
            {
                if (!comMoveCmd.Text.Equals(ABBControl.Cmd_moveput))
                {
                    MessageBox.Show("运动方式=" + ABBControl.Cmd_moveput + " 时,才能连续运动", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (SelConIp.Equals(""))
                {
                    MessageBox.Show("请先选择一个机器人", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                string moveCmd = comMoveCmd.Text;
                string PPoint = comMoveP.Text;
                string MType = "L";
                if (radioButton2.Checked)
                {
                    MType = "J";
                }
                int speed = (int)numSpeed.Value;
                if (speed > 500)
                {
                    speed = 100;
                }
                List<string> PointList = new List<string>();
                for (int i = comMoveP.SelectedIndex; i < comMoveP.Items.Count; i++)
                {
                    PointList.Add(comMoveP.Items[i].ToString());
                }
                ABBControl.StartConMove(SelConIp, PointList, "L", "p1", speed);
                lblCurrMove.Text = "连续运动中:" + ABBControl.LastAutoP;
                btnStartAuto.Text = "停止连续运动";
            }
        }


        private void btnReload_Click(object sender, EventArgs e)
        {
            LogUtil.info("点击:重新加载机器人");
            ABBControl.Dispose();
            Thread.Sleep(10);
            LoadController();
        }
    }
}