uds.utilities.conversions

Module with various conversion functions.

Classes

TimeSync

Synchronization between wall clock (time.time()) and performance counter (time.perf_counter()) values.

Functions

bytes_to_hex(bytes_list)

Convert a list of bytes to hex string.

bytes_to_int(bytes_list[, endianness])

Convert a list of bytes to integer value.

int_to_bytes(int_value[, size, endianness])

Convert integer value to a list of bytes.

obd_dtc_to_int(obd_dtc)

Convert text with DTC in OBD format into integer value (DTC in UDS format).

int_to_obd_dtc(dtc)

Encode integer value (DTC in UDS format) into text with DTC in OBD format.

get_signed_value_decoding_formula(bit_length)

Get formula for decoding signed integer value.

get_signed_value_encoding_formula(bit_length)

Get formula for encoding signed integer value.

Module Contents

uds.utilities.conversions.bytes_to_hex(bytes_list)[source]

Convert a list of bytes to hex string.

Parameters:

bytes_list (uds.utilities.common_types.RawBytesAlias) – List of bytes to convert.

Returns:

String with provided list of bytes presented as hexadecimal values.

Return type:

str

uds.utilities.conversions.bytes_to_int(bytes_list, endianness=Endianness.BIG_ENDIAN)[source]

Convert a list of bytes to integer value.

Parameters:
Returns:

The integer value represented by provided list of bytes.

Return type:

int

uds.utilities.conversions.int_to_bytes(int_value, size=None, endianness=Endianness.BIG_ENDIAN)[source]

Convert integer value to a list of bytes.

Parameters:
  • int_value (int) – Integer value to convert.

  • size (Optional[int]) – Number of bytes in the output. Use None to use the smallest possible number of bytes.

  • endianness (uds.utilities.enums.Endianness) – Order of bytes to use.

Raises:
  • TypeError – At least one provided value has invalid type.

  • ValueError – At least one provided value is out of range.

  • InconsistencyError – Provided value of size is too small to contain entire int_value.

Returns:

The value of bytes list that represents the provided integer value.

Return type:

bytes

uds.utilities.conversions.obd_dtc_to_int(obd_dtc)[source]

Convert text with DTC in OBD format into integer value (DTC in UDS format).

Parameters:

obd_dtc (str) – Text with DTC in OBD format.

Raises:
  • TypeError – Provided value is not str type.

  • ValueError – Provided value is not DTC in OBD format.

Returns:

Integer value representation of this DTC in UDS format.

Return type:

int

uds.utilities.conversions.int_to_obd_dtc(dtc)[source]

Encode integer value (DTC in UDS format) into text with DTC in OBD format.

Parameters:

dtc (int) – Integer with DTC in UDS format.

Raises:
  • TypeError – Provided value is not int type.

  • ValueError – Provided value is not DTC in OBD format.

Returns:

Text value representation of this DTC in OBD format.

Return type:

str

uds.utilities.conversions.get_signed_value_decoding_formula(bit_length)[source]

Get formula for decoding signed integer value.

Parameters:

bit_length (int) – Number of bits used for signed integer value.

Raises:
Returns:

Formula for decoding singed integer value from unsigned integer value.

Return type:

Callable[[int], int]

uds.utilities.conversions.get_signed_value_encoding_formula(bit_length)[source]

Get formula for encoding signed integer value.

Parameters:

bit_length (int) – Number of bits used for signed integer value.

Raises:
Returns:

Formula for encoding singed integer value into unsinged integer value.

Return type:

Callable[[int], int]

class uds.utilities.conversions.TimeSync(samples_number=None, sync_expiration=None)[source]

Synchronization between wall clock (time.time()) and performance counter (time.perf_counter()) values.

Get time synchronization object.

Parameters:
  • samples_number (Optional[int]) – Number of samples to use for synchronization.

  • sync_expiration (Optional[Union[int, float]]) – Time in seconds after which synchronization is considered no longer up to date.

Warning

Objects of this class are Singletons.

_instance = None

Instance of this Singleton.

DEFAULT_SAMPLES_NUMBER = 100

Default number of samples collected during synchronization.

DEFAULT_SYNC_EXPIRATION_S = 1

Default expiration time (in seconds) of the offset calculated during last synchronization.

property samples_number: int

Get number of samples to take during synchronization.

Return type:

int

property sync_expiration: float

Get time in seconds after which synchronization value is considered outdated.

Return type:

float

property last_sync_timestamp: float | None

Value of performance counter for the last synchronization point.

Return type:

Optional[float]

property is_sync_outdated: bool

Get flag whether the current sync value is outdated.

Return type:

bool

property offset: float | None

Difference between wall clock and performance counter.

Return type:

Optional[float]

sync()[source]

Perform synchronization.

Return type:

float

time_to_perf_counter(time_value, min_value=None, max_value=None)[source]

Convert wall clock time to performance counter.

Parameters:
  • time_value (float) – Wall clock time value to convert.

  • min_value (Optional[float]) – The lowest possible result (an earlier value of performance counter).

  • max_value (Optional[float]) – The highest possible result (a later value of performance counter).

Returns:

An approximation of performance counter for given wall clock time value.

Return type:

float

perf_counter_to_time(perf_counter_value, min_value=None, max_value=None)[source]

Convert performance counter to wall clock time.

Parameters:
  • perf_counter_value (float) – Performance counter value to convert.

  • min_value (Optional[float]) – The lowest possible result (an earlier value of wall clock time).

  • max_value (Optional[float]) – The highest possible result (a later value of wall clock time).

Returns:

An approximation of wall clock time for given performance counter value.

Return type:

float