Morelia.packet packageο
Subpackagesο
- Morelia.packet.data package
- Morelia.packet.legacy package
- Submodules
- Morelia.packet.legacy.Binary module
- Morelia.packet.legacy.Packet module
Packet
Packet.ASCIIbytesToInt_Split()
Packet.AsciiBytesToInt()
Packet.BinaryBytesToInt()
Packet.BinaryBytesToInt_Split()
Packet.CheckIfPacketIsValid()
Packet.CommandNumber()
Packet.ETX()
Packet.GetCommandNumber()
Packet.GetMinimumLength()
Packet.HasCommandNumber()
Packet.HasCommands()
Packet.IntToAsciiBytes()
Packet.STX()
Packet.TranslateAll()
Packet.TwosComplement()
Packet.UnpackAll()
- Module contents
Submodulesο
Morelia.packet.channel_mode moduleο
This file contains types used for indicating the types of various channels read from aquisition devices. Currently, the only device Morelia supports that takes advantage of modal channels is the 8401HR.
Morelia.packet.control_packet moduleο
- class Morelia.packet.control_packet.ControlPacket(decode_from, raw_packet)ο
Bases:
PodPacket
Class for parsing βStandardβ POD packets from raw bytes. These are associated with controlling or configuring a device, hence the name These packets take the following form
The payload is normally several values that need to parsed into a tuple of integers. Oftentimes, this class is used a βfactoryβ of sorts. For any given device, you might partially apply the constructor with the
decode_from
argument, and then any time you need to make a packet, just call the curried constructor with the raw bytes of the packet you want to parse. For a concrete example of this, see the constructor of thePod8206HR
class.- Parameters:
decode_from (
Union
[CommandSet
,Callable
[[int
,bytes
],tuple
]]) β Instructs the packet how to parse the payload from raw bytes to a Python object. If aCommandSet
object is passed, the payload is decoded according to that. Otherwise, a function must be passed that takes a command number andbytes
object to decode, and returns atuple
. For a simple example of how passing adecode_from
function directly may be used, see the constructor of thePod8206HR
class.raw_packet (
bytes
) β The raw bytes to be parsed as a control packet.
- static decode_payload_from_cmd_set(cmds, cmd_number, payload)ο
Used for parsing packet payloads according to a
CommandSet
object. Do not use this function for payload access (it lacks the memoization that makes the payload property speedy), but for building bespoke decoding functions to pass to this class asdecode_from
. Generally, this function is used to create decoding functions that default to using a command set, but have the capability to handle edge-cases on a per-command basis. For an example of how this is used, see the constructor of thePod8206HR
class.- Parameters:
cmds (
CommandSet
) β The CommandSet to decode from.cmd_number (
int
) β Command number of the packet that the payload corresponds to.payload (
bytes
) β Raw bytes of the payload you wish to decode.
- Return type:
tuple
- Returns:
The parsed payload as a tuple.
- property payload: tupleο
Parses the packet payload in a lazy, memoized fashion.
- Returns:
The parsed payload as a tuple.
Morelia.packet.conversion moduleο
This file implements the following commutative diagram:
In laypersonβs terms, it is full of utility functions for converting between binary-coded decimal, ASCII, and decimal integers.
- class Morelia.packet.conversion.Endianness(*values)ο
Bases:
Enum
This enumeration is used to specify the endianness of a series of bytes passed into a function.
- BIG = 1ο
- LITTLE = 2ο
- Morelia.packet.conversion.ascii_bytes_to_binary_bytes(msg_b, num_bytes, byteorder=Endianness.BIG, signed=False)ο
Converts a ASCII-encoded bytestring to an binary-coded decimal one.
- Parameters:
msg_b (
bytes
) β ASCII-encoded bytestring to convert.num_bytes (
bytes
) β Number of bytes that will be in returnedbytes
object. Must be at least enough to hold expected output.byteorder (
Endianness
) β Endianness of bytes.signed (
bool
) β True ifmsg_b
is signed, false if unsigned. Defaults to False.
- Return type:
bytes
- Returns:
msg_b
as binary-coded decimal bytes.
- Morelia.packet.conversion.ascii_bytes_to_int(msg_b, signed=False)ο
msg_b
contain a series of ascii-encoded hexadecimal digits that encode an integer. If signed=True, then this integer is interpreted as a negative twoβs complement number, and the negative number that the bits of the integer encoded in the hexadecimal digits represent is returned. Ifsigned=True
but the integer encoded inmsg_b
is not negative, then the βunderlyingβ twoβs complement number is ignored and the integer encoded in the hex is returned.- Parameters:
msg_b (
bytes
) β Bytes message to be converted to an integer. The bytes must be ascii-encoded or the conversion will fail.signed (
bool
) β True if the message is signed, false if unsigned. Defaults to False.
- Return type:
int
- Returns:
Integer result from the ASCII-encoded byte conversion.
- Morelia.packet.conversion.ascii_bytes_to_int_split(msg, msb_index, lsb_index)ο
Converts a specific bit range in an ASCII-encoded bytes object to an integer.
- Parameters:
msg (
bytes
) β Bytes message holding binary information to be converted into an integer.msb_index (
int
) β Integer position of the msb of desired bit range.lsb_index (
int
) β Integer number of lsb to remove.
- Return type:
int
- Returns:
Integer result from the ASCII-encoded bytes message in a given bit range.
- Morelia.packet.conversion.binary_bytes_to_ascii_bytes(msg, num_chars, byteorder=Endianness.BIG, signed=False)ο
Converts a binary-coded decimal bytestring to an ASCII-encoded one.
- Parameters:
msg (
bytes
) β Binary-coded decimal bytestring to convert.num_chars (
int
) β Number characters to be the length of the ASCII-encoded message.byteorder (
Endianness
) β Endianness of bytes.signed (
bool
) β True ifmsg
is signed, false if unsigned. Defaults to False.
- Return type:
bytes
- Returns:
msg
as ASCII-encoded bytes.
- Morelia.packet.conversion.binary_bytes_to_int(msg, byteorder=Endianness.BIG, signed=False)ο
Converts binary-encoded bytes into an integer.
- Parameters:
msg (
bytes
) β Bytes message holding binary information to be converted into an integer.byteorder (
Endianness
) β Endianness of bytes.signed (
bool
) β True if the message is signed, false if unsigned. Defaults to False.
- Return type:
int
- Returns:
Integer result from the binary-encoded bytes message.
- Morelia.packet.conversion.binary_bytes_to_int_split(msg, msb_index, lsb_index, byteorder=Endianness.BIG, signed=False)ο
Converts a specific bit range in a binary-encoded bytes object to an integer.
- Parameters:
msg (
bytes
) β Bytes message holding binary information to be converted into an integer.msb_index (
int
) β Integer position of the msb of desired bit range.lsb_index (
int
) β Integer number of lsb to remove.byteorder (
Endianness
) β Endianness of bytes.signed (
bool
) β True if the message is signed, false if unsigned. Defaults to False.
- Return type:
int
- Returns:
Integer result from the binary-encoded bytes message in a given bit range.
- Morelia.packet.conversion.int_to_ascii_bytes(value, num_chars)ο
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.
- Parameters:
value (
int
) β Integer value to be converted into ASCII-encoded bytes.num_chars (
int
) β Number characters to be the length of the ASCII-encoded message.
- Return type:
bytes
- Returns:
Bytes that are ASCII-encoded conversions of the value parameter.
- Morelia.packet.conversion.int_to_binary_bytes(msg, num_bytes, byteorder=Endianness.BIG)ο
Converts an integer to a bytes object.
- Parameters:
msg (
int
) β Integer to be convertednum_bytes (
int
) β Number of bytes that will be in returnedbytes
object. Must be at least enough to holdmsg
.byteorder (
Endianness
) β Desired endianness of returned bytes.
- Return type:
bytes
- Returns:
Bytes object representing
msg
in binary-coded decimal.
- Morelia.packet.conversion.neg_int_to_twos_complement(val, nbits)ο
Gets the twoβs complement of the argument value (negative int).
- Parameters:
val (
int
) β Negative integer to get the twoβs complament representation of.nbits (
int
) β Number of bits inval
. This is necessary as the built-inint
type does not track this
- Return type:
int
- Returns:
The twoβs complament representation of
val
. This may seem wrong, but if you look at the returned number in binary, it should matchval
but as twoβs complament.
- Morelia.packet.conversion.twos_complement_to_neg_int(val, nbits)ο
Interpret the binary representation of
val
as a twoβs complament number and return the value as a Python integer.- Parameters:
val (
int
) β Integer to interpret as twoβs complament for conversion.nbits (
int
) β Number of bits inval
. This is necessary as the built-inint
type does not track this
- Return type:
int
- Returns:
The value of
val
interpreted as a twoβs complament number as a negative Python integer.
Morelia.packet.pod_packet moduleο
- class Morelia.packet.pod_packet.PodPacket(raw_packet, min_length=6)ο
Bases:
object
The parent class of all POD protocol packets. Contains basic attributes common to all.
-
ETX:
bytes
= b'\x03'ο
-
STX:
bytes
= b'\x02'ο
- property command_number: intο
- Returns:
Packet command number. Decoded lazily and memoized for speed.
- property raw_packet: bytesο
- Returns:
Raw bytes of packet.
-
ETX: