Message

class n2k.message.Message(source: int = 15, priority: int = 6, pgn: int = 0, data: bytearray | None = None) None[source]

Bases: object

max_data_len: int = 223
__init__(source: int = 15, priority: int = 6, pgn: int = 0, data: bytearray | None = None) None[source]
source: int
destination: int
priority: int
pgn: int = 0
msg_time: int = 0
data: bytearray
data_len: int
check_destination() None[source]

Verify the destination, as only PGNs where the lower byte is 0 can be sent to specific addresses. :rtype: None :return:

is_valid() bool[source]
Return type:

bool

get_remaining_data_length(index: int) int[source]
Return type:

int

get_available_data_length() int[source]
Return type:

int

add_float(v: float | None) None[source]

Store float values as single precision IEEE floating point

Parameters:
  • v (float | None) – value to be stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_FLOAT_NA

Return type:

None

add_1_byte_udouble(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 1 byte (0 - 254) unsigned integer values. Thus if we choose a precision of 0.1, the maximum that could be stored is 0.1 * 254 = 25.4 If the value is outside of this range, it will be clamped to 0 or 254. 255 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_1_byte_double(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 1 byte (-128 - 126) signed integer values. Thus if we choose a precision of 0.1, the maximum that could be stored is 0.1 * 126 = 12.6 If the value is outside of this range, it will be clamped to -128 or 126. 127 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_2_byte_udouble(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 2 bytes (0 - 65534) unsigned integer values. Thus if we choose a precision of 0.01, the maximum that could be stored is 0.01 * 65534 = 655.34 If the value is outside of this range, it will be clamped to 0 or 65534. 65535 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_2_byte_double(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 2 bytes (-32768 - 32676) signed integer values. Thus if we choose a precision of 0.01, the maximum that could be stored is 0.01 * 32676 = 326.76 If the value is outside of this range, it will be clamped to -32768 or 32676. 32767 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_3_byte_udouble(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 3 bytes (0 - 16777214) unsigned integer values. Thus if we choose a precision of 0.001, the maximum that could be stored is 0.001 * 16777214 = 16777.214 If the value is outside of this range, it will be clamped to 0 or 16777214. 16777215 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_3_byte_double(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 3 bytes (-8388608 - 8388606) signed integer values. Thus if we choose a precision of 0.001, the maximum that could be stored is 0.001 * 8388606 = 8388.606 If the value is outside of this range, it will be clamped to -8388608 or 8388606. 8388607 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_4_byte_udouble(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 4 bytes (0 - 4294967294) unsigned integer values. Thus if we choose a precision of 0.00001, the maximum that could be stored is 0.00001 * 4294967294 = 42949.67294 If the value is outside of this range, it will be clamped to 0 or 4294967294. 4294967295 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_4_byte_double(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 4 bytes (-2147483648 - 2147483646) signed integer values. Thus if we choose a precision of 0.00001, the maximum that could be stored is 0.00001 * 2147483646 = 21474.83646 If the value is outside of this range, it will be clamped to -2147483648 or 2147483646. 2147483647 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_8_byte_double(v: float | None, precision: float) None[source]

Store float values with a fixed amount of decimal places

Limited to 8 bytes (-2^63 to 2^63-2) signed integer values. Thus if we choose a precision of 1e-7, the maximum that could be stored is 0.0000001 * (2^63-2). If the value is outside of this range, it will be clamped to -2^63 or 2^63-2. 2^63-1 is used to mark the field as undefined.

Parameters:
  • v (float | None) – value to be stored

  • precision (float) – factor by which the value is divided before being rounded and stored

  • undef_val – value which marks the field as undefined, defaults to constants.N2K_DOUBLE_NA

Return type:

None

add_byte_uint(v: int | None) None[source]
Return type:

None

add_byte_int(v: int | None) None[source]
Return type:

None

add_2_byte_uint(v: int | None) None[source]
Return type:

None

add_2_byte_int(v: int | None) None[source]
Return type:

None

add_3_byte_uint(v: int | None) None[source]
Return type:

None

add_3_byte_int(v: int | None) None[source]
Return type:

None

add_4_byte_uint(v: int | None) None[source]
Return type:

None

add_uint_64(v: int | None) None[source]
Return type:

None

add_str(v: str | None, length: int) None[source]
Return type:

None

add_var_str(v: str | None) None[source]
Return type:

None

add_ais_str(v: str | None, length: int) None[source]
Return type:

None

S = ~S
get_float(index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_1_byte_udouble(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_1_byte_double(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_2_byte_udouble(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_2_byte_double(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_3_byte_udouble(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_3_byte_double(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_4_byte_udouble(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_4_byte_double(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

get_8_byte_double(precision: float, index: IntRef, default: S = None) float | S[source]
Return type:

float | S

T = ~T
get_byte_uint(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_byte_int(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_2_byte_uint(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_2_byte_int(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_3_byte_uint(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_3_byte_int(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_4_byte_uint(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_4_byte_int(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_uint_64(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_8_byte_int(index: IntRef, default: T = None) int | T[source]
Return type:

int | T

get_str(length: int, index: IntRef, nul_char: bytes = b'@') str | None[source]
Return type:

str | None

get_var_str(index: IntRef) str | None[source]
Return type:

str | None

set_byte_uint(v: int, index: IntRef) bool[source]
Return type:

bool

set_2_byte_uint(v: int, index: IntRef) bool[source]
Return type:

bool

n2k.message.print_buf(port: Stream, length: int, p_data: str, add_lf: bool = False) None[source]
Return type:

None

n2k.message.apply_precision(raw_value: int, precision: float) float[source]

Apply the precision to the raw value.

Due to limitations of floating point numbers (https://docs.python.org/3/tutorial/floatingpoint.html) the exact value of many decimal numbers cannot be represented. When dividing by a float, python seems to return the actual number that the binary representation maps to. This is not a problem, but throws off unit tests that check for equality. Dividing by the inverse of the precision (which should always be an integer) instead, seems to yield the expected result.

Parameters:
  • raw_value (int) – value as parsed from the message

  • precision (float) – precision with which the value was stored

Return type:

float

Returns:

value with applied precision, with the appropriate number of decimal places