6 from threading
import Thread
12 import matplotlib.pyplot
as plt
13 import matplotlib.animation
as animation
30 self.server.nicla_disconnect =
False
34 packet = self.request.recv(65000)
38 self.server.pdm_buffer.put_nowait(packet)
51 except Exception
as e:
52 print(f
"Exception: {e}")
53 self.server.receiving_buffer.queue.clear()
54 self.server.nicla_disconnect =
True
70 super().
__init__((server_ip, server_port), TCPHandler)
111 bytes_arr = bytes([])
126 size = int.from_bytes(bytes_arr[:4],
"little")
127 bytes_arr = bytes_arr[4:]
129 print(
"SIZE: ", size)
131 print(
"BYTES LEN: ", len(bytes_arr))
133 if len(bytes_arr) >= size:
137 packet = bytes_arr[:size]
139 print(
"TIMESTAMP: ", int.from_bytes(bytes_arr[:4],
"little"))
140 print(
"TYPE: ", int.from_bytes(bytes_arr[4:5],
"little"))
143 with open(
"my_file_" +
str(cnt) +
".txt",
"wb")
as binary_file:
145 binary_file.write(packet[5:])
147 bytes_arr = bytes_arr[size:]
179 if len(self.
numbers) >= 16000 * 5:
185 with wave.open(
"audio.wav",
"w")
as wf:
188 wf.setframerate(16000)
189 wf.writeframes(self.
numbers.tobytes())
205 bkp_bytes_packets = bytes([])
212 timestamp = struct.unpack(
">d", bytes_packets[:8])[0]
213 bytes_packets = bytes_packets[8:]
216 bytes_packets = bytes([])
217 bkp_bytes_packets = bytes([])
221 bytes_packets = bkp_bytes_packets + bytes_packets
222 bkp_bytes_packets = bytes([])
224 if len(bytes_packets) < 9:
225 print(
"Got a packet from receiver less than header size!")
228 total_length = len(bytes_packets)
229 loop_termination_flag =
True
231 while loop_termination_flag:
232 size_packet = int.from_bytes(bytes_packets[:4],
"big")
234 if total_length - 4 >= size_packet:
235 packet = bytes_packets[4 : size_packet + 4]
236 bytes_packets = bytes_packets[size_packet + 4 :]
240 data_type = packet[4]
243 if data_type == RANGE_TYPE:
249 elif data_type == IMAGE_TYPE:
255 elif data_type == AUDIO_TYPE:
261 elif data_type == IMU_TYPE:
268 bkp_bytes_packets = bytes_packets
269 loop_termination_flag =
False
271 total_length = len(bytes_packets)
272 if total_length == 0:
273 loop_termination_flag =
False
309 if __name__ ==
"__main__":
321 nicla_receiver_server.serve()
322 except Exception
as e:
329 nicla_receiver_server.stop()