From 60328b34b853959613f8c97567b4a1ba934a15a9 Mon Sep 17 00:00:00 2001
From: IPC-610 <IPC-610@DESKTOP-6LEOOS3>
Date: 星期日, 22 九月 2024 17:36:39 +0800
Subject: [PATCH] 修改问题
---
Admin.NET/WCS.Application/PLC/PLCUtil.cs | 124 ++++++++++++++++++++++++++++++++++-------
1 files changed, 102 insertions(+), 22 deletions(-)
diff --git a/Admin.NET/WCS.Application/PLC/PLCUtil.cs b/Admin.NET/WCS.Application/PLC/PLCUtil.cs
index 90d6cbc..0421e04 100644
--- a/Admin.NET/WCS.Application/PLC/PLCUtil.cs
+++ b/Admin.NET/WCS.Application/PLC/PLCUtil.cs
@@ -1,4 +1,5 @@
锘縰sing DocumentFormat.OpenXml.Bibliography;
+using DocumentFormat.OpenXml.Drawing;
using Elastic.Clients.Elasticsearch;
using IoTClient;
using IoTClient.Clients.Modbus;
@@ -6,17 +7,21 @@
using IoTClient.Common.Enums;
using IoTClient.Enums;
using IoTClient.Interfaces;
+using System.Text;
+using System.Text.RegularExpressions;
namespace WCS.Application;
public class PLCUtil
{
public readonly long PlcId;
+ public readonly string PlcIP;
private SiemensClient _client;
private WcsPlc _modPlc;
private readonly object OLock = new object();
public PLCUtil(WcsPlc modPlc)
{
PlcId = modPlc.Id;
+ PlcIP = modPlc.IP;
_modPlc = modPlc;
_client = new SiemensClient((SiemensVersion)modPlc.PLCType, modPlc.IP, modPlc.Port);
_client.Open();
@@ -39,8 +44,9 @@
/// <param name="DbNumber">DB鍖烘寚瀹氬��</param>
/// <param name="PosType">瀛楃绫诲瀷</param>
/// <param name="Pos">鍋忕Щ閲�/鍦板潃</param>
+ /// <param name="Length">闀垮害锛堝瓧绗︿覆锛�</param>
/// <returns></returns>
- public (IoTClient.Result, dynamic value) GetPlcDBValue(PLCDataTypeEnum PosType, string DbNumber, string Pos)
+ public (IoTClient.Result, dynamic value) GetPlcDBValue(PLCDataTypeEnum PosType, string DbNumber, string Pos, int? Length = 0)
{
lock (OLock)
{
@@ -49,41 +55,72 @@
address = DbNumber + "." + Pos;
else
address = DbNumber + Pos;
+ return this.GetPlcDBValue(PosType, address, Length);
+ }
+ }
+ /// <summary>
+ /// 璇诲彇PLC鍊�
+ /// </summary>
+ /// <param name="PosType">瀛楃绫诲瀷</param>
+ /// <param name="Pos">鍋忕Щ閲�/鍦板潃</param>
+ /// <param name="Length">闀垮害锛堝瓧绗︿覆锛�</param>
+ /// <returns></returns>
+ public (IoTClient.Result, dynamic value) GetPlcDBValue(PLCDataTypeEnum PosType, string Pos, int? Length = 0)
+ {
+ lock (OLock)
+ {
dynamic result = null;
switch (PosType)
{
case PLCDataTypeEnum.Bit:
- result = _client.ReadBoolean(address);
+ result = _client.ReadBoolean(Pos);
break;
case PLCDataTypeEnum.Byte:
- result = _client.ReadByte(address);
+ result = _client.ReadByte(Pos);
break;
case PLCDataTypeEnum.Short:
- result = _client.ReadInt16(address);
+ result = _client.ReadInt16(Pos);
break;
case PLCDataTypeEnum.UShort:
- result = _client.ReadUInt16(address);
+ result = _client.ReadUInt16(Pos);
break;
case PLCDataTypeEnum.Int:
- result = _client.ReadInt32(address);
+ result = _client.ReadInt32(Pos);
break;
case PLCDataTypeEnum.UInt:
- result = _client.ReadUInt32(address);
+ result = _client.ReadUInt32(Pos);
break;
case PLCDataTypeEnum.Long:
- result = _client.ReadInt64(address);
+ result = _client.ReadInt64(Pos);
break;
case PLCDataTypeEnum.ULong:
- result = _client.ReadUInt64(address);
+ result = _client.ReadUInt64(Pos);
break;
case PLCDataTypeEnum.Float:
- result = _client.ReadFloat(address);
+ result = _client.ReadFloat(Pos);
break;
case PLCDataTypeEnum.Double:
- result = _client.ReadDouble(address);
+ result = _client.ReadDouble(Pos);
break;
case PLCDataTypeEnum.String:
- result = _client.ReadString(address);
+ {
+ string dpos = IncrementCode(Pos);
+ Result<byte[]> result1 = _client.ReadString(Pos, 1);
+ if (result1.IsSucceed)
+ {
+ Result<byte[]> result2 = _client.ReadString(dpos, (ushort)(result1.Value[0]));
+ Result<string> result3 = new Result<string>(result2);
+ if (result3.IsSucceed)
+ {
+ result3.Value = Encoding.ASCII.GetString(result2.Value, 0, result1.Value[0]).Replace("\0", "");
+ result = result3;
+ }
+ }
+ else
+ {
+ result = result1;
+ }
+ }
break;
default:
result = new IoTClient.Result<object>();
@@ -91,6 +128,37 @@
}
return (result, result.Value);
}
+ }
+ public static string IncrementCode(string code, int index = 2)
+ {
+ // 浣跨敤姝e垯琛ㄨ揪寮忚В鏋愬瓧绗︿覆
+ var match = Regex.Match(code, @"([A-Za-z]+)(\d+)(?:\.(\d+))?");
+
+ if (match.Success)
+ {
+ // 鎻愬彇瀛楁瘝閮ㄥ垎
+ string prefix = match.Groups[1].Value;
+ // 鎻愬彇鏁板瓧閮ㄥ垎
+ int number1 = int.Parse(match.Groups[2].Value);
+ int number2 = match.Groups[3].Success ? int.Parse(match.Groups[3].Value) : 0;
+
+ // 澶勭悊瑙勫垯
+ if (match.Groups[3].Success)
+ {
+ // 濡傛灉鏈夊皬鏁扮偣鍒嗛殧閮ㄥ垎锛屽鍔�2
+ number2 += index;
+ return $"{prefix}{number1}.{number2:D3}";
+ }
+ else
+ {
+ // 娌℃湁灏忔暟鐐瑰垎闅旈儴鍒嗙殑锛屽鍔�2
+ number1 += index;
+ return $"{prefix}{number1}";
+ }
+ }
+
+ // 濡傛灉涓嶅尮閰嶏紝杩斿洖鍘熷瓧绗︿覆
+ return code;
}
/// <summary>
/// 鎵归噺璇诲彇PLC鍊�
@@ -155,6 +223,13 @@
address = DbNumber + "." + Pos;
else
address = DbNumber + Pos;
+ return this.SetPlcDBValue(PosType, address, Value);
+ }
+ /// <summary>
+ /// 鍐欏叆PLC鍊�
+ /// </summary>
+ public IoTClient.Result SetPlcDBValue(PLCDataTypeEnum PosType, string Pos, string Value)
+ {
switch (PosType)
{
case PLCDataTypeEnum.Bit:
@@ -169,25 +244,30 @@
throw new Exception("鍐欏叆鍊奸敊璇�");
}
}
- return _client.Write(address, bit);
+ return _client.Write(Pos, bit);
case PLCDataTypeEnum.Byte:
- return _client.Write(address, byte.Parse(Value));
+ return _client.Write(Pos, byte.Parse(Value));
case PLCDataTypeEnum.Short:
- return _client.Write(address, short.Parse(Value));
+ return _client.Write(Pos, short.Parse(Value));
case PLCDataTypeEnum.UShort:
- return _client.Write(address, ushort.Parse(Value));
+ return _client.Write(Pos, ushort.Parse(Value));
case PLCDataTypeEnum.Int:
- return _client.Write(address, int.Parse(Value));
+ return _client.Write(Pos, int.Parse(Value));
case PLCDataTypeEnum.UInt:
- return _client.Write(address, uint.Parse(Value));
+ return _client.Write(Pos, uint.Parse(Value));
case PLCDataTypeEnum.Long:
- return _client.Write(address, long.Parse(Value));
+ return _client.Write(Pos, long.Parse(Value));
case PLCDataTypeEnum.ULong:
- return _client.Write(address, ulong.Parse(Value));
+ return _client.Write(Pos, ulong.Parse(Value));
case PLCDataTypeEnum.Float:
- return _client.Write(address, float.Parse(Value));
+ return _client.Write(Pos, float.Parse(Value));
case PLCDataTypeEnum.Double:
- return _client.Write(address, float.Parse(Value));
+ return _client.Write(Pos, float.Parse(Value));
+ case PLCDataTypeEnum.String:
+ {
+ string dpos = IncrementCode(Pos, 1);
+ return _client.Write(dpos, Value);
+ }
default:
return new IoTClient.Result();
}
--
Gitblit v1.8.0