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.