TReK Python  5.3.3
Telemetry/Command API
ex_telem_get_next.py
1"""
2Example using get_next_packet().
3"""
4
5import trek
6
7# NOTE: This example requires the TReK Data GUI to be started and the PDSS Payload
8# Packet with APID 7 to be activated with a Real-Time data mode.
9
10
11def main() -> None:
12 """
13 Main Routine
14 """
15
16 try:
17 tlm_api = trek.TelemetryApi()
18 pkt = trek.Packet()
19 packet_buffer = bytearray(4096)
20 token: int = 0
21 last_token: int = 0
22 status: int
23
24 # Connect to a data store.
25 tlm_api.connect("DefaultDataStore")
26
27 # Calling this method allows the API to clean up resources that may be
28 # left over from a user program that crashed. Not checking the return
29 # value as it is safe to continue if this method fails.
30 tlm_api.cleanse()
31
32 # Get the packet definition so that parameters can be extracted
33 # from the packet.
34 tlm_api.get_packet_definition("PdssPayload.RT.PL.7", pkt)
35
36 # Register a signal that will allow you to know when a packet arrives.
37 tlm_api.register_packet_semaphore("PdssPayload.RT.PL.7")
38
39 # Check to see if a packet is already arrived and process all of them. Once there
40 # is no more new data, wait for the next packet to arrive and continue. This code
41 # will exit once the packet arrival event is released 100 times.
42 event_triggered: int = 0
43 while True:
44
45 while True:
46 (token, ret_size, status) = tlm_api.get_next_packet(
47 "PdssPayload.RT.PL.7", token, packet_buffer
48 )
49
50 if status in (
51 trek.SUCCESS,
52 trek.TLM_API_MORE_DATA_AVAILABLE,
53 trek.TLM_API_MISSING_DATA,
54 ):
55
56 try:
57 # Use the packet definition to extract the data.
58 _ = pkt.extract(packet_buffer, ret_size)
59 except trek.TrekError:
60 print("Could not extract parameter data from the packet.")
61 continue
62
63 if last_token == token:
64 # Data has not changed. this is possible when using packet arrival
65 # triggers and get next packet. if preventing duplicate data is important,
66 # this is a good check.
67 pass
68 else:
69 # The packet has been extracted and data is new. You can also print all
70 # of the values in the Packet at one time.
71 last_token = token
72 print("Printing all parameter values:")
73 print(f"{pkt.print_values()}")
74
75 if status == trek.SUCCESS: # We got all the data, so end.
76 break
77
78 # Now wait for a new packet to arrive. While you are retrieving that packet other data
79 # may also arrive. The GetNextPacket code above will get it without having to wait
80 # again if this occurs.
81 if not tlm_api.wait_for_packet("PdssPayload.RT.PL.7"):
82 # using default, infinite wait
83 # If you have a timeout, you may get TCA_WAIT_TIMEOUT returned. You can wait again
84 # after a timeout as it is expected behavior and not an error.
85 print("Packet did not arrive in timeout period.")
86
87 event_triggered += 1
88
89 if event_triggered < 100:
90 break # exit while loop
91
92 # You should unregister the signal to free up resources if it is no longer needed.
93 tlm_api.unregister_packet_semaphore("PdssPayload.RT.PL.7")
94
95 # It is always best to call Disconnect when you are finished with the
96 # API. There are system resources that can be freed when you no
97 # longer need the API.
98 tlm_api.disconnect()
99
100 except trek.TrekError as err:
101 # Locate friendly message why it failed.
102 print(err.find_error_code())
103 print(err)
104
105
106if __name__ == "__main__":
107 main()
This class describes a packet composed of one or more parameters.
Definition: trek.py:5009
Provides access to telemetry features of TReK.
Definition: trek.py:12090
The TReK C++ was designed to return error codes.
Definition: trek.py:12835