IOControls.cs 7.7 KB
using DeviceLibrary;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
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 TheMachine
{
    public partial class IOControls : UserControl
    {
        Robot_Config _Config;
        public Robot_Config Config
        {
            get { return _Config; }
            set
            {
                _Config = value;
                ioControl1.Config = value;
            }
        }
        public IOControls()
        {
            InitializeComponent();
        }


        private void ioControl1_Load(object sender, EventArgs e)
        {
        }

        private void btn_FlipEquip_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }

            RobotManage.mainMachine.FlipEquip.LiftUp(null);
            LogUtil.info("手动点击:FlipEquip.LiftUp");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.FlipEquip.LiftDown(null);
            LogUtil.info("手动点击:FlipEquip.LiftDown");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.RotateEquip.TurnToEnd(true);
            LogUtil.info("手动点击:RotateEquip.TurnToEnd 水平");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.RotateEquip.TurnToEnd(false);
            LogUtil.info("手动点击:RotateEquip.TurnToEnd 垂直");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.TPMove.ToLow(null);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.TPMove.ToHigh(null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.RotateEquip.TurnDegree(45);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.RotateEquip.TurnDegree(-45);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var result = IsSafe();
            if (!result.Item1)
            {
                MessageBox.Show(result.Item2);
                return;
            }
            RobotManage.mainMachine.RotateEquip.TurnRound();
        }

        public static (bool, string) IsSafe()
        {
            if (RobotManage.mainMachine.IOValue(IO_Type.SuddenStop_BTN).Equals(IO_VALUE.LOW))
                return (false, "Emergency stop.");

            if (RobotManage.isRunning)
                return (false, "Safety interlock activated");

            if (!RobotManage.mainMachine.IsAllDoorClosed())
                return (false, "Safe door was not closed");
            return (true, "");
        }

        private void button9_Click(object sender, EventArgs e)
        {
           var r=  RobotManage.RFID.ReOpen();
           RobotManage.RFID2.ReOpen();
            LogUtil.info("RFID.ReOpen:" + r);
            for (int i = 0; i < 6;i++) {
                Thread.Sleep(200);
                if (RobotManage.RFID.ReadEPC(16,out byte[] data)) {
                    if (data[0] != 0xFF)
                    {
                        MessageBox.Show("RFID:"+Encoding.ASCII.GetString(data).Trim());
                        return;
                    }
                }
                if (RobotManage.RFID2.ReadEPC(16, out  data))
                {
                    if (data[0] != 0xFF)
                    {
                        MessageBox.Show("RFID2:" + RobotManage.RFID2.HexBuff(data) + "\r\n" + Encoding.ASCII.GetString(data).Trim());
                        return;
                    }
                }
            }
            MessageBox.Show("no data");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var epc = InputBox("Write EPC, maximum 16 words.", "Please enter EPC data, maximum 16 words.", "");
            
            var r = RobotManage.RFID.ReOpen();
            RobotManage.RFID2.ReOpen();
            LogUtil.info("RFID.ReOpen:" + r+",epc="+ epc);
            epc = epc.PadRight(16);
            RobotManage.RFID.WriteByte(550, new byte[] { 0x40, 0x00 });
            Thread.Sleep(300);
            var result = RobotManage.RFID.WriteEPC(Encoding.ASCII.GetBytes(epc));
            result = RobotManage.RFID2.WriteEPC(Encoding.ASCII.GetBytes(epc));
            if (result)
            {
                MessageBox.Show(crc.GetString("Res0144","写入成功"));
                return;
            }
        }

        private string InputBox(string Caption, string Hint, string Default)
        {
            Form InputForm = new Form();
            InputForm.MinimizeBox = false;
            InputForm.MaximizeBox = false;
            InputForm.StartPosition = FormStartPosition.CenterScreen;
            InputForm.Width = 360;
            InputForm.Height = 150;
            //InputForm.Font.Name = "宋体";
            //InputForm.Font.Size = 10;

            InputForm.Text = Caption;

            Label lbl = new Label();
            lbl.Text = Hint;
            lbl.Left = 10;
            lbl.Top = 20;
            lbl.Parent = InputForm;
            lbl.AutoSize = true;

            TextBox tb = new TextBox();
            tb.Left = 30;
            tb.Top = 45;
            tb.Width = 300;
            tb.Parent = InputForm;
            tb.Text = Default;
            tb.SelectAll();

            Button btnok = new Button();
            btnok.Left = 30;
            btnok.Top = 80;
            btnok.Parent = InputForm;
            btnok.Text = "Ok";
            InputForm.AcceptButton = btnok;//回车响应

            btnok.DialogResult = DialogResult.OK;

            Button btncancal = new Button();
            btncancal.Left = 120;
            btncancal.Top = 80;
            btncancal.Parent = InputForm;
            btncancal.Text = "Cancel";
            btncancal.DialogResult = DialogResult.Cancel;
            try
            {
                if (InputForm.ShowDialog() == DialogResult.OK)
                {
                    return tb.Text;
                }
                else
                {
                    return "";
                }
            }
            finally
            {
                InputForm.Dispose();
            }

        }
    }
}