919a8efe5f75b9d84ed79c91dfbea5263da8ce59..76d74124f6011a25ee1cdbc322ab22e2c36d7beb
2026-01-16 C3204
添加运动控制菜单底层逻辑
76d741 对比 | 目录
2026-01-15 C3204
添加运动控制菜单
a987bb 对比 | 目录
已修改2个文件
已添加3个文件
341 ■■■■■ 文件已修改
LB_SmartVision/Forms/Pages/CommunicatorPage/CommunicatorForm.Designer.cs 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LB_SmartVision/Forms/Pages/CommunicatorPage/CommunicatorForm.cs 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LB_VisionProcesses/MotionControl/BaseMotionControl.cs 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LB_VisionProcesses/MotionControl/IMotionControl.cs 42 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LB_VisionProcesses/MotionControl/MotionControlConfig.cs 189 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
LB_SmartVision/Forms/Pages/CommunicatorPage/CommunicatorForm.Designer.cs
@@ -36,10 +36,12 @@
            txtSendMsg = new TextBox();
            grpSetting = new GroupBox();
            txtAddress = new TextBox();
            cmbVarType = new ComboBox();
            cmbType = new ComboBox();
            txtIP = new TextBox();
            txtPort = new TextBox();
            lblPort = new Label();
            lbVarType = new Label();
            cmbIP = new ComboBox();
            lblType = new Label();
            lblAddress = new Label();
@@ -134,10 +136,12 @@
            // 
            grpSetting.BackColor = Color.FromArgb(32, 41, 50);
            grpSetting.Controls.Add(txtAddress);
            grpSetting.Controls.Add(cmbVarType);
            grpSetting.Controls.Add(cmbType);
            grpSetting.Controls.Add(txtIP);
            grpSetting.Controls.Add(txtPort);
            grpSetting.Controls.Add(lblPort);
            grpSetting.Controls.Add(lbVarType);
            grpSetting.Controls.Add(cmbIP);
            grpSetting.Controls.Add(lblType);
            grpSetting.Controls.Add(lblAddress);
@@ -164,6 +168,17 @@
            txtAddress.Size = new Size(124, 24);
            txtAddress.TabIndex = 9;
            txtAddress.TextChanged += txtAddress_TextChanged;
            //
            // cmbVarType
            //
            cmbVarType.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbVarType.FormattingEnabled = true;
            cmbVarType.Location = new Point(533, 32);
            cmbVarType.Margin = new Padding(4);
            cmbVarType.Name = "cmbVarType";
            cmbVarType.Size = new Size(124, 26);
            cmbVarType.TabIndex = 8;
            cmbVarType.SelectedIndexChanged += cmbVarType_SelectedIndexChanged;
            // 
            // cmbType
            // 
@@ -201,6 +216,16 @@
            lblPort.TabIndex = 4;
            lblPort.Text = "端口";
            // 
            // lbVarType
            //
            lbVarType.AutoSize = true;
            lbVarType.Location = new Point(463, 35);
            lbVarType.Margin = new Padding(4, 0, 4, 0);
            lbVarType.Name = "lbVarType";
            lbVarType.Size = new Size(68, 18);
            lbVarType.TabIndex = 0;
            lbVarType.Text = "变量类型";
            //
            // cmbIP
            // 
            cmbIP.FormattingEnabled = true;
@@ -228,7 +253,7 @@
            lblAddress.Name = "lblAddress";
            lblAddress.Size = new Size(38, 18);
            lblAddress.TabIndex = 0;
            lblAddress.Text = "地址";
            lblAddress.Text = "变量地址";
            // 
            // lblIP
            // 
LB_SmartVision/Forms/Pages/CommunicatorPage/CommunicatorForm.cs
@@ -28,10 +28,14 @@
        {
            InitializeComponent();
            cmbType.Items.Clear();
            if (communicator != null && !string.IsNullOrEmpty(communicator.CommunicatorConnections["型号"]?.ToString()))
            {
                cmbType.Items.Add(communicator.CommunicatorConnections["型号"]?.ToString());
            }
            cmbVarType.Items.Clear();
            if (!string.IsNullOrEmpty(communicator.CommunicatorConnections["变量类型"]?.ToString()))
            {
                cmbVarType.Items.Add(communicator.CommunicatorConnections["变量类型"]?.ToString());
            }
        }
@@ -39,8 +43,9 @@
        {
            InitializeComponent();
            if (communicator == null)
            {
                return;
            }
            cmbIP.Enabled = false;
            txtIP.Enabled = false;
            txtPort.Enabled = false;
@@ -56,23 +61,20 @@
            this.Text = name;
            if (communicator is UARTPort)
            {
                //统计可用端口
                SerialPortStream temp = new SerialPortStream();
                string[] ArryPort = temp.GetPortNames();
                for (int i = 0; i < ArryPort.Length; i++)
                {
                    cmbIP.Items.Add(ArryPort[i]);
                }
                cmbIP.Text = communicator.CommunicatorConnections["地址"]?.ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"]?.ToString();
                txtIP.Visible = false;
                cmbIP.Visible = true;
                this.btnRuleSend.Visible = true;
                lblType.Visible = false;
                lblAddress.Visible = false;
                cmbType.Visible = false;
@@ -89,10 +91,7 @@
                txtPort.Text = communicator.CommunicatorConnections["端口"]?.ToString();
                txtIP.Visible = true;
                cmbIP.Visible = false;
                this.btnRuleSend.Visible = false;
                lblType.Visible = false;
                lblAddress.Visible = false;
                cmbType.Visible = false;
@@ -108,7 +107,6 @@
                txtIP.Text = communicator.CommunicatorConnections["地址"]?.ToString();
                txtPort.Text = communicator.CommunicatorConnections["端口"]?.ToString();
                cmbType.Items.Clear();
                if (!string.IsNullOrEmpty(communicator.CommunicatorConnections["型号"]?.ToString()))
                {
                    cmbType.Items.Add(communicator.CommunicatorConnections["型号"]?.ToString());
@@ -130,7 +128,6 @@
                txtIP.Visible = true;
                cmbIP.Visible = false;
                this.btnRuleSend.Visible = false;
                lblType.Visible = true;
                lblAddress.Visible = true;
                cmbType.Visible = true;
@@ -313,5 +310,18 @@
                communicatorChanged(communicator);
            }
        }
        private void cmbVarType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (communicator != null && !communicator.CommunicatorConnections.Contains("变量类型"))
            {
                communicator.CommunicatorConnections.Add("变量类型", cmbVarType.Text);
            }
            else if (communicator != null)
            {
                communicator.CommunicatorConnections["变量类型"] = cmbVarType.Text;
                communicatorChanged(communicator);
            }
        }
    }
}
LB_VisionProcesses/MotionControl/BaseMotionControl.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_VisionProcesses.MotionControl
{
    public class BaseMotionControl : IMotionControl
    {
        /// <summary>
        /// è¿åŠ¨æŽ§åˆ¶åç§°
        /// </summary>
        public string MotionControlName { get; set; } = string.Empty;
        /// <summary>
        /// æ˜¯å¦è¿žæŽ¥
        /// </summary>
        public bool bConnected = true;
        public bool Connect()
        {
            throw new NotImplementedException();
        }
        public bool Disconnect()
        {
            throw new NotImplementedException();
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }
        public string ReceiveMsg()
        {
            throw new NotImplementedException();
        }
        public void SendHeartbeat()
        {
            throw new NotImplementedException();
        }
        public bool SendMessage(string message)
        {
            throw new NotImplementedException();
        }
    }
}
LB_VisionProcesses/MotionControl/IMotionControl.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_VisionProcesses.MotionControl
{
    public interface IMotionControl : IDisposable
    {
        /// <summary>
        /// è¿žæŽ¥é€šè®¯
        /// </summary>
        /// <returns></returns>
        bool Connect();
        /// <summary>
        /// æ–­å¼€è¿žæŽ¥
        /// </summary>
        /// <returns></returns>
        bool Disconnect();
        /// <summary>
        /// æŽ¥æ”¶æ¶ˆæ¯
        /// </summary>
        /// <returns></returns>
        string ReceiveMsg();
        /// <summary>
        /// å‘送消息
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        bool SendMessage(string message);
        /// <summary>
        /// å‘送心跳信号
        /// </summary>
        void SendHeartbeat();
    }
}
LB_VisionProcesses/MotionControl/MotionControlConfig.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,189 @@
using LB_VisionProcesses.Communicators;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LB_VisionProcesses.MotionControl
{
    [Serializable]
    [Process("运动控制模块", Category = "运动控制工具", Description = "创建运动控制工具")]
    public class MotionControlConfig : IProcess
    {
        /// <summary>
        /// è¿åŠ¨æŽ§åˆ¶æ¨¡å—é›†åˆ(Key:通讯名,Value:通讯句柄)
        /// </summary>
        public ConcurrentDictionary<string, BaseMotionControl> dicMotionControls { get; set; }
        /// <summary>
        /// é€šè®¯é›†åˆ(Key:通讯名,Value:通讯句柄)
        /// </summary>
        public ConcurrentDictionary<string, BaseCommunicator> dicCommunicators { get; set; }
        public MotionControlConfig(ConcurrentDictionary<string, BaseMotionControl> dicMotionControl)
        {
            this.dicMotionControls = dicMotionControl;
            strProcessClass = "LB_VisionProcesses.Communicators.MotionControlConfig";
            Params.Inputs.Add("运动控制名称", "");
            //Params.Inputs.Add("Xè½´PLC地址", "");
            //Params.Inputs.Add("X轴检测位置", "");
            //Params.Inputs.Add("Yè½´PLC地址", "");
            //Params.Inputs.Add("Zè½´PLC地址", "");
            //Params.Inputs.Add("Wè½´PLC地址", "");
            Params.Outputs.Add("收到的信息", "");
        }
        public override object Clone()
        {
            return MemberwiseClone();
        }
        public override void Dispose()
        {
            return;
        }
        public override void InitRunParams()
        {
            Result = true;
            Msg = "";
            if (Record != null)
            {
                Record.Dispose();
            }
        }
        /// <summary>
        /// åŠ è½½ç®—æ³•
        /// </summary>
        /// <param name="fullPath">完整路径带.json</param>
        /// <returns></returns>
        public override bool Load(string fullPath)
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath))
                    return false;
                if (!fullPath.Contains(".json"))
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
                if (string.IsNullOrEmpty(fullPath) || fullPath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
                // èŽ·å–ä¸å¸¦æ–‡ä»¶åçš„ç›®å½•è·¯å¾„
                string directoryPath = Path.GetDirectoryName(fullPath);
                strProcessName = Path.GetFileNameWithoutExtension(fullPath);
                if (!File.Exists(fullPath))
                {
                    Debug.WriteLine("文件不存在创建空文件");
                    Save(directoryPath);
                    return true;
                }
                string strJson = string.Empty;
                using (StreamReader streamReader = new StreamReader(fullPath, Encoding.UTF8))
                {
                    strJson = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
                if (Params == null)
                    return false;
                Params.FixDeserializedData();
                return true;
            }
            catch { return false; }
        }
        public override bool Run()
        {
            InitRunParams();
            Params.Outputs["收到消息"] = "";
            string MotionControlName = Params.Inputs["运动控制名称"].ToString();
            if (!dicMotionControls.ContainsKey(MotionControlName))
            {
                Msg = $"运动控制[{MotionControlName}]不存在";
                Result = false;
                return Result;
            }
            BaseMotionControl BaseMotionControl = dicMotionControls[MotionControlName];
            if (BaseMotionControl == null)
            {
                Msg = $"运动控制[{MotionControlName}]未实例化";
                Result = false;
                return Result;
            }
            if (!BaseMotionControl.bConnected)
            {
                Msg = $"运动控制[{MotionControlName}]未连接";
                Result = false;
                return Result;
            }
            return true;
        }
        /// <summary>
        /// ä¿å­˜ç®—法
        /// </summary>
        /// <param name="filePath">不带.json</param>
        /// <returns></returns>
        public override bool Save(string filePath = null)
        {
            try
            {
                if (string.IsNullOrEmpty(filePath) || filePath.Trim() == "")
                {
                    Debug.WriteLine("文件路径不完整");
                    return false;
                }
                string strJson = string.Empty;
                var settings = new JsonSerializerSettings
                {
                    Formatting = Newtonsoft.Json.Formatting.Indented,
                    // è‡ªå®šä¹‰ç¼©è¿›ï¼ˆ4空格)
                    ContractResolver = new DefaultContractResolver
                    {
                        NamingStrategy = new CamelCaseNamingStrategy()
                    }
                };
                strJson = JsonConvert.SerializeObject(Params, settings);
                Params = JsonConvert.DeserializeObject<ProcessParams>(strJson);
                if (Params == null)
                    return false;
                //判断文件夹是否存在,防呆输入为文件名称
                if (!Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    catch (Exception)
                    { }
                }
                File.WriteAllText(filePath + "//" + strProcessName + ".json", strJson, Encoding.UTF8);
                return true;
            }
            catch { return false; }
        }
    }
}