Morelia.packet.legacy package

Submodules

Morelia.packet.legacy.Binary module

class Morelia.packet.legacy.Binary.PacketBinary(pkt, commands=None)

Bases: Packet

Container class that stores a standard binary command packet for a POD device. The format is STX (1 byte) + command number (4 bytes) + length of binary (4 bytes) + checksum (2 bytes) + ETX (1 bytes) + binary (LENGTH bytes) + checksum (2 bytes) + ETX (1 bytes)

Attributes:

binaryLength (bytes): Number of bytes of binary data from the packet. binaryData (bytes): Variable length binary datafrom the packet.

BinaryLength()

Translate the binary ASCII encoding of the binary data length into a readable integer

Return type:

int

Returns:

int: Integer of the binary data length.

static CheckIfPacketIsValid(msg)

Raises an Exception if the packet is incorrectly formatted.

Args:

msg (bytes): Bytes string containing a POD packet. Should begin with STX and end with ETX.

Raises:

Exception: Packet is too small to be a standard packet. Exception: A standard binary packet must have an ETX before the binary bytes.

static GetBinaryData(pkt)

Gets the binary data from a POD packet.

Return type:

bytes

Args:

pkt (bytes): Bytes string containing a POD packet.

Returns:

bytes: Bytes string containg binary data.

static GetBinaryLength(pkt)

Gets the length, or number of bytes, of the binary data in a POD packet.

Return type:

bytes

Args:

pkt (bytes): Bytes string containing a POD packet.

Returns:

bytes: Bytes string of the length of the binary data.

static GetMinimumLength()

Gets the number of bytes in the smallest possible packet.

Return type:

int

Returns:

int: integer representing the minimum length of a binary POD command packet. Format is STX (1 byte) + command number (4 bytes) + length of binary (4 bytes) + checksum (2 bytes) + ETX (1 bytes) + binary (LENGTH bytes) + checksum (2 bytes) + ETX (1 bytes)

TranslateAll()

Builds a dictionary containing all parts of the POD packet in readable values.

Return type:

dict[str, Any]

Returns:

dict[str,Any]: Dictionary with the command number, binary packet length, and binary data.

UnpackAll()

Builds a dictionary containing all parts of the POD packet in bytes.

Return type:

dict[str, bytes]

Returns:

dict[str,bytes]: Dictionary with the command number, binary packet length, and binary data.

Morelia.packet.legacy.Packet module

class Morelia.packet.legacy.Packet.Packet(pkt, commands=None)

Bases: object

Container class that stores a command packet for a POD device. The format is STX (1 byte) + command number (4 bytes) + data (? bytes) + ETX (1 byte). This class also has a collection of methods for creating and interpreting POD packets.

Attributes:

_commands (POD_Commands | None): Available commands for a POD device. rawPacket (bytes): Bytes string containing a POD packet. Should begin with STX and end with ETX. commandNumber (bytes | None): Command number from the Pod packet.

static ASCIIbytesToInt_Split(msg, keepTopBits, cutBottomBits)

Converts a specific bit range in an ASCII-encoded bytes object to an integer.

Return type:

int

Args:

msg (bytes): Bytes message holding binary information to be converted into an integer. keepTopBits (int): Integer position of the msb of desired bit range. cutBottomBits (int): Integer number of lsb to remove.

Returns:

int: Integer result from the ASCII-encoded bytes message in a given bit range.

static AsciiBytesToInt(msg_b, signed=False)

Converts a ASCII-encoded bytes message into an integer. It does this using a base-16 conversion. If the message is signed and the msb is β€˜1’, the integer will be converted to it’s negative 2’s complement.

Return type:

int

Args:

msg_b (bytes): Bytes message to be converted to an integer. The bytes must be base-16 or the conversion will fail. signed (bool, optional): True if the message is signed, false if unsigned. Defaults to False.

Returns:

int: Integer result from the ASCII-encoded byte conversion.

static BinaryBytesToInt(msg, byteorder='big', signed=False)

Converts binary-encoded bytes into an integer.

Return type:

int

Args:

msg (bytes): Bytes message holding binary information to be converted into an integer. byteorder (str, optional): Ordering of bytes. β€˜big’ for big endian and β€˜little’ for little endian. Defaults to β€˜big’. signed (bool, optional): Boolean flag to mark if the msg is signed (True) or unsigned (False). Defaults to False.

Returns:

int: Integer result from the binary-encoded bytes message.

static BinaryBytesToInt_Split(msg, keepTopBits, cutBottomBits, byteorder='big', signed=False)

Converts a specific bit range in a binary-encoded bytes object to an integer.

Return type:

int

Args:

msg (bytes): Bytes message holding binary information to be converted into an integer. keepTopBits (int): Integer position of the msb of desired bit range. cutBottomBits (int): Integer number of lsb to remove. byteorder (str, optional): Ordering of bytes. β€˜big’ for big endian and β€˜little’ for little endian. Defaults to β€˜big’. signed (bool, optional): Boolean flag to mark if the msg is signed (True) or unsigned (False). Defaults to False.

Returns:

int: Integer result from the binary-encoded bytes message in a given bit range.

static CheckIfPacketIsValid(msg)

Raises an Exception if the packet is incorrectly formatted.

Args:

msg (bytes): Bytes string containing a POD packet. Should begin with STX and end with ETX.

Raises:

Exception: Packet must begin with STX. Exception: Packet must end in ETX

CommandNumber()

Translate the binary ASCII encoding into a readable integer

Return type:

int

Returns:

int: Integer of the command number.

static ETX()

Get end-of-transmission (ETX) character in bytes. ETX marks the end byte of a POD Packet.

Return type:

bytes

Returns:

bytes: Bytes for ETX(0x03).

static GetCommandNumber(pkt)

Gets the command number bytes from a POD packet.

Return type:

bytes | None

Args:

pkt (bytes): Bytes string containing a POD packet. Should begin with STX and end with ETX.

Returns:

bytes|None: Bytes string of the command number, if available.

static GetMinimumLength()

Gets the number of bytes in the smallest possible packet; STX (1 byte) + something + ETX (1 byte).

Return type:

int

Returns:

int: integer representing the minimum length of a generic bytes string.

HasCommandNumber()

Checks if the packet has a command number.

Return type:

bool

Returns:

bool: True if the packet has a command number, False otherwise.

HasCommands()

Checks if the Packet instance has commands set.

Return type:

bool

Returns:

bool: True if the commands have been set, false otherwise.

static IntToAsciiBytes(value, numChars)

Converts an integer value into ASCII-encoded bytes.

First, it converts the integer value into a usable uppercase hexadecimal string. Then it converts the ASCII code for each character into bytes. Lastly, it ensures that the final message is the desired length.

Example: if value=2 and numBytes=4, the returned ASCII will show b’0002’, which is β€˜0x30 0x30 0x30 0x32’ in bytes. Uses the 2’s complement if the val is negative.

Return type:

bytes

Args:

value (int): Integer value to be converted into ASCII-encoded bytes. numChars (int): Number characters to be the length of the ASCII-encoded message.

Returns:

bytes: Bytes that are ASCII-encoded conversions of the value parameter.

static STX()

Get start-of-transmission (STX) character in bytes. STX marks the starting byte of a POD Packet.

Return type:

bytes

Returns:

bytes: Bytes for STX (0x02).

TranslateAll()

Builds a dictionary containing all parts of the POD packet in readable values.

Return type:

dict[str, Any]

Raises:

Exception: Nothing to translate.

Returns:

dict[str,Any]: Dictionary with the command number.

static TwosComplement(val, nbits)

Gets the 2’s complement of the argument value (negative int).

Return type:

int

Args:

val (int): Negative value to be complemented. nbits (int): Number of bits in the value.

Returns:

int: Integer of the 2’s complement for the val.

UnpackAll()

Builds a dictionary containing all parts of the POD packet in bytes.

Return type:

dict[str, bytes]

Raises:

Exception: Nothing to unpack.

Returns:

dict[str,bytes]: Dictionary with the command number.

Module contents