Skip to content

Commit 37b44c1

Browse files
committed
Add SetVariableRequest and SetVariableResponse function codes
1 parent 1705beb commit 37b44c1

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#region License
2+
/******************************************************************************
3+
* S7CommPlusDriver
4+
*
5+
* Copyright (C) 2023 Thomas Wiens, [email protected]
6+
*
7+
* This file is part of S7CommPlusDriver.
8+
*
9+
* S7CommPlusDriver is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
/****************************************************************************/
14+
#endregion
15+
16+
using System;
17+
using System.IO;
18+
19+
namespace S7CommPlusDriver
20+
{
21+
public class SetVariableRequest : IS7pSendableObject
22+
{
23+
public byte ProtocolVersion;
24+
public UInt16 SequenceNumber;
25+
public UInt32 SessionId;
26+
byte TransportFlags = 0x34;
27+
28+
public UInt32 InObjectId;
29+
30+
public UInt32 Address;
31+
public PValue Value;
32+
33+
public bool WithIntegrityId = true;
34+
public UInt32 IntegrityId;
35+
36+
public SetVariableRequest(byte protocolVersion)
37+
{
38+
ProtocolVersion = protocolVersion;
39+
}
40+
41+
public byte GetProtocolVersion()
42+
{
43+
return ProtocolVersion;
44+
}
45+
46+
public int Serialize(Stream buffer)
47+
{
48+
UInt32 i;
49+
int ret = 0;
50+
ret += S7p.EncodeByte(buffer, Opcode.Request);
51+
ret += S7p.EncodeUInt16(buffer, 0); // Reserved
52+
ret += S7p.EncodeUInt16(buffer, Functioncode.SetVariable);
53+
ret += S7p.EncodeUInt16(buffer, 0); // Reserved
54+
ret += S7p.EncodeUInt16(buffer, SequenceNumber);
55+
ret += S7p.EncodeUInt32(buffer, SessionId);
56+
ret += S7p.EncodeByte(buffer, TransportFlags);
57+
58+
// Request set
59+
ret += S7p.EncodeUInt32(buffer, InObjectId);
60+
ret += S7p.EncodeUInt32Vlq(buffer, 1); // Immer 1 (?)
61+
ret += S7p.EncodeUInt32Vlq(buffer, Address);
62+
ret += Value.Serialize(buffer);
63+
64+
ret += S7p.EncodeObjectQualifier(buffer);
65+
// 1 Byte unbekannter Funktion
66+
ret += S7p.EncodeByte(buffer, 0x00);
67+
68+
if (WithIntegrityId)
69+
{
70+
ret += S7p.EncodeUInt32Vlq(buffer, IntegrityId);
71+
}
72+
73+
// Füllbytes?
74+
ret += S7p.EncodeUInt32(buffer, 0);
75+
76+
return ret;
77+
}
78+
79+
public override string ToString()
80+
{
81+
string s = "";
82+
s += "<SetVariableRequest>" + Environment.NewLine;
83+
s += "<ProtocolVersion>" + ProtocolVersion.ToString() + "</ProtocolVersion>" + Environment.NewLine;
84+
s += "<SequenceNumber>" + SequenceNumber.ToString() + "</SequenceNumber>" + Environment.NewLine;
85+
s += "<SessionId>" + SessionId.ToString() + "</SessionId>" + Environment.NewLine;
86+
s += "<TransportFlags>" + TransportFlags.ToString() + "</TransportFlags>" + Environment.NewLine;
87+
s += "<RequestSet>" + Environment.NewLine;
88+
s += "<InObjectId>" + InObjectId.ToString() + "</InObjectId>" + Environment.NewLine;
89+
s += "<AddressList>" + Environment.NewLine;
90+
s += "<Id>" + Address.ToString() + "</Id>" + Environment.NewLine;
91+
s += "</AddressList>" + Environment.NewLine;
92+
s += "<ValueList>" + Environment.NewLine;
93+
s += "<Value>" + Value.ToString() + "</Value>" + Environment.NewLine;
94+
s += "</ValueList>" + Environment.NewLine;
95+
s += "</RequestSet>" + Environment.NewLine;
96+
s += "</SetVariableRequest>" + Environment.NewLine;
97+
return s;
98+
}
99+
}
100+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#region License
2+
/******************************************************************************
3+
* S7CommPlusDriver
4+
*
5+
* Copyright (C) 2023 Thomas Wiens, [email protected]
6+
*
7+
* This file is part of S7CommPlusDriver.
8+
*
9+
* S7CommPlusDriver is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
/****************************************************************************/
14+
#endregion
15+
16+
using System;
17+
using System.IO;
18+
19+
namespace S7CommPlusDriver
20+
{
21+
public class SetVariableResponse
22+
{
23+
public byte ProtocolVersion;
24+
public UInt16 SequenceNumber;
25+
public byte TransportFlags;
26+
public UInt64 ReturnValue;
27+
public UInt32 IntegrityId;
28+
29+
public SetVariableResponse(byte protocolVersion)
30+
{
31+
ProtocolVersion = protocolVersion;
32+
}
33+
34+
public int Deserialize(Stream buffer)
35+
{
36+
int ret = 0;
37+
38+
ret += S7p.DecodeUInt16(buffer, out SequenceNumber);
39+
ret += S7p.DecodeByte(buffer, out TransportFlags);
40+
41+
// Response Set
42+
ret += S7p.DecodeUInt64Vlq(buffer, out ReturnValue);
43+
44+
ret += S7p.DecodeUInt32Vlq(buffer, out IntegrityId);
45+
return ret;
46+
}
47+
48+
public override string ToString()
49+
{
50+
string s = "";
51+
s += "<SetVariableResponse>" + Environment.NewLine;
52+
s += "<ProtocolVersion>" + ProtocolVersion.ToString() + "</ProtocolVersion>" + Environment.NewLine;
53+
s += "<SequenceNumber>" + SequenceNumber.ToString() + "</SequenceNumber>" + Environment.NewLine;
54+
s += "<TransportFlags>" + TransportFlags.ToString() + "</TransportFlags>" + Environment.NewLine;
55+
s += "<ResponseSet>" + Environment.NewLine;
56+
s += "<ReturnValue>" + ReturnValue.ToString() + "</ReturnValue>" + Environment.NewLine;
57+
s += "</ResponseSet>" + Environment.NewLine;
58+
s += "<IntegrityId>" + IntegrityId.ToString() + "</IntegrityId>" + Environment.NewLine;
59+
s += "</SetVariableResponse>" + Environment.NewLine;
60+
return s;
61+
}
62+
63+
public static SetVariableResponse DeserializeFromPdu(Stream pdu)
64+
{
65+
byte protocolVersion;
66+
byte opcode;
67+
UInt16 function;
68+
UInt16 reserved;
69+
// ProtocolVersion wird vorab als ein Byte in den Stream geschrieben, Sonderbehandlung
70+
S7p.DecodeByte(pdu, out protocolVersion);
71+
S7p.DecodeByte(pdu, out opcode);
72+
if (opcode != Opcode.Response)
73+
{
74+
return null;
75+
}
76+
S7p.DecodeUInt16(pdu, out reserved);
77+
S7p.DecodeUInt16(pdu, out function);
78+
S7p.DecodeUInt16(pdu, out reserved);
79+
if (function != Functioncode.SetVariable)
80+
{
81+
return null;
82+
}
83+
SetVariableResponse resp = new SetVariableResponse(protocolVersion);
84+
resp.Deserialize(pdu);
85+
86+
return resp;
87+
}
88+
}
89+
}

src/S7CommPlusDriver/S7CommPlusDriver.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
<Compile Include="ClientApi\Browser.cs" />
5050
<Compile Include="Core\IS7pSerialize.cs" />
5151
<Compile Include="Core\ProtocolVersion.cs" />
52+
<Compile Include="Core\SetVariableResponse.cs" />
53+
<Compile Include="Core\SetVariableRequest.cs" />
5254
<Compile Include="Net\MsgSocket.cs" />
5355
<Compile Include="Net\S7Client.cs" />
5456
<Compile Include="Net\S7Consts.cs" />

0 commit comments

Comments
 (0)