Skip to content

Commit

Permalink
新增写入指令,单元测试未通过
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jan 8, 2024
1 parent 6de7ce5 commit f6e5b96
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 56 deletions.
11 changes: 6 additions & 5 deletions NewLife.Siemens/Messages/DataItem.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using NewLife.Serialization;
using NewLife.Siemens.Models;

namespace NewLife.Siemens.Messages;

/// <summary>数据项</summary>
public class DataItem
{
#region 属性
/// <summary>错误码。0xFF表示成功</summary>
/// <summary>错误码。0xFF表示成功,写入请求时置零</summary>
public Byte Code { get; set; }

/// <summary>传输大小。BIT</summary>
public Byte TransportSize { get; set; }
/// <summary>变量类型</summary>
public VarType Type { get; set; }

/// <summary>数据</summary>
public Byte[] Data { get; set; }
Expand All @@ -22,7 +23,7 @@ public class DataItem
public void Read(Binary reader)
{
Code = reader.ReadByte();
TransportSize = reader.ReadByte();
Type = (VarType)reader.ReadByte();

var len = reader.ReadUInt16();
Data = reader.ReadBytes(len);
Expand All @@ -33,7 +34,7 @@ public void Read(Binary reader)
public void Writer(Binary writer)
{
writer.WriteByte(Code);
writer.WriteByte(TransportSize);
writer.WriteByte((Byte)Type);

var len = Data?.Length ?? 0;
writer.WriteUInt16((UInt16)len);
Expand Down
71 changes: 71 additions & 0 deletions NewLife.Siemens/Messages/WriteRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using NewLife.Serialization;

namespace NewLife.Siemens.Messages;

/// <summary>写入变量请求</summary>
/// <remarks></remarks>
public class WriteRequest : S7Parameter
{
#region 属性
/// <summary>请求项</summary>
public IList<RequestItem> Items { get; set; } = [];

/// <summary>数据项</summary>
public IList<DataItem> DataItems { get; set; } = [];
#endregion

#region 构造
/// <summary>实例化</summary>
public WriteRequest() => Code = S7Functions.WriteVar;
#endregion

#region 方法
/// <summary>读取</summary>
/// <param name="reader"></param>
protected override void OnRead(Binary reader)
{
var count = reader.ReadByte();

var list = new List<RequestItem>();
for (var i = 0; i < count; i++)
{
var di = new RequestItem();
di.Read(reader);

list.Add(di);
}
Items = list.ToArray();

if (!reader.EndOfStream())
{
var list2 = new List<DataItem>();
for (var i = 0; i < count; i++)
{
var di = new DataItem();
di.Read(reader);

list2.Add(di);
}
DataItems = list2.ToArray();
}
}

/// <summary>写入</summary>
/// <param name="writer"></param>
protected override void OnWrite(Binary writer)
{
var count = Items?.Count ?? 0;
writer.WriteByte((Byte)count);

for (var i = 0; i < count; i++)
{
Items[i].Writer(writer);
}

for (var i = 0; i < count; i++)
{
DataItems[i].Writer(writer);
}
}
#endregion
}
54 changes: 54 additions & 0 deletions NewLife.Siemens/Messages/WriteResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using NewLife.Serialization;

namespace NewLife.Siemens.Messages;

/// <summary>写入变量响应</summary>
/// <remarks></remarks>
public class WriteResponse : S7Parameter
{
#region 属性
///// <summary>项个数</summary>
//public Byte ItemCount { get; set; }

/// <summary>数据项</summary>
public IList<DataItem> Items { get; set; } = [];
#endregion

#region 构造
/// <summary>实例化</summary>
public WriteResponse() => Code = S7Functions.WriteVar;
#endregion

#region 方法
/// <summary>读取</summary>
/// <param name="reader"></param>
protected override void OnRead(Binary reader)
{
var count = reader.ReadByte();

var list = new List<DataItem>();
for (var i = 0; i < count; i++)
{
var di = new DataItem();
di.Read(reader);

list.Add(di);
}

Items = list;
}

/// <summary>写入</summary>
/// <param name="writer"></param>
protected override void OnWrite(Binary writer)
{
var count = Items?.Count ?? 0;
writer.WriteByte((Byte)count);

for (var i = 0; i < count; i++)
{
Items[i].Writer(writer);
}
}
#endregion
}
99 changes: 49 additions & 50 deletions NewLife.Siemens/Models/ErrorCode.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
namespace NewLife.Siemens.Models
namespace NewLife.Siemens.Models;

/// <summary>
/// Types of error code that can be set after a function is called
/// </summary>
public enum ErrorCode
{
/// <summary>
/// Types of error code that can be set after a function is called
/// </summary>
public enum ErrorCode
{
/// <summary>
/// The function has been executed correctly
/// </summary>
NoError = 0,

/// <summary>
/// Wrong type of CPU error
/// </summary>
WrongCPU_Type = 1,

/// <summary>
/// Connection error
/// </summary>
ConnectionError = 2,

/// <summary>
/// Ip address not available
/// </summary>
IPAddressNotAvailable,

/// <summary>
/// Wrong format of the variable
/// </summary>
WrongVarFormat = 10,

/// <summary>
/// Wrong number of received bytes
/// </summary>
WrongNumberReceivedBytes = 11,

/// <summary>
/// Error on send data
/// </summary>
SendData = 20,

/// <summary>
/// Error on read data
/// </summary>
ReadData = 30,

/// <summary>
/// Error on write data
/// </summary>
WriteData = 50
}
/// The function has been executed correctly
/// </summary>
NoError = 0,

/// <summary>
/// Wrong type of CPU error
/// </summary>
WrongCPU_Type = 1,

/// <summary>
/// Connection error
/// </summary>
ConnectionError = 2,

/// <summary>
/// Ip address not available
/// </summary>
IPAddressNotAvailable,

/// <summary>
/// Wrong format of the variable
/// </summary>
WrongVarFormat = 10,

/// <summary>
/// Wrong number of received bytes
/// </summary>
WrongNumberReceivedBytes = 11,

/// <summary>
/// Error on send data
/// </summary>
SendData = 20,

/// <summary>
/// Error on read data
/// </summary>
ReadData = 30,

/// <summary>
/// Error on write data
/// </summary>
WriteData = 50
}
12 changes: 12 additions & 0 deletions NewLife.Siemens/Protocols/S7Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ void ReadParameter(Byte[] buf)
}
break;
case S7Functions.WriteVar:
if (Kind == S7Kinds.AckData)
{
var rv = new WriteResponse();
if (rv.Read(null, reader))
Parameters.Add(rv);
}
else
{
var rv = new WriteRequest();
if (rv.Read(null, reader))
Parameters.Add(rv);
}
break;
default:
throw new NotSupportedException($"不支持的S7参数类型[{kind}]");
Expand Down
Loading

0 comments on commit f6e5b96

Please sign in to comment.