Skip to content

Commit

Permalink
解析部分S7数据
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 28, 2023
1 parent ed1fe43 commit 4f1ef76
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
61 changes: 41 additions & 20 deletions NewLife.Siemens/Protocols/COTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Boolean IAccessor.Read(Stream stream, Object context)

Boolean IAccessor.Write(Stream stream, Object context)
{
var pk = context as Packet;
stream ??= pk?.GetStream();
var writer = new Binary { Stream = stream, IsLittleEndian = false };

switch (Type)
{
case PduType.Data:
Expand All @@ -129,33 +133,50 @@ Boolean IAccessor.Write(Stream stream, Object context)
}
break;
case PduType.ConnectionRequest:
{
var len = 2;
stream.WriteByte((Byte)len);
stream.WriteByte((Byte)Type);

var flags = (Byte)(Number & 0x7F);
if (LastDataUnit) flags |= 0x80;
stream.WriteByte(flags);

Data?.CopyTo(stream);
}
break;
case PduType.ConnectionConfirmed:
default:
{
var len = 2;
stream.WriteByte((Byte)len);
stream.WriteByte((Byte)Type);
writer.WriteByte((Byte)len);
writer.WriteByte((Byte)Type);

var flags = (Byte)(Number & 0x7F);
if (LastDataUnit) flags |= 0x80;
stream.WriteByte(flags);
writer.Write(Destination);
writer.Write(Source);
writer.WriteByte(Option);

Data?.CopyTo(stream);
var ps = Parameters;
if (ps != null)
{
foreach (var item in ps)
{
writer.WriteByte(item.Type);

if (item.Value is Byte b)
{
writer.WriteByte(1);
writer.WriteByte(b);
}
else if (item.Value is UInt16 u16)
{
writer.WriteByte(2);
writer.Write(u16);
}
else if (item.Value is UInt32 u32)
{
writer.WriteByte(4);
writer.Write(u32);
}
else if (item.Value is Byte[] buf)
{
writer.WriteByte((Byte)buf.Length);
writer.Write(buf);
}
else
throw new NotSupportedException();
}
}
}
break;
default:
break;
}

return true;
Expand Down
57 changes: 57 additions & 0 deletions XUnitTest/COTPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ public void DecodeCR()
Assert.Equal(0x0A, (Byte)ps[2].Value);
}

[Fact]
public void DecodeCR2()
{
Byte[] plcHead1_200smart = [3, 0, 0, 22, 17, 224, 0, 0, 0, 1, 0, 193, 2, 16, 0, 194, 2, 3, 0, 192, 1, 10];
var pk = new Packet(plcHead1_200smart);

// 前面有TPKT头
var tpkt = new TPKT();
tpkt.Read(pk);
Assert.Equal(3, tpkt.Version);
Assert.Equal(0, tpkt.Reserved);
Assert.Equal(0x16, tpkt.Length);

var cotp = new COTP();
var rs = cotp.Read(tpkt.Data);
Assert.True(rs);
Assert.Equal(PduType.ConnectionRequest, cotp.Type);
Assert.Equal(0x0000, cotp.Destination);
Assert.Equal(0x0001, cotp.Source);
Assert.Equal(0x00, cotp.Option);

var ps = cotp.Parameters;
Assert.NotEmpty(ps);

Assert.Equal(0xC1, ps[0].Type);
Assert.Equal(0x1000, (UInt16)ps[0].Value);

Assert.Equal(0xC2, ps[1].Type);
Assert.Equal(0x0300, (UInt16)ps[1].Value);

Assert.Equal(0xC0, ps[2].Type);
Assert.Equal(0x0A, (Byte)ps[2].Value);
}

[Fact]
public void DecodeCC()
{
Expand Down Expand Up @@ -94,6 +128,29 @@ public void DecodeCC()
Assert.Equal(0x0300, (UInt16)ps[2].Value);
}

[Fact]
public void DecodeDT()
{
Byte[] plcHead2_200smart = [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 204, 193, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 3, 192];
var pk = new Packet(plcHead2_200smart);

// 前面有TPKT头
var tpkt = new TPKT();
tpkt.Read(pk);
Assert.Equal(3, tpkt.Version);
Assert.Equal(0, tpkt.Reserved);
Assert.Equal(25, tpkt.Length);

var cotp = new COTP();
var rs = cotp.Read(tpkt.Data);
Assert.True(rs);
Assert.Equal(PduType.Data, cotp.Type);
Assert.True(cotp.LastDataUnit);

Assert.NotNull(cotp.Data);
Assert.Equal(17, cotp.Data.Total);
}

[Fact]
public void ConnectTest()
{
Expand Down

0 comments on commit 4f1ef76

Please sign in to comment.