beckhoff TE1000 TwinCAT 3 — 6.7.2 Using GetModulePara in C#

Samples | TcCOM Because the bytes do not contain any type information, the caller must know how many bytes the parameter occupies and how they should be interpreted. Without the module's TMC description – or a prior call to GetTypeOfModulePara – the bytes cannot be interpreted correctly. Which function for which situation The following table summarizes which of the three access functions is suitable for which parameter type: GetModuleParaDWORD for scalar parameters For all scalar parameters up to 4 bytes in size, GetModuleParaDWORD is the simplest choice. The function returns an uint directly, so byte arrays are not necessary. The return value is then cast to the parameter's actual type:

var mi = (ITcModuleInstance)treeItem; uint pid = 0x050300B1; uint rawValue = mi.GetModuleParaDWORD(pid); // Cast according to type: int asInt = (int)rawValue; bool asBool = rawValue != 0; GetModulePara + BitConverter for larger types Parameters larger than 4 bytes – such as LREAL (8 bytes) or LINT (8 bytes) – as well as structs cannot be read using GetModuleParaDWORD . Here, you retrieve the raw byte array using GetModulePara and convert it to the target type using BitConverter . It is important to choose the BitConverter method that matches the parameter type, since the bytes are in little-endian format:

var mi = (ITcModuleInstance)treeItem; uint pid = 0x050300B1; byte[] bytes = (byte[])mi.GetModulePara(pid); // LREAL (= double, 8 bytes): double position = BitConverter.ToDouble(bytes, 0); // LINT (= long, 8 bytes): long lint = BitConverter.ToInt64(bytes, 0); // DINT (= int, 4 bytes): int dint = BitConverter.ToInt32(bytes, 0); GetTypeOfModulePara + ITcTypeInstance.Name If the type of a parameter is unknown at development time, it can be determined at runtime using GetTypeOfModulePara . The function returns a ITcTypeInstance object whose .Name property returns the type name directly as a readable string – there is no need to compare it against magic numbers.

Using this type name, you can then select the appropriate conversion for the byte array returned by GetModulePara : var mi = (ITcModuleInstance)treeItem; uint pid = 0x050300B1; mi.GetTypeOfModulePara(pid, out ITcTypeInstance typeInstance); string typeName = typeInstance.Name; // e.g. "LREAL", "UDINT", "BOOL", "DINT" byte[] bytes = (byte[])mi.GetModulePara(pid); object value = typeName switch { TE1000 279 Version: 1.7.0 Situation Recommendation The parameters are BOOL, BYTE, WORD, DWORD, UDINT, UINT, and INT (≤ 4 bytes) GetModuleParaDWORD The parameter is LREAL (8 bytes), LINT (8 bytes), or a struct GetModulePara + BitConverter Type is unknown Call GetTypeOfModulePara first