优化了西门子plc通讯套件,适配hsl测试demo,增加浮点数和字符串解析

This commit is contained in:
dd 2022-01-05 22:37:07 +08:00
parent ad30078f2e
commit 9d7aa0d235
9 changed files with 44 additions and 3 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -80,7 +80,7 @@ namespace S7.Net
throw new WrongNumberOfBytesException("Not enough data received in response to Communication Setup"); throw new WrongNumberOfBytesException("Not enough data received in response to Communication Setup");
//Check for S7 Ack Data //Check for S7 Ack Data
if (s7data[1] != 0x03) if (s7data[1] != 0x03&& s7data[1] != 0x01)
throw new InvalidDataException("Error reading Communication Setup response", s7data, 1, 0x03); throw new InvalidDataException("Error reading Communication Setup response", s7data, 1, 0x03);
if (s7data.Length < 20) if (s7data.Length < 20)

View File

@ -1,6 +1,7 @@
using PluginInterface; using PluginInterface;
using S7.Net; using S7.Net;
using System; using System;
using System.Text;
namespace DriverSiemensS7 namespace DriverSiemensS7
{ {
@ -45,7 +46,6 @@ namespace DriverSiemensS7
public SiemensS7(Guid deviceId) public SiemensS7(Guid deviceId)
{ {
DeviceId = deviceId; DeviceId = deviceId;
plc = new Plc(CpuType, IpAddress, Port, Rack, Slot);
} }
@ -61,6 +61,7 @@ namespace DriverSiemensS7
{ {
try try
{ {
plc = new Plc(CpuType, IpAddress, Port, Rack, Slot);
plc.Open(); plc.Open();
} }
catch (Exception) catch (Exception)
@ -96,7 +97,7 @@ namespace DriverSiemensS7
} }
} }
[Method("读西门子PLC", description: "读西门子PLC")] [Method("读西门子PLC标准地址", description: "读西门子PLC标准地址")]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioarg) public DriverReturnValueModel Read(DriverAddressIoArgModel ioarg)
{ {
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good }; var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
@ -106,6 +107,46 @@ namespace DriverSiemensS7
try try
{ {
ret.Value = plc.Read(ioarg.Address); ret.Value = plc.Read(ioarg.Address);
if (ioarg.ValueType == DataTypeEnum.Float)
{
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 = "连接失败";
}
return ret;
}
[Method("读字符串", description: "读字符串")]
public DriverReturnValueModel ReadString(DriverAddressIoArgModel ioarg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (plc != null && plc.IsConnected)
{
try
{
int db = int.Parse(ioarg.Address.Trim().Split(',')[0]);
int startAdr = int.Parse(ioarg.Address.Trim().Split(',')[1]);
int count = int.Parse(ioarg.Address.Trim().Split(',')[2]);
var buffers = plc.ReadBytes(DataType.DataBlock, db, startAdr, count);
ret.Value = Encoding.ASCII.GetString(buffers);
} }
catch (Exception ex) catch (Exception ex)
{ {

Binary file not shown.

Binary file not shown.