C3204
2026-01-15 a987bb05357a451c8476098067573059287dc008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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; }
        }
    }
}