TReK Python  5.3.3
Telemetry/Command API
ex_packet_extract.py
1"""
2This example extracts the packet described by the define_packet example. If
3that example hasn't run, you will need to do that now. The file that is
4created must be located where this program can find it.
5The table below shows what is defined in the packet:
6Name Data Type Size Notes
7Version unsigned integer 4 Should always be zero
8PktId unsigned integer 12 For this packet, the value is 54
9PktLen unsigned integer 16 Length of packet after this point
10PktCnt unsigned integer 8 Packet counter
11Time system time 32 A.k.a. DT_UNIX_TIME
12IntParam signed integer 16 First data parameter
13UintParam unsigned integer 32 Three consecutive values
14FloatParam double 64 Little-endian floating point
15StrParam string 800 Varible length string
16Checksum unsigned integer 32 CRC-32 checksum
17"""
18import trek
19
20
21def main() -> None:
22 """
23 Main Routine
24 """
25
26 try:
27
28 # NOTE: There is only minimal error checking shown in the code below in
29 # order to simplify the example. In general, only the first time a
30 # method is used will the error handling be in this example. You should
31 # always check the returned value when one is available to ensure proper
32 # operation of the code. Error handling for this example is to exit on
33 # error.
34 pkt = trek.Packet()
35
36 # Read in the packet file.
37 pkt.load_file("my_file.xml")
38
39 # After loading a packet, you must create the 'map' that allows parameter lookup.
40 pkt.create_global_packet_map()
41
42 # Normally you would extract a packet that came in from the network or
43 # some other interface. Since this is a simple example a buffer is shown
44 # below already popluated with data that is valid (including the
45 # checksum).
46 buf = bytearray(100)
47 buf_size: int = 0
48
49 buf[0] = 0x00 # Version = 0
50 buf[1] = 0x36 # PktId = 54
51 buf[2] = 0x00 # PktLen = 36 (40 - 4 byte offset)
52 buf[3] = 0x24
53 buf[4] = 0xFE # PktCnt = 254
54 buf[5] = 0x53 # Time = 2014-02-28 21:57:47
55 buf[6] = 0x11
56 buf[7] = 0x06
57 buf[8] = 0x5B
58 buf[9] = 0xFF # IntParam = -1
59 buf[10] = 0xFF
60 buf[11] = 0x00 # UintParam(1) = 255
61 buf[12] = 0x00
62 buf[13] = 0x00
63 buf[14] = 0xFF
64 buf[15] = 0x00 # UintParam(2) = 256
65 buf[16] = 0x00
66 buf[17] = 0x01
67 buf[18] = 0x00
68 buf[19] = 0x00 # UintParam(3) = 65,536
69 buf[20] = 0x01
70 buf[21] = 0x00
71 buf[22] = 0x00
72 buf[23] = 0x00 # FloatParam = -10000.125
73 buf[24] = 0x00
74 buf[25] = 0x00
75 buf[26] = 0x00
76 buf[27] = 0x10
77 buf[28] = 0x88
78 buf[29] = 0xC3
79 buf[30] = 0xC0
80 buf[31] = 0x48 # StrParam = "Help!"
81 buf[32] = 0x65
82 buf[33] = 0x6C
83 buf[34] = 0x70
84 buf[35] = 0x21
85 buf[36] = 0x00
86 buf[37] = 0x20 # Checksum (decimal=1,534,784,980)
87 buf[38] = 0x96
88 buf[39] = 0x54
89 buf[40] = 0x1C
90 buf_size: int = 41
91
92 _ = pkt.extract(buf, buf_size) # returns last bit, unused.
93
94 # Get a list of the parameters in the packet sorted by their start
95 # location. Get the values of all samples of each parameter as a string.
96 all_params = pkt.get_sorted_parameter_list()
97 for param_name in all_params:
98 param = pkt.find_parameter(param_name)
99
100 # See how many samples of parameter has and get each.
101 for sample in range(1, param.get_number_of_samples() + 1):
102 print(
103 f"{param_name} (sample {sample}): {param.get_value_as_string(sample)}"
104 )
105
106 except trek.TrekError as err:
107 # Locate friendly message why it failed.
108 print(err.find_error_code())
109 print(err)
110
111
112if __name__ == "__main__":
113 main()
This class describes a packet composed of one or more parameters.
Definition: trek.py:5009
The TReK C++ was designed to return error codes.
Definition: trek.py:12835