TReK Python
5.3.3
Telemetry/Command API
|
For this tutorial, we will walk through constructing a packet using the Python package classes. Let's start with our packet definition.
Name | Data Type | Size | Notes |
---|---|---|---|
Version | unsigned integer | 4 | Should always be zero |
PktId | unsigned integer | 12 | For this packet, the value is 54 |
PktLen | unsigned integer | 16 | Length of packet after this point |
PktCnt | unsigned integer | 8 | Packet counter |
Time | system time | 32 | A.k.a. DT_UNIX_TIME |
IntParam | signed integer | 16 | First data parameter |
UintParam | unsigned integer | 32 | Three consecutive values |
FloatParam | double | 64 | Little-endian floating point |
StrParam | string | 800 | Varible length string |
Checksum | unsigned integer | 32 | CRC-32 checksum |
To get started, the basic process to build a packet is bottom up:
From our list above, let's create the first parameter, Version. Version will be the first 4-bits.
In our Python, we import the classes we will need (usually at the top of a pyhon file).
Then, create a Parameter and configure the data.
We add our parameter to a collection. Once added to the collection, a copy is made in the collection and we can re-use our instance of the parameter class for the next parameter because the collection class will make it's own copy.
Putting it together, below, there are two parameters added to our collection while re-using our Parameter instance after it is added to the collection.
We have created two parameters totalling 16 bits. The first 4 bits are the version and the next 12 bits are the packet ID.
This same process is used to configure and add each parameter into our collection for a desired zone. Once we step through all the parameters for our zone, we can add the completed collection to our packet's zone.
This could be add_header, add_trailer, or add_data zones.
The example below provides the completed process for the whole packet and saves the packet in XML form to a file. This saved XML Packet constitutes a "packet definition" used for sending / recieving data.
In closing, we have provided three examples to help with defining, building and extracting packets.