Data converter

Data converters are required to read or write the byte data available via GInOut in a typed manner, e.g. as an integer.

The following converters are available in Viper.NET:

Most converters support the parameter

TwistBytes

[True|False] With True the byte order is reversed (most significant byte first). With False the byte order remains unchanged (least significant byte first).

Exceptions: The string, byte-array and bits converters do not support TwistBytes. For the GenericBitArrayConverter, TwistBytes only affects the 4 length bytes (with FixedCount=False).

Example:

Int16 val = 5;
Byte-Daten [TwistBytes = False]: [0x05][0x00]
Byte-Daten [TwistBytes = True]:  [0x00][0x05]

For some converters additional parameters are required.

GenericInputConverter

For the GenericInputConverter, in contrast to the other converters, the target type must also be specified. The following target types are available:

Target type

data length in bytes

Boolean

1

Byte

1

SByte

1

Char

2

Int16

2

Int32

4

Int64

8

UInt16

2

UInt32

4

UInt64

8

Single (=float)

4

Double

8

Decimal

16

Care must be taken to ensure that the expected data length is maintained.

Byte array -> Byte

This converter has been replaced by the GenericInputConverter (target type Byte) and is only present for backward compatibility.

Byte array -> Byte array

The byte data are taken over 1:1. However, it can happen that the data lengths do not match. A distinction must be made between two cases here:

  • The data area in GInOut is larger than or equal to the byte data to be written.

    • The data area in GInOut is filled with “MemSetVal”.

  • The data area in GInOut is smaller than the data to be written:

    • Invalid, generates an error (The data cannot be written).

Care must therefore be taken to ensure that the data area is sufficiently large.

Additional converter parameters:

Length

Maximum length of the byte array. Shorter data is filled with MemSetVal. With “-1” the length of the data area is used.

MemSetVal

This byte value is used to fill the data area if the data to be written is shorter.

Byte array -> String

This converter interprets data as an ASCII-encoded string. For different data lengths, the same rules apply as for Byte array -> Byte array.

Additional converter parameters:

Length

Maximum length of the string. Shorter data is filled with MemSetVal. With “-1” the length of the data area is used.

MemSetVal

This byte value is used to fill the data area if the data to be written is shorter.

TrimCharacters

Listing of all single characters that can represent the end of the string. The characters can be specified either as ASCII characters or in hex notation ([0x31] is the character ‘1’, for example).

StringEncodingName

Name of the character encoding, e.g. ASCII, UTF8, Unicode (UTF-16 little endian) or BigEndianUnicode. Empty means: the default encoding of the GInOut manager is used (set in the HwExplorer on the HW page under MAK Inputconverter default string encoding; default ASCII). For an encoding with more than one byte per character, CheckForZeroTermination should be switched off.

CheckForZeroTermination

[True|False] With True the string read ends at the first byte 0x00. Default: True.

Example:

Datenbereich mit Länge 5, Inhalt in HEX: 3132330000
TrimCharacters [0x00]
Der Konverter liefert hier "123"

Byte array -> S7 String

This converter interprets data as S7 STRING:

S7 String scheme

Structure S7 STRING

Converter parameters:

Capacity

Maximum length of the string [0..255]. When the data area is assigned, it is automatically set to its length minus 2, which is correct for most use cases.

MemSetVal

This byte value is used to fill the data area if the data to be written is shorter.

TrimCharacters

Listing of all single characters that can represent the end of the string. The characters can be specified either as ASCII characters or in hex notation ([0x31] is the character ‘1’, for example).

StringEncodingName

Name of the character encoding, e.g. ASCII, UTF8, Unicode (UTF-16 little endian) or BigEndianUnicode. Empty means: the default encoding of the GInOut manager is used (set in the HwExplorer on the HW page under MAK Inputconverter default string encoding; default ASCII). For an encoding with more than one byte per character, CheckForZeroTermination should be switched off.

CheckForZeroTermination

[True|False] With True the string read ends at the first byte 0x00. Default: True.

Example:

Data segment with length=5, content in HEX: 3132330000
TrimCharacters [0x00]
Converter returns "123"

Byte array -> S7 WString

This converter interprets data as S7 WSTRING, the Siemens string type with two bytes per character (UTF-16 big endian). This allows characters outside the Western European character set, e.g. Asian characters, to be exchanged with the PLC without loss.

Data layout:

Word 1: capacity (maximum number of characters, WSTRING[n] in the PLC)
Word 2: current length in characters
then:   characters, 2 bytes each (big endian)

The data length of the GInOut variable is therefore capacity * 2 + 4 bytes. For a WSTRING[50] in the PLC this is 104 bytes.

Converter parameters:

Capacity

Maximum number of characters [0..65535]. When the data area is assigned, it is calculated automatically from its length ((data length - 4) / 2). If Auto adjust length from converter is active, the data length of the variable is set from the capacity instead.

MemSetVal

This byte value is used to fill the unused characters if the string to be written is shorter than the capacity.

TrimCharacters

List of all single characters that are removed from the end of the string when reading.

CheckForZeroTermination

[True|False] With True the string read ends at the first character 0x0000. Default: False.

When writing, a string longer than the capacity is truncated to the capacity. When reading, the length from the length word applies, limited to the characters present in the data area; the capacity word is not evaluated when reading. With the data length capacity * 2 + 4 this corresponds to the capacity.

Example:

Variable with data length 24 (capacity 10)
Value "日本語" (3 characters)
Data in HEX: 000A 0003 65E5 672C 8A9E 0000 0000 0000 0000 0000 0000 0000

Hint

For a single-byte S7 STRING see Byte array -> S7 String. The existing STRING communication is not affected by this converter.

Byte array Int -> String

This converter reads an integer from the data area and represents it as a string, either via a mapping table (Int-to-String map) or via a number format. When writing, the string is converted back into the integer the same way.

Converter parameters:

IntLength

Length of the integer in the data area in bytes [1|2|4].

Mode

IntToStringMap: the number is translated into a text via the table given under IntToStringMapName (default).

FormatString: the number is represented as text with the number format given under FormatString.

IntToStringMapName

Name of an Int-to-String map from the object tree of the HwExplorer (node IntToStringMaps). Empty: the number is returned unchanged as text.

FormatString

Number format according to .NET, e.g. D8 (decimal, 8 digits with leading zeros), X8 (hexadecimal, 8 digits) or 00000000. Empty: the number is returned unchanged as text. If the format starts with X, a hexadecimal input is expected when writing.

Example:

Data segment with length=2, content in HEX: 3412 (TwistBytes=True, value 13330)
Mode=FormatString, FormatString=X8  ->  "00003412"
Mode=FormatString, FormatString=D8  ->  "00013330"
Mode=FormatString, FormatString empty ->  "13330"

Configurations from earlier versions do not contain the parameter Mode yet and keep working in mode IntToStringMap.

Byte array -> Int16 or Int32

These converters have been replaced by the GenericInputConverter and are only required for backward compatibility.

Byte array bits -> Bool, Int16 or Int32

With the “Byte array bits” converters, individual bits are read from the data area and a boolean or numerical value is formed. This allows individual bits within a byte to be masked out and the sequence of the bits to be influenced.

Additional converter parameters:

MapWeightToBitIdx

Comma-separated list of bit indices from the data area that are used for the conversion. The order also forms the weighting.

Example:

Data segment with length=1, value t 0x17. (Binary1: 0b00010111)
MapWeightToBitIdx: 4, 3, 2
Result after conversion: 5 (Bit 4: 1, Bit 3: 0, Bit 2: 1).

Byte array string -> Int16, Int32 or Double

The byte array string converters are mainly needed for communication with Waycom Prodel, since this controller provides its data exclusively in ASCII-coded form. A bit in Prodel, for example, always has the value “0x30” (LOW) or “0x31” (HIGH).

The additional converter parameters are analogous to Byte array -> String.

Byte array hex string -> UInt32

GenericArrayConverter

With this converter, arrays of simple value types can be read in. The same data types are supported as with GenericInputConverter. The data length results from the maximum number of elements (`MaxCount`), the selected data type, and an additional 4 bytes if `FixedCount=False`.

In addition to TwistBytes this converter supports the following parameters:

MaxCount

Specifies the maximum number of elements in the array.

FixedCount

[True]: All elements are always valid. [False]: The first 4 bytes in the data area indicate how many elements in the array are valid. For the conversion of these bytes, the GenericInputConverter for UInt32 is used.

GenericBitArrayConverter

This converter assigns one of two values to each bit in the data segment, depending on whether the bit is 0 or 1. This method saves space in the data segment if several Boolean information are to be transmitted simultaneously.

MaxCount

Specifies the maximum number of elements in the array.

FixedCount

[True]: All elements are always valid. [False]: The first 4 bytes in the data area indicate how many elements in the array are valid. For the conversion of these bytes, the GenericInputConverter for UInt32 is used.

LowVal

Value in the array if the associated bit is 0.

HighVal

Value in the array if the associated bit is 1.

StringArrayConverter

Analogous to GenericArrayConverter this converter can be used to convert arrays of strings. The strings have a maximum length. For the strings themselves Byte array -> String is used.

MaxCount

Specifies the maximum number of elements in the array.

FixedCount

[True]: All elements are always valid. [False]: The first 4 bytes in the data area indicate how many elements in the array are valid. For the conversion of these bytes, the GenericInputConverter for UInt32 is used.

Length

The maximum length of a string in the array.

MemSetVal

See also Byte array -> String

TrimCharacters

See also Byte array -> String