| | |
| | | using IoTClient.Common.Enums; |
| | | using IoTClient.Enums; |
| | | using IoTClient.Interfaces; |
| | | using System.Text; |
| | | using System.Text.RegularExpressions; |
| | | |
| | | namespace WCS.Application; |
| | | public class PLCUtil |
| | |
| | | result = _client.ReadDouble(Pos); |
| | | break; |
| | | case PLCDataTypeEnum.String: |
| | | result = _client.ReadString(Pos, Convert.ToUInt16(Length)); |
| | | { |
| | | 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>(); |
| | |
| | | } |
| | | return (result, result.Value); |
| | | } |
| | | } |
| | | public static string IncrementCode(string code, int index = 2) |
| | | { |
| | | // 使用正则表达式解析字符串 |
| | | 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值 |
| | |
| | | return _client.Write(Pos, float.Parse(Value)); |
| | | case PLCDataTypeEnum.Double: |
| | | 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(); |
| | | } |