西门子支持两种字符串读取和下发
This commit is contained in:
parent
a1128ce109
commit
feb09b14ae
Binary file not shown.
@ -2,6 +2,8 @@
|
|||||||
using S7.Net;
|
using S7.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using S7.Net.Types;
|
||||||
|
|
||||||
namespace DriverSiemensS7
|
namespace DriverSiemensS7
|
||||||
{
|
{
|
||||||
@ -102,7 +104,15 @@ namespace DriverSiemensS7
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ret.Value = plc.Read(ioarg.Address);
|
if (ioarg.ValueType == DataTypeEnum.AsciiString)
|
||||||
|
{
|
||||||
|
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]);
|
||||||
|
ret.Value = Encoding.ASCII.GetString(strBytes).TrimEnd(new char[] { '\0' }); ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ret.Value = plc.Read(ioarg.Address);
|
||||||
if (ioarg.ValueType == DataTypeEnum.Float)
|
if (ioarg.ValueType == DataTypeEnum.Float)
|
||||||
{
|
{
|
||||||
var buffer = new byte[4];
|
var buffer = new byte[4];
|
||||||
@ -129,22 +139,25 @@ namespace DriverSiemensS7
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Method("读字符串", description: "读字符串")]
|
[Method("读西门子字节字符串", description: "DB10.DBW6,10 即开始地址,字节长度")]
|
||||||
public DriverReturnValueModel ReadString(DriverAddressIoArgModel ioarg)
|
public DriverReturnValueModel ReadByteString(DriverAddressIoArgModel ioarg)
|
||||||
{
|
{
|
||||||
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
|
||||||
|
|
||||||
if (plc != null && plc.IsConnected)
|
if (plc != null && plc.IsConnected)
|
||||||
{
|
{
|
||||||
|
var str = string.Empty;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
int db = int.Parse(ioarg.Address.Trim().Split(',')[0]);
|
var arrParams = ioarg.Address.Trim().Split(',');
|
||||||
int startAdr = int.Parse(ioarg.Address.Trim().Split(',')[1]);
|
if (arrParams.Length ==2)
|
||||||
int count = int.Parse(ioarg.Address.Trim().Split(',')[2]);
|
{
|
||||||
var buffers = plc.ReadBytes(DataType.DataBlock, db, startAdr, count);
|
var dataItemitem = S7.Net.Types.DataItem.FromAddress(arrParams[0]);
|
||||||
var str = Encoding.ASCII.GetString(buffers);
|
int.TryParse(arrParams[1], out var length);
|
||||||
if (str.Contains('\0'))
|
|
||||||
str = str.Split('\0')[0];
|
var data = plc.ReadBytes(dataItemitem.DataType, dataItemitem.DB, dataItemitem.StartByteAdr, length);
|
||||||
|
str = Encoding.ASCII.GetString(data).TrimEnd(new char[] { '\0' });
|
||||||
|
}
|
||||||
ret.Value = str;
|
ret.Value = str;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -260,7 +273,7 @@ namespace DriverSiemensS7
|
|||||||
toWrite = float.Parse(ioarg.Value.ToString());
|
toWrite = float.Parse(ioarg.Value.ToString());
|
||||||
break;
|
break;
|
||||||
case DataTypeEnum.AsciiString:
|
case DataTypeEnum.AsciiString:
|
||||||
toWrite = Encoding.ASCII.GetBytes(ioarg.Value.ToString());
|
toWrite = GetStringBytes(ioarg);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rpcResponse.Description = $"类型{DataTypeEnum.Float}不支持写入";
|
rpcResponse.Description = $"类型{DataTypeEnum.Float}不支持写入";
|
||||||
@ -268,7 +281,10 @@ namespace DriverSiemensS7
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (toWrite == null)
|
if (toWrite == null)
|
||||||
|
{
|
||||||
|
rpcResponse.Description = "解析错误";
|
||||||
return rpcResponse;
|
return rpcResponse;
|
||||||
|
}
|
||||||
|
|
||||||
//通用方法
|
//通用方法
|
||||||
if (method == nameof(Read))
|
if (method == nameof(Read))
|
||||||
@ -279,18 +295,16 @@ namespace DriverSiemensS7
|
|||||||
return rpcResponse;
|
return rpcResponse;
|
||||||
}
|
}
|
||||||
//字符串
|
//字符串
|
||||||
else if (method == nameof(ReadString))
|
else if (method == nameof(ReadByteString))
|
||||||
{
|
{
|
||||||
int db = int.Parse(ioarg.Address.Trim().Split(',')[0]);
|
var arrParams = ioarg.Address.Trim().Split(',');
|
||||||
int startAdr = int.Parse(ioarg.Address.Trim().Split(',')[1]);
|
if (arrParams.Length == 2)
|
||||||
int count = int.Parse(ioarg.Address.Trim().Split(',')[2]);
|
{
|
||||||
//防止写入到其他地址 进行截断
|
var dataItem = DataItem.FromAddress(arrParams[0]);
|
||||||
if (((byte[])toWrite).Length > count)
|
plc?.Write(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, toWrite);
|
||||||
toWrite = ((byte[])toWrite).Take(count);
|
rpcResponse.IsSuccess = true;
|
||||||
plc?.Write(DataType.DataBlock, db, startAdr, toWrite);
|
return rpcResponse;
|
||||||
|
}
|
||||||
rpcResponse.IsSuccess = true;
|
|
||||||
return rpcResponse;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
rpcResponse.Description = $"不支持写入:{method}";
|
rpcResponse.Description = $"不支持写入:{method}";
|
||||||
@ -303,5 +317,40 @@ namespace DriverSiemensS7
|
|||||||
|
|
||||||
return rpcResponse;
|
return rpcResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private byte[]? GetStringBytes(DriverAddressIoArgModel ioarg)
|
||||||
|
{
|
||||||
|
var toWriteString = ioarg.Value.ToString();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var arrParams = ioarg.Address.Trim().Split(',');
|
||||||
|
int length = 0;//最大长度,因为字符串后面得补满'\0'
|
||||||
|
//直接读取byte[]的方式
|
||||||
|
if (arrParams.Length == 2)
|
||||||
|
{
|
||||||
|
//如DB100.DBW23,10
|
||||||
|
int.TryParse(arrParams[1], out length);
|
||||||
|
}
|
||||||
|
//使用西门子String读取
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//如DB100.DBW23
|
||||||
|
var dataItem = DataItem.FromAddress(ioarg.Address);
|
||||||
|
var head = plc.ReadBytes(dataItem.DataType, dataItem.DB, dataItem.StartByteAdr, 2);
|
||||||
|
length = head[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toWriteString.Length > length)
|
||||||
|
toWriteString = toWriteString.Take(length).ToString();
|
||||||
|
if (toWriteString.Length < length)
|
||||||
|
toWriteString = toWriteString.PadRight(length, '\0');
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new Exception("字符串解析异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Encoding.ASCII.GetBytes(toWriteString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user