From 99a5b1bcd300c690807170c68057413353bbf2be Mon Sep 17 00:00:00 2001 From: 45coll <674148718@qq.com> Date: Tue, 24 Aug 2021 15:32:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gui/__pycache__/main_ui.cpython-37.pyc | Bin 1931 -> 2717 bytes .../gui/__pycache__/wifi_udp.cpython-37.pyc | Bin 1086 -> 1168 bytes python_gui/gui/main.py | 85 ++++++++++++------ python_gui/gui/main_ui.py | 25 ++++++ python_gui/gui/main_ui.ui | 61 +++++++++++++ python_gui/gui/test2.py | 49 ++-------- python_gui/gui/wifi_udp.py | 7 +- 7 files changed, 157 insertions(+), 70 deletions(-) diff --git a/python_gui/gui/__pycache__/main_ui.cpython-37.pyc b/python_gui/gui/__pycache__/main_ui.cpython-37.pyc index 81b729af98ed360289b609a0a178d3c1090dc710..7e3c881f0869ebbad36f766a109f1d6b0e16e164 100644 GIT binary patch delta 1294 zcmZ8fOH30{6rI=av;{0g5h#>jffh>nC@TIIH6SKpXwZ-}kY?l!Fl3w|uQQ6INlA3! zXQLToqVY4t#GNEAh>1ZLhNbS@*u*8Dt_%y;df)3XpqrE9Tut$paTLdJ=L*PJ zYY`~c#V|74OLle(9XZ4bMS+W2UV17Aj@#ZSc)KlcjOr+cHaacuKo^i*AWB%GI9)T$ zncY+ba(+Hx_LMN@#<^IJvElfU--U4&&p{~gvqaSwlImjdC*>L`S3Rc^X-QQyLB1^9 zR&{Mc!n2}QL^K)vKUoOgl4hjLrLl}GDdG^8v_dswrd4G|n$0Ze*}0^yX{s_T<)B1y zife&bPm9$uArYr zwi&<0cx)zMG2Rlxz6bsa@zAS4y}A$ni_5P!Kfe97{ygn1O|^Y&DbP3ApXfVrDseo~ zuT$~P>gVlGs|;@4H#XOoV`avO=UBdi7V^S9UKJNLQVAWkFur-t^mK!Q84eU- pU(8#q$9wcR)Do{0j)*R@ndm delta 491 zcmbO$+Re}B#LLUY00ik%r4#pXP2@Ym^oDWcl?=xE8pZ`uH4IrS3mF+1N|+W%*Dx+* ztYL_k0gA~iU@zfVz?mWqgiI;23&Co@;tVNrAoW1KvLHTGuN+jbJWx!20Yi#BRE+|N z4`wqgPz0#~+QgBiv_Kii<^*z57=jrz6(+x9z9}UIbmA@c+|<0%q{O0IoW&)HC8fnc zrl!i`W>&_@FWF?|#DPkpxQbIte1WQ*5{sg^0)xTIfb1fP$;#}3(lS6Lzl>e1V$$-H z;|q%Nvr>~wVoXev^K)`0Ph{6&beX)5-E^`vhdra-y(>cKj}nplCdd_Y{x0wg#XIaDWma_MLwOopqx V#bJ}1pHiBWYR3&y&IBYxm;u!Pb!7km diff --git a/python_gui/gui/__pycache__/wifi_udp.cpython-37.pyc b/python_gui/gui/__pycache__/wifi_udp.cpython-37.pyc index e8301b4dbbb631c2cd016d35c504ae44efe1f907..e099e090c5e36309bc8530f86313907058f7f9b1 100644 GIT binary patch delta 531 zcmZutJ8K(35Z;}8txhM&NJeow7shanLP+5dLLiCaCSXiVh>>N)>24(yOLt;-mBKi< za7ZmUV{j3$$`meAxb+X&-g!XU^qD<3F1*8hJ3G&B=bFt+l~;5{uy1AVHzWVa>?7VA|a}Qc8%NQi!QX^Fe0H}{r zhx`q6+X2QpuHo$7OtKH|u}I?-xm8#dw@iI-M!eMEka9L6Zp{e;SBv zB1#zO*e~1dciNeXQL40?@Pc~Hl-$EWv*(j6nT`1ivw3McGrH7hGF%E!Mt!IwE1=8p EAGg(Xg8%>k delta 432 zcmYjNy-ve05VoB(b$=4t0v$j+K$f6Zp-@yI0ojlk=vEa~Y3!m@XWZ*%VIESUS?z8=!?z_+TRQaOjnQ2xLtjC{SfBtD&*wWyo;v6CwWH#P+ z?$NybLK%6*7%?KuG0C-S~wwCJ&k7m*U zFrMl{~neNwJU=5+nW8^RDvJQigGw8;mQbc`1lQ n!q>VL%?pooun=q&@e>TyNgIG}<3^Lh?+K>3N?KTl+ZOo)1`}H= diff --git a/python_gui/gui/main.py b/python_gui/gui/main.py index 3a6d145..78f965d 100644 --- a/python_gui/gui/main.py +++ b/python_gui/gui/main.py @@ -1,5 +1,5 @@ import sys -from PyQt5.QtWidgets import QApplication, QMainWindow +from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox from main_ui import * from wifi_udp import * import threading #引入并行 @@ -11,50 +11,79 @@ class MyWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MyWindow, self).__init__(parent) self.setupUi(self) - self.v_list=[] - self.v_list.append(np.zeros(300)) - self.timeArray = np.arange(-300, 0, 1) + # 变量初始化 + self.variable_init() + # 设置实例 + self.CreateItems() + # 设置信号与槽 + self.CreateSignalSlot() + # 图表初始化 + self.plot_init() - self.velocity = 0 - # 滑条 - self.velocity_lineEdit.setText(str(self.velocity_horizontalSlider.value())) + # 设置信号与槽 + def CreateSignalSlot(self): + self.wifi_recv_open_pushButton.clicked.connect(self.wifi_recv_open_pushButton_clicked) self.velocity_horizontalSlider.valueChanged.connect(self.velocity_horizontalSlider_valueChanged) + self.wifi_config_pushButton.clicked.connect(self.wifi_config_pushButton_clicked) + + # 设置实例 + def CreateItems(self): + # 定时器-绘图刷新 + self.timer = QtCore.QTimer() + self.timer.timeout.connect(self.update_plot) + self.timer.start(50) # wifi udp self.udp = udp() + self.wifi_IP_lineEdit.setText(self.udp.user_ip) + def variable_init(self): + # 图表数据变量 + self.wifi_recv_flag = 0 + self.udp_data = 0 + self.target_velocity = 0 + self.now_velocity = 0 - # 接收数据 - self.udp_data = '' - t1 = threading.Thread(target=self.udp_recv) - t1.start() + def plot_init(self): + # 图表可视化数组 + self.v_list = [] + self.v_list.append(np.zeros(300)) + self.timeArray = np.arange(-300, 0, 1) # 绘图对象 self.pw = pg.PlotWidget() self.curve = self.pw.plot(pen='y') self.gridLayout.addWidget(self.pw) - # 定时器 - self.timer = QtCore.QTimer() - self.timer.timeout.connect(self.update_plot) - self.timer.start(50) - - - # 滑条事件绑定 + # 滑条绑定 def velocity_horizontalSlider_valueChanged(self): - value = self.velocity_horizontalSlider.value() - self.velocity_lineEdit.setText(str(value)) - # self.udp.send_message(str(value)) - # self.curve.updateItems() - # self.curve.sigPlotChanged.emit(self.curve) + self.target_velocity = self.velocity_horizontalSlider.value() + self.velocity_lineEdit.setText(str(self.target_velocity)) + self.udp.send_message(str(self.velocity_lineEdit)) + def wifi_config_pushButton_clicked(self): + try: + self.udp.udpClientSocket.bind((self.wifi_IP_lineEdit.text(), 2333)) + t1 = threading.Thread(target=self.udp_recv) + t1.start() + self.wifi_recv_open_pushButton.setEnabled(True) + except: + QMessageBox.critical(self, "错误", '该请求的地址无效') + def wifi_recv_open_pushButton_clicked(self): + if self.wifi_recv_flag == 0: + # 打开wifi接收 + self.wifi_recv_flag = 1 + self.wifi_recv_open_pushButton.setText('关闭') + else: + self.wifi_recv_flag = 0 + self.wifi_recv_open_pushButton.setText('打开') def udp_recv(self): - while True: + while self.wifi_recv_flag: recv_data = self.udp.udpClientSocket.recv(1024) recv_data = recv_data.decode('utf-8') self.udp_data = recv_data def update_plot(self): - print(self.udp_data) - self.v_list[0] = np.roll(self.v_list[0], -1) - self.v_list[0][-1] = self.udp_data - self.curve.setData(self.timeArray, self.v_list[0]) # 在绘图部件中绘制折线图 + if self.wifi_recv_flag: + self.v_list[0] = np.roll(self.v_list[0], -1) + self.v_list[0][-1] = self.udp_data + self.curve.setData(self.timeArray, self.v_list[0]) # 在绘图部件中绘制折线图 if __name__ == '__main__': app = QApplication(sys.argv) myWin = MyWindow() diff --git a/python_gui/gui/main_ui.py b/python_gui/gui/main_ui.py index 2151d33..dab51df 100644 --- a/python_gui/gui/main_ui.py +++ b/python_gui/gui/main_ui.py @@ -37,6 +37,26 @@ class Ui_MainWindow(object): self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") + self.groupBox = QtWidgets.QGroupBox(self.centralwidget) + self.groupBox.setGeometry(QtCore.QRect(20, 420, 241, 61)) + self.groupBox.setObjectName("groupBox") + self.horizontalLayoutWidget = QtWidgets.QWidget(self.groupBox) + self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 20, 221, 31)) + self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget") + self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget) + self.horizontalLayout.setContentsMargins(0, 0, 0, 0) + self.horizontalLayout.setSpacing(1) + self.horizontalLayout.setObjectName("horizontalLayout") + self.wifi_IP_lineEdit = QtWidgets.QLineEdit(self.horizontalLayoutWidget) + self.wifi_IP_lineEdit.setObjectName("wifi_IP_lineEdit") + self.horizontalLayout.addWidget(self.wifi_IP_lineEdit) + self.wifi_config_pushButton = QtWidgets.QPushButton(self.horizontalLayoutWidget) + self.wifi_config_pushButton.setObjectName("wifi_config_pushButton") + self.horizontalLayout.addWidget(self.wifi_config_pushButton) + self.wifi_recv_open_pushButton = QtWidgets.QPushButton(self.centralwidget) + self.wifi_recv_open_pushButton.setEnabled(False) + self.wifi_recv_open_pushButton.setGeometry(QtCore.QRect(180, 380, 93, 28)) + self.wifi_recv_open_pushButton.setObjectName("wifi_recv_open_pushButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26)) @@ -52,4 +72,9 @@ class Ui_MainWindow(object): def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) + self.velocity_lineEdit.setText(_translate("MainWindow", "0")) self.label.setText(_translate("MainWindow", "速度:")) + self.groupBox.setTitle(_translate("MainWindow", "wifi_IP")) + self.wifi_IP_lineEdit.setText(_translate("MainWindow", "192.168.4.2")) + self.wifi_config_pushButton.setText(_translate("MainWindow", "设置")) + self.wifi_recv_open_pushButton.setText(_translate("MainWindow", "打开")) diff --git a/python_gui/gui/main_ui.ui b/python_gui/gui/main_ui.ui index 6fb30ee..ffa1791 100644 --- a/python_gui/gui/main_ui.ui +++ b/python_gui/gui/main_ui.ui @@ -48,6 +48,9 @@ 21 + + 0 + @@ -73,6 +76,64 @@ + + + + 20 + 420 + 241 + 61 + + + + wifi_IP + + + + + 10 + 20 + 221 + 31 + + + + + 1 + + + + + 192.168.4.2 + + + + + + + 设置 + + + + + + + + + false + + + + 180 + 380 + 93 + 28 + + + + 打开 + + diff --git a/python_gui/gui/test2.py b/python_gui/gui/test2.py index 2e4884c..9e74959 100644 --- a/python_gui/gui/test2.py +++ b/python_gui/gui/test2.py @@ -1,42 +1,9 @@ -# -*- coding: utf-8 -*- -""" -This example demonstrates many of the 2D plotting capabilities -in pyqtgraph. All of the plots may be panned/scaled by dragging with -the left/right mouse buttons. Right click on any plot to show a context menu. -""" +import socket -from pyqtgraph.Qt import QtGui, QtCore -import numpy as np -import pyqtgraph as pg - -#QtGui.QApplication.setGraphicsSystem('raster') -app = QtGui.QApplication([]) -#mw = QtGui.QMainWindow() -#mw.resize(800,800) - -win = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples") -win.resize(1000,600) -win.setWindowTitle('pyqtgraph example: Plotting') - -# Enable antialiasing for prettier plots -pg.setConfigOptions(antialias=True) - -p6 = win.addPlot(title="Updating plot") -curve = p6.plot(pen='y') -data = np.random.normal(size=(10,1000)) -ptr = 0 -def update(): - global curve, data, ptr, p6 - curve.setData(data[ptr%10]) - if ptr == 0: - p6.enableAutoRange('xy', False) ## stop auto-scaling after the first data set is plotted - ptr += 1 -timer = QtCore.QTimer() -timer.timeout.connect(update) -timer.start(50) - -## Start Qt event loop unless running in interactive mode or using pyside. -if __name__ == '__main__': - import sys - if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): - QtGui.QApplication.instance().exec_() +try: + s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) + s.connect(('8.8.8.8',80)) + ip = s.getsockname()[0] +finally: + s.close() +print(ip) \ No newline at end of file diff --git a/python_gui/gui/wifi_udp.py b/python_gui/gui/wifi_udp.py index 488e3e5..65cdd3e 100644 --- a/python_gui/gui/wifi_udp.py +++ b/python_gui/gui/wifi_udp.py @@ -9,8 +9,13 @@ class udp(object): self.ADDRESS = (self.HOST, self.PORT) self.udpClientSocket = socket(AF_INET, SOCK_DGRAM) - self.udpClientSocket.bind(("192.168.4.2",2333)) + try: + s = socket(AF_INET, SOCK_DGRAM) + s.connect(('8.8.8.8', 80)) + self.user_ip = s.getsockname()[0] + finally: + s.close() def send_message(self,data): if not data: return 0