client-gui.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import numpy as np
  2. from matplotlib.figure import Figure
  3. import socket
  4. import msgpack
  5. from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QPushButton, QHBoxLayout, QMessageBox
  6. from PySide6.QtWidgets import QDialog, QFormLayout, QLineEdit, QLabel, QComboBox, QSpinBox, QToolButton, QFileDialog
  7. from PySide6.QtCore import QTimer
  8. from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
  9. from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
  10. from PySide6.QtWidgets import QSizePolicy
  11. import json
  12. class MplCanvas(FigureCanvas):
  13. def __init__(self, parent=None, width=5, height=4, dpi=100):
  14. self.fig = Figure(figsize=(width, height), dpi=dpi, facecolor='grey')
  15. super().__init__(self.fig)
  16. self.setParent(parent)
  17. self.axes = self.fig.add_subplot(111)
  18. # Построим простой график
  19. self.axes.plot([0, 1, 2, 3], [10, 1, 20, 3])
  20. self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
  21. class SDRClient(QMainWindow):
  22. def __init__(self):
  23. super().__init__()
  24. # Параметры подключения к серверу
  25. self.server_ip = '127.0.0.1'
  26. self.server_port = 5003
  27. self.sock = None
  28. # Параметры для отрисовки
  29. self.channel_data = {
  30. 'time': np.array([]),
  31. 'channel_g': np.array([]),
  32. 'channel_rf': np.array([]),
  33. 'channel_adc_ttl': np.array([]),
  34. 'channel_rf_ttl': np.array([])
  35. }
  36. # Настройка окна
  37. self.setWindowTitle("SDR Client")
  38. self.setGeometry(100, 100, 1280, 720)
  39. self.central_widget = QWidget()
  40. self.setCentralWidget(self.central_widget)
  41. self.layout = QHBoxLayout()
  42. self.plot_layout = QVBoxLayout()
  43. self.menu_layout = QVBoxLayout()
  44. self.layout.addLayout(self.menu_layout)
  45. self.layout.addLayout(self.plot_layout)
  46. self.central_widget.setLayout(self.layout)
  47. self.command_layout = QHBoxLayout()
  48. self.plot_layout.addLayout(self.command_layout)
  49. self.add_event_button = QPushButton("Add Event")
  50. self.command_layout.addWidget(self.add_event_button)
  51. self.add_rfwave_button = QPushButton("Add RF Wave")
  52. self.command_layout.addWidget(self.add_rfwave_button)
  53. self.run_event_list_button = QPushButton("Run Event List")
  54. self.command_layout.addWidget(self.run_event_list_button)
  55. self.clear_event_list_button = QPushButton("Clear Event List")
  56. self.command_layout.addWidget(self.clear_event_list_button)
  57. self.plot_canvas = MplCanvas(self, width=5, height=4, dpi=100)
  58. #self.plot_canvas.setSizePolicy(self.plot_widget.sizePolicy())
  59. self.plot_layout.addWidget(self.plot_canvas, stretch=1)
  60. toolbar = NavigationToolbar(self.plot_canvas, self)
  61. self.plot_layout.addWidget(toolbar, stretch=1)
  62. self.connect_button = QPushButton("Connect to SDR")
  63. self.connect_button.setMinimumSize(200, 50)
  64. self.disconnect_button = QPushButton("Disconnect")
  65. self.disconnect_button.setMinimumSize(200, 50)
  66. self.settings_button = QPushButton("Settings")
  67. self.settings_button.setMinimumSize(200, 50)
  68. self.recv_sim_data_button = QPushButton("Receive Sim Data")
  69. self.recv_sim_data_button.setMinimumSize(200, 50)
  70. self.menu_layout.addWidget(self.connect_button)
  71. self.menu_layout.addWidget(self.disconnect_button)
  72. self.menu_layout.addWidget(self.settings_button)
  73. self.menu_layout.addWidget(self.recv_sim_data_button)
  74. self.connect_button.clicked.connect(self.connect_to_server)
  75. self.disconnect_button.clicked.connect(self.disconnect_from_server)
  76. self.add_event_button.clicked.connect(self.add_event_dialog)
  77. def connect_to_server(self):
  78. try:
  79. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  80. self.sock.connect((self.server_ip, self.server_port))
  81. print(f"Connected to SDR server at {self.server_ip}:{self.server_port}")
  82. except Exception as e:
  83. print(f"Failed to connect to server: {e}")
  84. QMessageBox.critical(self, "Connection Error", f"Failed to connect to server: {e}")
  85. self.sock = None
  86. def disconnect_from_server(self):
  87. if self.sock:
  88. self.sock.close()
  89. self.sock = None
  90. QMessageBox.information(self, "Disconnected", "Successfully disconnected from SDR server")
  91. print("Disconnected from SDR server")
  92. def add_event_dialog(self):
  93. #if not self.sock:
  94. # QMessageBox.warning(self, "Not Connected", "Please connect to the SDR server first")
  95. # return False
  96. add_event_dialog = QDialog(self)
  97. add_event_dialog.setWindowTitle("Add Event")
  98. layout = QFormLayout()
  99. labels = ["Start Time (ticks)", "Duration (ticks)", "Waveform Type", "time_front_rf_ttl (ticks)", "time_back_rf_ttl (ticks)", "time_front_adc_ttl (ticks)", "time_back_adc_ttl (ticks)", "time_start_rf (ticks)", "time_end_rf (ticks)", "rf_freq (Hz)"]
  100. self.event_inputs = {}
  101. for label in labels:
  102. if label == "Waveform Type":
  103. combo = QComboBox()
  104. combo.addItems(["sine", "square", "triangle"])
  105. layout.addRow(QLabel(label), combo)
  106. self.event_inputs[label] = combo
  107. else:
  108. line_edit = QLineEdit()
  109. layout.addRow(QLabel(label), line_edit)
  110. self.event_inputs[label] = line_edit
  111. submit_button = QPushButton("Submit")
  112. layout.addWidget(submit_button)
  113. load_event_list_button = QPushButton("Load Event List")
  114. layout.addWidget(load_event_list_button)
  115. submit_button.clicked.connect(self.submit_event)
  116. load_event_list_button.clicked.connect(self.load_event_list)
  117. add_event_dialog.setLayout(layout)
  118. add_event_dialog.exec()
  119. def submit_event(self):
  120. try:
  121. event_data = {
  122. "time": int(self.event_inputs["Start Time (ticks)"].text()),
  123. "duration": int(self.event_inputs["Duration (ticks)"].text()),
  124. "wave": self.event_inputs["Waveform Type"].currentText(),
  125. "time_front_rf_trigger": int(self.event_inputs["time_front_rf_ttl (ticks)"].text()),
  126. "time_back_rf_trigger": int(self.event_inputs["time_back_rf_ttl (ticks)"].text()),
  127. "time_front_adc_trigger": int(self.event_inputs["time_front_adc_ttl (ticks)"].text()),
  128. "time_back_adc_trigger": int(self.event_inputs["time_back_adc_ttl (ticks)"].text()),
  129. "rf_start": int(self.event_inputs["time_start_rf (ticks)"].text()),
  130. "rf_end": int(self.event_inputs["time_end_rf (ticks)"].text()),
  131. "carrier_freq": float(self.event_inputs["rf_freq (Hz)"].text())
  132. }
  133. msg = msgpack.packb({"command": 0x10, "data": event_data})
  134. self.sock.sendall(msg)
  135. print("Event added successfully")
  136. QMessageBox.information(self, "Success", "Event added successfully")
  137. self.add_event_dialog.close()
  138. except Exception as e:
  139. print(f"Failed to add event: {e}")
  140. QMessageBox.critical(self, "Error", f"Failed to add event: {e}")
  141. def load_event_list(self):
  142. file_dialog = QFileDialog(self)
  143. file_dialog.setNameFilter("JSON Files (*.json)")
  144. if file_dialog.exec():
  145. selected_file = file_dialog.selectedFiles()[0]
  146. try:
  147. with open(selected_file, 'r') as f:
  148. event_list = json.load(f)
  149. for event_data in event_list:
  150. msg = {
  151. "magic": 0xAA,
  152. "cmd": 0x10,
  153. "time": event_data["time"],
  154. "duration": event_data["duration"],
  155. "wave": event_data["wave"],
  156. "time_front_rf_trigger": event_data["time_front_rf_trigger"],
  157. "time_back_rf_trigger": event_data["time_back_rf_trigger"],
  158. "time_front_adc_trigger": event_data["time_front_adc_trigger"],
  159. "time_back_adc_trigger": event_data["time_back_adc_trigger"],
  160. "rf_start": event_data["rf_start"],
  161. "rf_end": event_data["rf_end"],
  162. "carrier_freq": event_data["carrier_freq"]
  163. }
  164. packed_msg = msgpack.packb(msg, use_bin_type=True)
  165. self.sock.sendall(packed_msg)
  166. resp = self.sock.recv(1024)
  167. if not resp:
  168. raise Exception("No response from server")
  169. unpacked_resp = msgpack.unpackb(resp, raw=False)
  170. if unpacked_resp["cmd"] != 0xFE and unpacked_resp["code"] != 0x00000010:
  171. raise Exception(f"Server error: {unpacked_resp['message']}")
  172. print("Event list loaded successfully")
  173. QMessageBox.information(self, "Success", "Event list loaded successfully")
  174. self.add_event_dialog.close()
  175. except Exception as e:
  176. print(f"Failed to load event list: {e}")
  177. QMessageBox.critical(self, "Error", f"Failed to load event list: {e}")
  178. app = QApplication([])
  179. client = SDRClient()
  180. client.show()
  181. #plt.ion() # Enable interactive mode for plotting
  182. #plt.show()
  183. app.exec()