-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_receiver.py
More file actions
150 lines (126 loc) · 5.08 KB
/
plot_receiver.py
File metadata and controls
150 lines (126 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
import serial
import serial.tools.list_ports
import tkinter as tk
t = 0
lat_data = []
long_data = []
high_data = []
time_data = []
x = ""
list_con = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ".", '-']
ports = serial.tools.list_ports.comports()
print("--------------------Порты--------------------")
for port in ports:
print(port.device)
print("---------------------------------------------")
my_port = input("По умолчанию COM14. Для смены введите номер порта(COM#)/оставить как есть(0): ")
my_port = "COM" + my_port
if my_port == "0":
my_port = "COM14"
baudrate = 115200
ser = serial.Serial(my_port, baudrate=baudrate)
def get_data(t=t, x=x):
t = time.time()
x = x
data = ser.read(62)
print(data)
decoded_data = data.decode('utf-8', errors='ignore').strip()
parts = decoded_data.split(';')
try:
for elem in parts[0]:
if elem in list_con:
x += elem
lat = float(x) if x.replace('.', '', 1).isdigit() else None
x = ''
for elem in parts[1]:
if elem in list_con:
x += elem
long = float(x) if x.replace('.', '', 1).isdigit() else None
x = ''
for elem in parts[2]:
if elem in list_con:
x += elem
high = float(x) if x.replace('.', '', 1).lstrip('-').isdigit() else None
x = ''
data = {"latitude": lat, "longitude": long, "height": high}
return lat, long, high, t
except (ValueError, IndexError):
print("Ошибка при обработке данных!")
lat = lat_data[-1] if lat_data else None
long = long_data[-1] if long_data else None
high = high_data[-1] if high_data else None
return lat, long, high, t
def update(frame):
lat, long, high, current_time = get_data()
lat_data.append(lat)
long_data.append(long)
high_data.append(high)
time_data.append(current_time)
if len(time_data) > 300:
lat_data.pop(0)
long_data.pop(0)
high_data.pop(0)
time_data.pop(0)
ax1.clear()
ax1.plot(lat_data, long_data, color="white")
ax1.set_title("Широта от долготы", fontsize=12, color="#00ff00")
ax1.grid(True, color="#00ff00", alpha=0.3)
ax1.set_facecolor("black")
ax1.tick_params(colors="#00ff00")
ax2.clear()
ax2.plot(time_data, high_data, color="white")
ax2.set_title("Высота от времени", fontsize=12, color="#00ff00")
ax2.grid(True, color="#00ff00", alpha=0.3)
ax2.set_facecolor("black")
ax2.tick_params(colors="#00ff00")
def copy_coordinates():
if lat_data and long_data:
latest_lat = lat_data[-1]
latest_long = long_data[-1]
coordinates = f"{latest_lat}, {latest_long}"
root.clipboard_clear() # Clear the clipboard
root.clipboard_append(coordinates) # Append the coordinates to the clipboard
print(f"Скопировано в буфер обмена: {coordinates}")
# Создаем основное окно
root = tk.Tk()
root.title("Актуальные данные")
root.configure(bg="black")
# Создаем заголовок
title_label = tk.Label(root, text="Актуальные данные", bg="black", fg="#00ff00", font=("Arial", 16))
title_label.pack(pady=10)
# Создаем метки для отображения координат
lat_label = tk.Label(root, text="Широта: ", bg="black", fg="white", font=("Arial", 12))
lat_label.pack(pady=5)
long_label = tk.Label(root, text="Долгота: ", bg="black", fg="white", font=("Arial", 12))
long_label.pack(pady=5)
high_label = tk.Label(root, text="Высота: ", bg="black", fg="white", font=("Arial", 12))
high_label.pack(pady=5)
# Создаем кнопку для копирования координат
copy_button = tk.Button(root, text="Копировать координаты", command=copy_coordinates, bg="black", fg="#00ff00", font=("Arial", 12))
copy_button.pack(pady=10)
# Создаем фигуру и оси
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 10))
fig.patch.set_facecolor("black")
# Настраиваем отступы
fig.subplots_adjust(top=0.9, bottom=0.1, left=0.1, right=0.9, hspace=0.5)
# Создаем анимацию
ani = FuncAnimation(fig, update, interval=1000)
def update_labels():
if lat_data and long_data and high_data:
latest_lat = lat_data[-1]
latest_long = long_data[-1]
latest_high = high_data[-1]
lat_label.config(text=f"Широта: {latest_lat}")
long_label.config(text=f"Долгота: {latest_long}")
high_label.config(text=f"Высота: {latest_high}")
# Запускаем обновление меток в отдельном потоке
ani.event_source.add_callback(update_labels)
# Отображаем графики
plt.tight_layout()
plt.show(block=False)
# Запускаем основной цикл Tkinter
root.mainloop()
ser.close() # Remember to close the connection when done