轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-04-03 c98c47d5fc31ac10f1671a67eee588cb74b223c6
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using LB_SmartVisionCommon;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
 
namespace LB_VisionProcesses.Communicators.SiemensS7
{
    public class SiemensLBS7 : BaseCommunicator
    {
        private Plc plc;
        // 默认变量地址
        public string variable = "DB100.DBX0.1"; 
        // 数据类型
        private string dataType = "Bool";
        
        // 缓存连接参数
        private string ip = "127.0.0.1";
        private short rack = 0;
        private short slot = 1;
        private CpuType cpuType = CpuType.S71500;
 
        public SiemensLBS7(string name = "西门子S7") : base(name)
        {
            CommunicatorName = name;
            CommunicatorBrand = CommunicatorBrand.SiemensS7;
            
            // 初始化默认参数
            if (!CommunicatorConnections.Contains("地址")) CommunicatorConnections.Add("地址", "192.168.0.1");
            if (!CommunicatorConnections.Contains("机架号")) CommunicatorConnections.Add("机架号", "0");
            if (!CommunicatorConnections.Contains("插槽号")) CommunicatorConnections.Add("插槽号", "1");
            if (!CommunicatorConnections.Contains("型号")) CommunicatorConnections.Add("型号", CpuType.S71500);
            if (!CommunicatorConnections.Contains("变量地址")) CommunicatorConnections.Add("变量地址", "DB1.DBD0");
            if (!CommunicatorConnections.Contains("数据类型")) CommunicatorConnections.Add("数据类型", "String");
            
            // 兼容旧配置 "端口"
            if (CommunicatorConnections.Contains("端口"))
            {
                CommunicatorConnections["插槽号"] = CommunicatorConnections["端口"];
            }
 
            // 设置默认心跳消息
            strHeartbeat = "HEARTBEAT"; 
        }
 
        public override bool Connect()
        {
            try
            {
                // 更新参数
                if (CommunicatorConnections.Contains("地址")) ip = CommunicatorConnections["地址"].ToString();
                
                if (CommunicatorConnections.Contains("机架号")) 
                    short.TryParse(CommunicatorConnections["机架号"].ToString(), out rack);
                
                if (CommunicatorConnections.Contains("插槽号")) 
                    short.TryParse(CommunicatorConnections["插槽号"].ToString(), out slot);
                else if (CommunicatorConnections.Contains("端口"))
                    short.TryParse(CommunicatorConnections["端口"].ToString(), out slot);
 
                if (CommunicatorConnections.Contains("型号"))
                {
                    if (CommunicatorConnections["型号"] is CpuType type)
                        cpuType = type;
                    else
                        Enum.TryParse(CommunicatorConnections["型号"].ToString(), out cpuType);
                }
                
                if (CommunicatorConnections.Contains("变量地址"))
                {
                    variable = CommunicatorConnections["变量地址"].ToString();
                }
 
                if (CommunicatorConnections.Contains("数据类型"))
                {
                    dataType = CommunicatorConnections["数据类型"].ToString();
                }
 
                // 关闭旧连接
                plc?.Close();
                
                plc = new Plc(cpuType, ip, rack, slot);
                plc.Open();
                
                if (plc.IsConnected)
                {
                    bConnected = true;
                    AsyncLogHelper.Info($"Device:[{CommunicatorName}] 已连接到 {ip} 机架:{rack} 插槽:{slot}");
                    return true;
                }
                else
                {
                    bConnected = false;
                    AsyncLogHelper.Error($"Device:[{CommunicatorName}] 连接失败: IsConnected 为 false");
                    return false;
                }
            }
            catch (Exception ex)
            {
                bConnected = false;
                AsyncLogHelper.Error($"Device:[{CommunicatorName}] 连接错误: {ex.Message}");
                return false;
            }
        }
 
        public override bool Disconnect()
        {
            try
            {
                if (plc != null)
                {
                    plc.Close();
                    bConnected = false;
                    AsyncLogHelper.Info($"Device:[{CommunicatorName}] 已断开连接");
                }
                return true;
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"Device:[{CommunicatorName}] 断开连接错误: {ex.Message}");
                return false;
            }
        }
 
        public override bool SendMessage(string message)
        {
            if (plc == null || !plc.IsConnected)
            {
                Msg = "连接未开启";
                return false;
            }
 
            if (message == strHeartbeat) return plc.IsConnected;
 
            try
            {
                string targetVar = variable;//"DB103.DBD0";
                string strValue = message;
 
                // 简单的协议解析:地址:值
                if (message.Contains(":"))
                {
                    var parts = message.Split(new char[] { ':' }, 2);
                    if (parts.Length == 2 && !string.IsNullOrWhiteSpace(parts[0]))
                    {
                        targetVar = parts[0];
                        strValue = parts[1];
                    }
                }
 
                object valueToWrite = strValue;
                // 获取当前数据类型配置
                string currentDataType = CommunicatorConnections.Contains("数据类型") ? CommunicatorConnections["数据类型"].ToString() : "String";
 
                // 根据配置的数据类型进行转换
                try 
                {
                    switch (currentDataType)
                    {
                        case "Bool":
                            {
                                if (strValue == "1")
                                {
                                    valueToWrite = true;
                                }
                                else if (strValue == "0")
                                {
                                    valueToWrite = false;
                                }
                                else
                                {
                                    valueToWrite = bool.Parse(strValue);
                                }
                                break;
                            }
                        case "Byte":
                            {
                                valueToWrite = byte.Parse(strValue);
                                break;
                            }
                        case "Int": // 16-bit
                            {
                                valueToWrite = short.Parse(strValue);
                                break;
                            }
                        case "DInt": // 32-bit
                            {
                                valueToWrite = int.Parse(strValue);
                                break;
                            }
                        case "Word": // 16-bit unsigned
                            {
                                valueToWrite = ushort.Parse(strValue);
                                break;
                            }
                        case "DWord": // 32-bit unsigned
                            {
                                valueToWrite = uint.Parse(strValue);
                                break;
                            }
                        case "Real": // Float
                            valueToWrite = float.Parse(strValue);
                            break;
                        case "Double": // LReal
                            {
                                valueToWrite = double.Parse(strValue);
                                break;
                            }
                        case "String":
                        default:
                            {
                                valueToWrite = strValue;
                                break;
                            }
                    }
                }
                catch (FormatException)
                {
                    Msg = $"无效的{currentDataType}值,请输入正确格式。";
                    if (currentDataType == "Bool") Msg += " (true/false 或 1/0)";
                    AsyncLogHelper.Error($"Device:[{CommunicatorName}] {Msg}");
                    return false;
                }
                catch (Exception castEx)
                {
                    Msg = $"数据转换错误({currentDataType}): {castEx.Message}";
                    AsyncLogHelper.Error($"Device:[{CommunicatorName}] {Msg}");
                    return false;
                }
 
                // 尝试写入
                plc.Write(targetVar, valueToWrite);
                AsyncLogHelper.Info($"Device:[{CommunicatorName}] 写入({currentDataType}) {targetVar} = {valueToWrite}");
                return true;
            }
            catch (Exception ex)
            {
                Msg = $"发送消息错误: {ex.Message}";
                AsyncLogHelper.Error($"Device:[{CommunicatorName}] {Msg}");
                return false;
            }
        }
 
        public override string ReceiveMsg()
        {
            if (plc == null || !plc.IsConnected) return string.Empty;
 
            try
            {
                // 获取当前数据类型配置
                string currentDataType = CommunicatorConnections.Contains("数据类型") ? CommunicatorConnections["数据类型"].ToString() : "String";
 
                if (currentDataType == "String")
                {
                    var match = Regex.Match(variable, @"DB(\d+)\.DB[B|W|D|X]?(\d+)", RegexOptions.IgnoreCase);
                    if (match.Success)
                    {
                        try
                        {
                            int db = int.Parse(match.Groups[1].Value);
                            int startByte = int.Parse(match.Groups[2].Value);
 
                            // 读取头部 (MaxLen, ActLen)
                            byte[] header = plc.ReadBytes(DataType.DataBlock, db, startByte, 2);
                            if (header != null && header.Length >= 2)
                            {
                                int actLen = header[1];
                                if (actLen > 0)
                                {
                                    // 读取实际字符数据
                                    byte[] strBytes = plc.ReadBytes(DataType.DataBlock, db, startByte + 2, actLen);
                                    strReceiveMsg = Encoding.ASCII.GetString(strBytes);
                                    return strReceiveMsg;
                                }
                                else
                                {
                                    strReceiveMsg = string.Empty;
                                    return strReceiveMsg;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            AsyncLogHelper.Error($"Device:[{CommunicatorName}] 读取S7String失败: {ex.Message}");
                        }
                    }
                    // 如果正则不匹配或读取失败,回退到默认读取
                }
 
                var result = plc.Read(variable);
                if (result != null)
                {
                    // 尝试根据 dataType 格式化输出 (S7.Net 读出来的类型可能与预期不符,特别是 DWord/Real)
                    // 例如 DBD0 默认读出来是 UInt32,如果 dataType 是 Real,需要转换
                    if (currentDataType == "Real" && (result is uint || result is int))
                    {
                        byte[] bytes = BitConverter.GetBytes(Convert.ToUInt32(result));
                        float f = BitConverter.ToSingle(bytes, 0);
                        strReceiveMsg = f.ToString();
                    }
                    else
                    {
                        strReceiveMsg = result.ToString();
                    }
                    return strReceiveMsg;
                }
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"Device:[{CommunicatorName}] 接收消息错误: {ex.Message}");
            }
            return string.Empty;
        }
 
        /// <summary>
        /// 带填充的 S7 字符串写入,防止残留数据
        /// </summary>
        private bool WriteS7StringWithPadding(string address, string value)
        {
            try
            {
                // 解析地址,例如 DB1.DBB0, DB1.DBD0
                var match = Regex.Match(address, @"DB(\d+)\.DB[B|W|D|X]?(\d+)", RegexOptions.IgnoreCase);
                if (!match.Success) return false;
 
                int db = int.Parse(match.Groups[1].Value);
                int startByte = int.Parse(match.Groups[2].Value);
 
                byte maxLen = 254; // 默认最大值
                try
                {
                    var header = plc.ReadBytes(DataType.DataBlock, db, startByte, 1);
                    if (header != null && header.Length > 0 && header[0] > 0)
                    {
                        maxLen = header[0];
                    }
                }
                catch { }
 
                byte[] buffer = new byte[maxLen + 2];
                buffer[0] = maxLen;
                int currentLen = Math.Min(value.Length, maxLen);
                buffer[1] = (byte)currentLen;
 
                if (currentLen > 0)
                {
                    byte[] strBytes = Encoding.ASCII.GetBytes(value);
                    Array.Copy(strBytes, 0, buffer, 2, Math.Min(strBytes.Length, currentLen));
                }
 
                plc.WriteBytes(DataType.DataBlock, db, startByte, buffer);
                return true;
            }
            catch
            {
                return false;
            }
        }
        
        public object Read(string address)
        {
            if (plc == null || !plc.IsConnected)
            {
                return null;
            }
             return plc.Read(address);
        }
        
        public void Write(string address, object value)
        {
            if (plc != null && plc.IsConnected)
            {
                plc.Write(address, value);
            }
        }
 
        public override void Dispose()
        {
            try
            {
                AsyncLogHelper.Info($"Device:[{CommunicatorName}],释放资源(Dispose)");
                plc?.Close();
                plc = null;
                GC.SuppressFinalize(this);
            }
            catch (Exception ex)
            {
                AsyncLogHelper.Error($"Device:[{CommunicatorName}],释放资源(Dispose)错误: " + ex.Message);
            }
        }
 
        public override bool SendMessage(byte[] message)
        {
            throw new NotImplementedException();
        }
    }
}