TReK Python  5.3.2
Telemetry/Command API
ex_packet_build.py
1"""
2Example TReK Building a Packet
3
4This example builds the packet described by the define_packet example. If
5that example hasn't run, you will need to do that now. The file that is
6created must be located where this program can find it.
7The table below shows what is defined in the packet:
8Name Data Type Size Notes
9Version unsigned integer 4 Should always be zero
10PktId unsigned integer 12 For this packet, the value is 54
11PktLen unsigned integer 16 Length of packet after this point
12PktCnt unsigned integer 8 Packet counter
13Time system time 32 A.k.a. DT_UNIX_TIME
14IntParam signed integer 16 First data parameter
15UintParam unsigned integer 32 Three consecutive values
16FloatParam double 64 Little-endian floating point
17StrParam string 800 Varible length string
18Checksum unsigned integer 32 CRC-32 checksum
19"""
20
21import trek
22
23
24def main() -> None:
25 """
26 Main Routine
27 """
28
29 try:
30 # NOTE: There is only minimal error checking shown in the code below in
31 # order to simplify the example. In general, only the first time a
32 # method is used will the error handling be in this example. You should
33 # always check the returned value when one is available to ensure proper
34 # operation of the code. Error handling for this example is to exit on
35 # error.
36 pkt = trek.Packet()
37 param_value: float = 0.0
38
39 # Read in the packet file.
40 pkt.load_file("my_file.xml")
41
42 # After loading a packet, you must create the 'map' that allows parameter lookup.
43 pkt.create_global_packet_map()
44
45 # Since the define_packet example doesn't set all of the parameter
46 # values, they must be set here or the build will fail. You can use
47 # GetParameterList or GetSortedParameterList to get a list of the
48 # parameter names if needed. Since the names are already known (check
49 # the top of this file) we can set the values without the lookup.
50 #
51 # HINT: You'll only need to set the values in the "data zone". The
52 # values in the header and trailer were either set in the
53 # define_packet example with your code or were set automatically to
54 # defaults when setting packet attributes.
55 #
56 # Must find the parameter before we can set the value. The pointer that is
57 # returned can be saved so you don't have to perform the lookup each time.
58 param = pkt.find_parameter("IntParam")
59
60 # Now set the value. This is a 16 bit signed integer. TReK will check
61 # value you pass in to make sure that it is allowed for the parameter.
62 # Since there wasn't a high or low range set here, TReK will check that
63 # the value passed in is between -32,768 and 32,767 inclusive. We're
64 # using a floating point value so TReK will also check if the value is a
65 # fraction. The value will still be set, but you'll get a return code
66 # indicating a 'possible loss of data'.
67 param_value = -2
68 param.set_value(param_value)
69
70 # SetValue calls can fail because of data type mismatches (trying to
71 # set a binary value with a float) or many other things. You can look
72 # up the return code in the online documentation to get an idea on
73 # what to check.
74
75 # Now set the value for the UintParam which has 3 samples. The set value
76 # call takes an optional sample number. We don't have to use it for the
77 # first sample.
78 param = pkt.find_parameter("UintParam")
79 param.set_value(1024, 1)
80 param.set_value(2048, 2)
81 param.set_value(4096, 3)
82
83 # The floating point parameter needs a value too.
84 param = pkt.find_parameter("FloatParam")
85 param_value = 1.2345
86 param.set_value(param_value)
87
88 # The StrParam is variable length. Only the bytes needed for the value
89 # (plus the NULL terminating character) will appear in the packet.
90 param = pkt.find_parameter("StrParam")
91 param.set_value("Howdy Y'all")
92
93 # Now that all of the data is defined, we can build the packet. You'll
94 # need a buffer large enough to hold the maximum length packet. The
95 # packet attributes will automatically be set by the Packet class. Each
96 # identifier will be set, the sequence count will be set and incremented,
97 # the time will be set to the current system time, the length will be
98 # calculated and set, and finally the checksum (CRC-32) will be
99 # calculated and set.
100
101 buf = bytearray(1000)
102 _ = pkt.build(buf) # returned last_bit_used not used in example.
103
104 # One common reason for a build to fail is that one or more parameters
105 # do not have a value. Another reason is that the build buffer isn't
106 # large enough. You should always set the buf_size prior to each call
107 # to Build(). The build method will reset the value to the number of
108 # bytes that are valid. If you have a variable length packet, failure
109 # to reset the value can cause an error.
110
111 # Now that the buffer is set, you could use other functions in TReK to
112 # send the data. Check out the Device Services API if you need to do
113 # that.
114
115 except trek.TrekError as err:
116 # Locate friendly message why it failed.
117 print(err.find_error_code())
118 print(err)
119
120
121if __name__ == "__main__":
122 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:12846