Frm_AxisMoveByIO.cs 4.6 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 Frm_AxisMoveByIO : Form
    {
        private List<AxisBean> axisList;
        private AxisBean currentAxis;
        private string PortName;
        private short SlvAddr;

        public Frm_AxisMoveByIO()
        {
            InitializeComponent();
            this.FormClosing += Frm_AxisMoveByIO_FormClosing;
        }

        private void Frm_AxisMoveByIO_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (threadrun)
            {
                threadrun = false;
                e.Cancel = true;
            }
        }

        public void LoadData(List<AxisBean> axisList)
        {
            //this.boxBean = equipBase;
            this.axisList = axisList;
            //axisList = new List<ConfigMoveAxis>(equipBase.Config.moveAxisList);

            if (axisList.Count > 0)
            {
                cmbAxis.Items.Clear();
                foreach (var a in axisList)
                {
                    cmbAxis.Items.Add(crc.GetString(a.Config.ProName, a.Config.Explain));
                }
                cmbAxis.SelectedIndex = 0;
                currentAxis = axisList[0];
                PortName = axisList[0].Config.DeviceName;
                SlvAddr = axisList[0].Config.GetAxisValue();
            }
            
            crc.LanguageProcess(this);
        }
        Thread td;
        bool threadrun = true;
        private void Frm_AxisMoveByIO_Load(object sender, EventArgs e)
        {
            td = new Thread(new ThreadStart(monitorio));
            threadrun = true;
            td.Start();
        }
        void monitorio() {
            bool isrun = false;
            while (threadrun) {
                Thread.Sleep(10);
                string msg = "伺服未使能";
                if (!AxisManager.IsServeoOn(PortName, SlvAddr))
                {
                    SetState(msg);
                    continue; }
                msg = "监听中";
                var Fwd = IOManager.IOValue(IO_Type.Axis_Fwd_Btn).Equals(IO_VALUE.HIGH);
                var Rwd = IOManager.IOValue(IO_Type.Axis_Rwd_Btn).Equals(IO_VALUE.HIGH);
                if ((Fwd && Rwd) || (!Fwd && !Rwd))
                {
                    msg = "按钮已释放";
                    if (isrun)
                    {
                        msg += ",停止运动";
                        isrun = false;
                        AxisManager.SuddenStop(currentAxis.Config);
                    }
                }
                else
                {
                    msg = "按钮已按下";
                    if (!isrun)
                    {
                        msg += ",开始移动";
                        isrun = true;
                        AxisMove((int)comjSpeed.SelectedItem * (Fwd?1:-1));
                    }
                }
                SetState(msg);
            }
            this.Invoke((EventHandler)delegate {
                Close();
            });
        }
        void SetState(string msg) {

            if (IsDisposed)
                return;

            if (this.InvokeRequired) {

                this.Invoke((EventHandler)delegate{
                    SetState(msg);
                });
                return;            
            }
            label_state.Text = msg;
        }
        private void cmbAxis_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentAxis = axisList[cmbAxis.SelectedIndex];
            PortName = axisList[cmbAxis.SelectedIndex].Config.DeviceName;
            SlvAddr = axisList[cmbAxis.SelectedIndex].Config.GetAxisValue();

            int targetSpeed = currentAxis.Config.TargetSpeed;
            comjSpeed.Items.Clear();
            for (int i = 1; i <= 10; i++)
            {
                comjSpeed.Items.Add(targetSpeed * i / 10);
            }
            comjSpeed.SelectedIndex = 4;
            open();
        }

        private void open()
        {
            //timer1.Start();
            LogUtil.info("" + "点击【打开伺服】,【" + PortName + "_" + SlvAddr + "】 ");
            AxisManager.ServoOn(PortName, SlvAddr);
        }

        private void AxisMove(int speed)
        {
            LogUtil.info("" + "【" + PortName + "_" + SlvAddr + "】点动: 速度:" + speed);
            AxisManager.SpeedMove(PortName, SlvAddr, speed, 0, 0);
        }
    }
}