Skip to content

Commit 385f460

Browse files
committed
add more high level fct
1 parent 88bd4eb commit 385f460

File tree

2 files changed

+105
-6
lines changed

2 files changed

+105
-6
lines changed

src/S7CommPlusDriver/Core/Ids.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ namespace S7CommPlusDriver
1717
{
1818
public static class Ids
1919
{
20-
public const int None = 0;
21-
public const int NativeObjects_thePLCProgram_Rid = 3;
20+
public const int None = 0;
21+
public const int NativeObjects_theASRoot_Rid = 1;
22+
public const int NativeObjects_thePLCProgram_Rid = 3;
23+
public const int NativeObjects_theCPUProxy_Rid = 49;
24+
public const int NativeObjects_theCPUCommon_Rid = 50;
2225
public const int NativeObjects_theCPUexecUnit_Rid = 52;
2326
public const int NativeObjects_theIArea_Rid = 80;
2427
public const int NativeObjects_theQArea_Rid = 81;
@@ -145,6 +148,8 @@ public static class Ids
145148
public const int AlarmSubscriptionRef_SendAlarmTexts_Rid = 8173;
146149
public const int ReturnValue = 40305;
147150

151+
public const uint ReleaseMngmtRoot_Rid = 2303328256;
152+
148153
public const uint Constants = 0x8a110000;
149154

150155
public const int TI_BOOL = 0x02000000 + 1;

src/S7CommPlusDriver/S7CommPlusConnectionHighLevel.cs

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Text;
1920

2021
namespace S7CommPlusDriver
2122
{
@@ -187,7 +188,7 @@ public int BrowseAllBlocks(out List<BlockInfo> exploreData)
187188
};
188189

189190
var lang = ((ValueUInt)ob.Attributes[Ids.Block_BlockLanguage]).GetValue();
190-
data.lang = (ProgrammingLanguage) lang;
191+
data.lang = (ProgrammingLanguage)lang;
191192
exploreData.Add(data);
192193
break;
193194
}
@@ -418,8 +419,8 @@ public int GetBlockXml(uint relid, out string blockName, out ProgrammingLanguage
418419
}
419420
}
420421
return 0;
421-
}
422-
422+
}
423+
423424
public int RunExploreRequest(uint relid, uint[] attributes, out List<PObject> objects, byte exploreChildsRecursive = 1, byte exploreParents = 0)
424425
{
425426
int res;
@@ -460,6 +461,99 @@ public int RunExploreRequest(uint relid, uint[] attributes, out List<PObject> ob
460461
objects = exploreRes.Objects;
461462

462463
return 0;
463-
}
464+
}
465+
466+
public int RunGetVarSubstreamedRequest(uint objectId, ushort address, out PValue value)
467+
{
468+
int res;
469+
value = null;
470+
471+
var getVarSubstreamedReq = new GetVarSubstreamedRequest(ProtocolVersion.V2);
472+
getVarSubstreamedReq.InObjectId = objectId;
473+
getVarSubstreamedReq.SessionId = m_SessionId;
474+
getVarSubstreamedReq.Address = address;
475+
res = SendS7plusFunctionObject(getVarSubstreamedReq);
476+
if (res != 0)
477+
{
478+
return res;
479+
}
480+
m_LastError = 0;
481+
WaitForNewS7plusReceived(m_ReadTimeout);
482+
if (m_LastError != 0)
483+
{
484+
return m_LastError;
485+
}
486+
487+
488+
var getVarSubstreamedRes = GetVarSubstreamedResponse.DeserializeFromPdu(m_ReceivedPDU);
489+
if (getVarSubstreamedRes == null)
490+
{
491+
return S7Consts.errIsoInvalidPDU8;
492+
}
493+
494+
value = getVarSubstreamedRes.Value;
495+
496+
return 0;
497+
}
498+
499+
public class CpuInfo
500+
{
501+
public string PlcName { get; set; }
502+
public string ProjectName { get; set; }
503+
504+
public Version VersionTia { get; set; }
505+
public Version Version2 { get; set; }
506+
public string CpuMlfb { get; set; }
507+
public string CpuSerial { get; set; }
508+
public Version CpuFirmware { get; set; }
509+
}
510+
511+
public int GetCpuInfos(out CpuInfo cpuInfo)
512+
{
513+
cpuInfo = null;
514+
515+
var res = this.RunGetVarSubstreamedRequest(Ids.NativeObjects_theASRoot_Rid, 2459, out var pValueVersions);
516+
if (res != 0)
517+
return res;
518+
var arrVersions = ((ValueUSIntArray)pValueVersions).GetValue();
519+
var version1 = new Version(arrVersions[0], arrVersions[1], arrVersions[2], arrVersions[3]);
520+
var version2 = new Version(arrVersions[4], arrVersions[5], arrVersions[6], arrVersions[7]);
521+
522+
res = this.RunGetVarSubstreamedRequest(Ids.NativeObjects_theCPUCommon_Rid, 233, out var pValuePlcName);
523+
if (res != 0)
524+
return res;
525+
var cpuName = ((ValueWString)pValuePlcName).GetValue();
526+
527+
res = this.RunGetVarSubstreamedRequest(Ids.NativeObjects_theCPUProxy_Rid, 3753, out var pValueMlfbSerial);
528+
if (res != 0)
529+
return res;
530+
var data = ((ValueBlob)pValueMlfbSerial).GetValue();
531+
var mlfb = Encoding.ASCII.GetString(data[8..27]);
532+
//space
533+
var serial = Encoding.ASCII.GetString(data[28..44]);
534+
//nulbyte
535+
var hardware = data[46];
536+
var firmware = new Version(data[47], data[48], data[49], data[50]);
537+
//data maybe conatins more:
538+
//rack (1byte), slot (1byte),
539+
540+
res = this.RunGetVarSubstreamedRequest(Ids.ReleaseMngmtRoot_Rid, 8342, out var pValueVersionsAndName);
541+
if (res != 0)
542+
return res;
543+
var versionsAndName = ((ValueWStringArray)pValueVersionsAndName).GetValue();
544+
545+
cpuInfo = new CpuInfo()
546+
{
547+
PlcName = cpuName,
548+
ProjectName = versionsAndName[2],
549+
VersionTia = version1,
550+
Version2 = version2,
551+
CpuMlfb = mlfb,
552+
CpuSerial = serial,
553+
CpuFirmware = firmware
554+
};
555+
556+
return 0;
557+
}
464558
}
465559
}

0 commit comments

Comments
 (0)