Primitive Types#

Primitive data types and bit string representations used in the DNP3 object library.

This module maps DNP3’s primitive object types onto their Python equivalents (e.g., UINT8, INT32, FLT32). It also implements the two bit-string codecs used elsewhere in this package, BSTRn and DBSTRn, according to DNP3 Specification, Section 11.3.3.

Two flavors of bit string are covered here:

  • BSTRn: a plain packed bit string – one bit per boolean flag.

  • DBSTRn: a packed string of 2-bit values, letting each entry hold one of four states instead of a simple on/off (useful, for instance, for binary inputs that support an intermediate state).

Note

These types are used internally by the unpacking and packing mechanisms when parsing or constructing DNP3 Application Layer objects.

icspacket.proto.dnp3.objects.primitive.UINT8 = <int8>#

8-bit unsigned integer.

icspacket.proto.dnp3.objects.primitive.UINT16 = <int16>#

16-bit unsigned integer.

icspacket.proto.dnp3.objects.primitive.UINT24 = <uint24>#

24-bit unsigned integer.

icspacket.proto.dnp3.objects.primitive.UINT32 = <int32>#

32-bit unsigned integer.

icspacket.proto.dnp3.objects.primitive.INT16 = <int16>#

16-bit signed integer.

icspacket.proto.dnp3.objects.primitive.INT32 = <int32>#

32-bit signed integer.

icspacket.proto.dnp3.objects.primitive.VSTR#

Variable-length string.

icspacket.proto.dnp3.objects.primitive.OSTR#

Octet string (arbitrary-length byte sequence).

icspacket.proto.dnp3.objects.primitive.FLT32 = <float32>#

32-bit IEEE-754 floating point.

icspacket.proto.dnp3.objects.primitive.FLT64 = <float64>#

64-bit IEEE-754 floating point.

class icspacket.proto.dnp3.objects.primitive.BCD(count: int)[source]#

Binary-coded decimal (BCD) codec, named BCDn for an n-digit value.

For instance, a BCD8 field holds 8 decimal digits, according to DNP3 Specification, Section 11.3.6. Each digit occupies a 4-bit nibble, and this codec packs two digits into every byte in little-endian order.

decode(parsed: bytes, context: _ContextLike) str[source]#

Encode a string of decimal digits into BCD bytes.

Parameters:
  • obj (str) – The decimal string to encode. A ‘-’ character may be used to represent a nibble value of 10.

  • context (Any) – Transformation context (unused in this implementation).

Returns:

Encoded BCD bytes.

Return type:

bytes

encode(obj: str, context: _ContextLike) bytes[source]#

Encode data using the wrapped _StructLike object.

Parameters:
  • obj – The original data to be encoded.

  • context – The current context.

Returns:

The encoded data.

class icspacket.proto.dnp3.objects.primitive.BSTRn[source]#

Codec for a DNP3 packed bit string (BSTRn), one bit per flag.

Bit 0 – the field’s least-significant bit – holds the first flag, with later flags packed upward from there; the field is always padded out to whole octets, so for example 10 flags occupy 2 octets on the wire. This codec exposes the decoded value as a little-endian bitarray.bitarray.

count(obj: bitarray) int[source]#

Return the number of bits in the given bit string.

Parameters:

obj (bitarray.bitarray) – The bit string.

Returns:

The number of bits.

Return type:

int

class icspacket.proto.dnp3.objects.primitive.DBSTRn[source]#

Codec for a DNP3 double-bit string (DBSTRn), 2 bits per value.

Every value in the string occupies 2 bits, giving four possible readings:

  • 00 → intermediate or indeterminate state

  • 01 → determined OFF

  • 10 → determined ON

  • 11 → reserved

Four such values fit in each octet, and this codec keeps the unpacked form as a plain list of integers.

count(obj: list[int]) int[source]#

Return the number of double-bit values in the list.

Parameters:

obj (list[int]) – List of double-bit values.

Returns:

The number of double-bit values.

Return type:

int