2023-04-12 15:40:00 +00:00
|
|
|
|
using S7.Net;
|
2022-01-05 14:37:07 +00:00
|
|
|
|
using System.Text;
|
2022-08-22 04:45:49 +00:00
|
|
|
|
using S7.Net.Types;
|
2023-04-12 15:40:00 +00:00
|
|
|
|
using PluginInterface;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
namespace PLC.SiemensS7
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
|
|
|
|
[DriverSupported("1500")]
|
|
|
|
|
[DriverSupported("1200")]
|
|
|
|
|
[DriverSupported("400")]
|
|
|
|
|
[DriverSupported("300")]
|
|
|
|
|
[DriverSupported("200")]
|
|
|
|
|
[DriverSupported("200Smart")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
[DriverInfo("SiemensS7", "V1.0.0", "Copyright IoTGateway.net 20230220")]
|
|
|
|
|
public class DeviceSiemensS7 : IDriver
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
private Plc _plc;
|
2022-08-06 10:21:00 +00:00
|
|
|
|
|
|
|
|
|
public ILogger _logger { get; set; }
|
2022-08-08 07:15:09 +00:00
|
|
|
|
private readonly string _device;
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2021-12-16 08:25:02 +00:00
|
|
|
|
#region 配置参数
|
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("PLC类型")] public CpuType CpuType { get; set; } = CpuType.S71200;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1";
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("端口号")] public int Port { get; set; } = 102;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("Rack")] public short Rack { get; set; } = 0;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("Slot")] public short Slot { get; set; } = 0;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2022-08-10 08:53:47 +00:00
|
|
|
|
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
#region 生命周期
|
|
|
|
|
|
|
|
|
|
public DeviceSiemensS7(string device, ILogger logger)
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
2022-08-06 10:21:00 +00:00
|
|
|
|
_device = device;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
2022-08-08 07:15:09 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Create()");
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接状态
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsConnected => _plc is { IsConnected: true };
|
2021-12-16 08:25:02 +00:00
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-12-16 08:25:02 +00:00
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Connect()");
|
|
|
|
|
|
|
|
|
|
_plc = new Plc(CpuType, IpAddress, Port, Rack, Slot);
|
|
|
|
|
_plc.ReadTimeout = Timeout;
|
|
|
|
|
_plc.WriteTimeout = Timeout;
|
|
|
|
|
_plc.Open();
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Connect()");
|
2021-12-16 08:25:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2021-12-16 08:25:02 +00:00
|
|
|
|
return IsConnected;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 断开
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-12-16 08:25:02 +00:00
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Close()");
|
|
|
|
|
|
|
|
|
|
_plc?.Close();
|
2021-12-16 08:25:02 +00:00
|
|
|
|
return !IsConnected;
|
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogError(ex, $"Device:[{_device}],Close(),Error");
|
2021-12-16 08:25:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 释放
|
|
|
|
|
/// </summary>
|
2021-12-16 08:25:02 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogInformation($"Device:[{_device}],Dispose()");
|
|
|
|
|
|
|
|
|
|
_plc = null;
|
|
|
|
|
|
|
|
|
|
// Suppress finalization.
|
|
|
|
|
GC.SuppressFinalize(this);
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
catch (Exception ex)
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
_logger.LogError(ex, $"Device:[{_device}],Dispose(),Error");
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 读写方法
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ioArg"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-01-05 14:37:07 +00:00
|
|
|
|
[Method("读西门子PLC标准地址", description: "读西门子PLC标准地址")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
if (_plc is { IsConnected: true })
|
2021-12-16 08:25:02 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
if (ioArg.ValueType == DataTypeEnum.AsciiString)
|
2022-08-22 04:45:49 +00:00
|
|
|
|
{
|
2022-11-02 04:33:30 +00:00
|
|
|
|
var str = string.Empty;
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var dataItem = S7.Net.Types.DataItem.FromAddress(ioArg.Address);
|
|
|
|
|
var head = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, 2);
|
|
|
|
|
var strBytes = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr + 2, head[1]);
|
2022-11-02 04:33:30 +00:00
|
|
|
|
var strRaw = Encoding.ASCII.GetString(strBytes).TrimEnd(new char[] { '\0' });
|
|
|
|
|
if (strRaw.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var chart in strRaw)
|
|
|
|
|
{
|
|
|
|
|
if (chart >= 0x20 && chart <= 0x7E)
|
|
|
|
|
str += chart;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret.Value = strRaw;
|
|
|
|
|
|
2022-08-22 04:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2023-04-12 15:40:00 +00:00
|
|
|
|
ret.Value = _plc.Read(ioArg.Address);
|
|
|
|
|
if (ioArg.ValueType == DataTypeEnum.Float)
|
2022-01-05 14:37:07 +00:00
|
|
|
|
{
|
|
|
|
|
var buffer = new byte[4];
|
|
|
|
|
|
|
|
|
|
buffer[3] = (byte)((uint)ret.Value >> 24);
|
|
|
|
|
buffer[2] = (byte)((uint)ret.Value >> 16);
|
|
|
|
|
buffer[1] = (byte)((uint)ret.Value >> 8);
|
|
|
|
|
buffer[0] = (byte)((uint)ret.Value >> 0);
|
|
|
|
|
ret.Value = BitConverter.ToSingle(buffer, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = $"读取失败,{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2022-01-05 14:37:07 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ioArg"></param>
|
|
|
|
|
/// <returns></returns>
|
2022-08-22 04:45:49 +00:00
|
|
|
|
[Method("读西门子字节字符串", description: "DB10.DBW6,10 即开始地址,字节长度")]
|
2023-04-12 15:40:00 +00:00
|
|
|
|
public DriverReturnValueModel ReadByteString(DriverAddressIoArgModel ioArg)
|
2022-01-05 14:37:07 +00:00
|
|
|
|
{
|
|
|
|
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
if (_plc != null && _plc.IsConnected)
|
2022-01-05 14:37:07 +00:00
|
|
|
|
{
|
2022-08-22 04:45:49 +00:00
|
|
|
|
var str = string.Empty;
|
2022-01-05 14:37:07 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var arrParams = ioArg.Address.Trim().Split(',');
|
2022-11-02 04:33:30 +00:00
|
|
|
|
if (arrParams.Length == 2)
|
2022-08-22 04:45:49 +00:00
|
|
|
|
{
|
|
|
|
|
var dataItemitem = S7.Net.Types.DataItem.FromAddress(arrParams[0]);
|
|
|
|
|
int.TryParse(arrParams[1], out var length);
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var data = _plc.ReadBytes(dataItemitem.DataType, dataItemitem.DB, dataItemitem.StartByteAdr, length);
|
2022-11-02 04:33:30 +00:00
|
|
|
|
var strRaw = Encoding.ASCII.GetString(data).TrimEnd(new char[] { '\0' });
|
|
|
|
|
if (strRaw.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var chart in strRaw)
|
|
|
|
|
{
|
|
|
|
|
if (chart >= 0x20 && chart <= 0x7E)
|
|
|
|
|
str += chart;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-22 04:45:49 +00:00
|
|
|
|
}
|
2022-11-02 04:33:30 +00:00
|
|
|
|
else
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.AddressError;
|
2022-07-18 15:04:33 +00:00
|
|
|
|
ret.Value = str;
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = $"读取失败,{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2021-12-16 08:25:02 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 写
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="requestId"></param>
|
|
|
|
|
/// <param name="method"></param>
|
|
|
|
|
/// <param name="ioArg"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg)
|
2022-04-13 09:01:24 +00:00
|
|
|
|
{
|
2022-07-18 15:04:33 +00:00
|
|
|
|
RpcResponse rpcResponse = new() { IsSuccess = false };
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!IsConnected)
|
|
|
|
|
rpcResponse.Description = "设备连接已断开";
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-08-10 08:53:47 +00:00
|
|
|
|
object? toWrite = null;
|
2023-04-12 15:40:00 +00:00
|
|
|
|
switch (ioArg.ValueType)
|
2022-07-18 15:04:33 +00:00
|
|
|
|
{
|
|
|
|
|
case DataTypeEnum.Bit:
|
|
|
|
|
case DataTypeEnum.Bool:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = ioArg.Value.ToString()?.ToLower() == "true" ||
|
|
|
|
|
ioArg.Value.ToString()?.ToLower() == "1";
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.UByte:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = byte.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Byte:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = sbyte.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Uint16:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = ushort.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int16:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = short.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Uint32:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = uint.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int32:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = int.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Float:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = float.Parse(ioArg.Value.ToString());
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.AsciiString:
|
2023-04-12 15:40:00 +00:00
|
|
|
|
toWrite = GetStringBytes(ioArg);
|
2022-07-18 15:04:33 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
rpcResponse.Description = $"类型{DataTypeEnum.Float}不支持写入";
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2022-07-18 15:04:33 +00:00
|
|
|
|
if (toWrite == null)
|
2022-08-22 04:45:49 +00:00
|
|
|
|
{
|
|
|
|
|
rpcResponse.Description = "解析错误";
|
2022-07-18 15:04:33 +00:00
|
|
|
|
return rpcResponse;
|
2022-08-22 04:45:49 +00:00
|
|
|
|
}
|
2022-07-18 15:04:33 +00:00
|
|
|
|
|
|
|
|
|
//通用方法
|
2022-08-10 08:53:47 +00:00
|
|
|
|
if (method == nameof(Read))
|
2022-07-18 15:04:33 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var dataItem = DataItem.FromAddress(ioArg.Address);
|
|
|
|
|
if (ioArg.ValueType == DataTypeEnum.AsciiString)
|
2022-11-02 04:33:30 +00:00
|
|
|
|
{
|
|
|
|
|
//先写入长度
|
2023-04-12 15:40:00 +00:00
|
|
|
|
await _plc?.WriteAsync(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr + 1,
|
2022-11-02 04:33:30 +00:00
|
|
|
|
(byte)((byte[])toWrite).Length);
|
|
|
|
|
//在写入字符串内容
|
2023-04-12 15:40:00 +00:00
|
|
|
|
await _plc?.WriteAsync(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr + 2, (byte[])toWrite);
|
2022-11-02 04:33:30 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-04-12 15:40:00 +00:00
|
|
|
|
await _plc?.WriteAsync(ioArg.Address, toWrite);
|
2022-07-18 15:04:33 +00:00
|
|
|
|
|
|
|
|
|
rpcResponse.IsSuccess = true;
|
|
|
|
|
return rpcResponse;
|
|
|
|
|
}
|
2022-11-02 04:33:30 +00:00
|
|
|
|
//数组字符串
|
|
|
|
|
if (method == nameof(ReadByteString))
|
2022-07-18 15:04:33 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var arrParams = ioArg.Address.Trim().Split(',');
|
2022-08-22 04:45:49 +00:00
|
|
|
|
if (arrParams.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
var dataItem = DataItem.FromAddress(arrParams[0]);
|
2023-04-12 15:40:00 +00:00
|
|
|
|
await _plc?.WriteAsync(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, toWrite);
|
2022-08-22 04:45:49 +00:00
|
|
|
|
rpcResponse.IsSuccess = true;
|
|
|
|
|
return rpcResponse;
|
|
|
|
|
}
|
2022-11-02 04:33:30 +00:00
|
|
|
|
else
|
|
|
|
|
rpcResponse.Description = $"地址错误:{method}";
|
2022-07-18 15:04:33 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2022-08-10 08:53:47 +00:00
|
|
|
|
rpcResponse.Description = $"不支持写入:{method}";
|
2022-07-18 15:04:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
rpcResponse.Description = $"写入失败,[method]:{method},[ioArg]:{ioArg},[ex]:{ex}";
|
2022-07-18 15:04:33 +00:00
|
|
|
|
}
|
2022-08-10 08:53:47 +00:00
|
|
|
|
|
2022-04-13 09:01:24 +00:00
|
|
|
|
return rpcResponse;
|
|
|
|
|
}
|
2022-08-22 04:45:49 +00:00
|
|
|
|
|
2023-04-12 15:40:00 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 私有方法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private byte[]? GetStringBytes(DriverAddressIoArgModel ioArg)
|
2022-08-22 04:45:49 +00:00
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var toWriteString = ioArg.Value.ToString();
|
2022-08-22 04:45:49 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var arrParams = ioArg.Address.Trim().Split(',');
|
2022-08-22 04:45:49 +00:00
|
|
|
|
int length = 0;//最大长度,因为字符串后面得补满'\0'
|
|
|
|
|
//直接读取byte[]的方式
|
|
|
|
|
if (arrParams.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
//如DB100.DBW23,10
|
|
|
|
|
int.TryParse(arrParams[1], out length);
|
2022-11-02 04:33:30 +00:00
|
|
|
|
if (toWriteString.Length > length)
|
|
|
|
|
toWriteString = toWriteString.Take(length).ToString();
|
|
|
|
|
if (toWriteString.Length < length)
|
|
|
|
|
toWriteString = toWriteString.PadRight(length, '\0');
|
2022-08-22 04:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
//使用西门子String读取
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//如DB100.DBW23
|
2023-04-12 15:40:00 +00:00
|
|
|
|
var dataItem = DataItem.FromAddress(ioArg.Address);
|
|
|
|
|
var head = _plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, 2);
|
2022-08-22 04:45:49 +00:00
|
|
|
|
length = head[0];
|
2022-11-02 04:33:30 +00:00
|
|
|
|
if (toWriteString.Length > length)
|
|
|
|
|
toWriteString = toWriteString.Take(length).ToString();
|
2022-08-22 04:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("字符串解析异常");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Encoding.ASCII.GetBytes(toWriteString);
|
|
|
|
|
}
|
2023-04-12 15:40:00 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2021-12-16 08:25:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|