#!/usr/bin/python3 # -*- coding: utf-8 -* #Bertrand Vandeportaele 2019 import os import sys debug=True ######################################## #https://stackoverflow.com/questions/1051254/check-if-python-package-is-installed #install automatically watchdog if not already installed import subprocess import sys reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze']) installed_packages = [r.decode().split('==')[0] for r in reqs.split()] if 'PyQt5' in installed_packages: if debug: print('PyQt5 pip package already installed') else: if debug: print('PyQt5 pip package missing, lets install it') import pip pip.main(['install','PyQt5']) ''' Could not find a version that satisfies the requirement PyQt5-Qt5>=5.15.2 (from PyQt5) (from versions: ) No matching distribution found for PyQt5-Qt5>=5.15.2 (from PyQt5) You are using pip version 9.0.1, however version 21.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Traceback (most recent call last): File "/home/bvandepo/Bureau/pythonb/staubli/simu_sensor_2_qt5.py", line 32, in from PyQt5.QtGui import * ModuleNotFoundError: No module named 'PyQt5' ''' ######################################## #from PyQt5 import QtCore, QtGui, QtWidgets ''' from PyQt5.QtGui import * from PyQt5.QtNetwork import * from PyQt5.QtCore import * from PyQt5 import * from PyQt5.QtWidgets import * ''' from PyQt5.QtGui import * from PyQt5.QtNetwork import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5 import * import re #pour découpage chaine comme sur https://stackoverflow.com/questions/2175080/sscanf-in-python global numeroPremiereDonneeCapteur #TODO: donner une valeur à cette variable en fonction de votre numéro d'étudiant numeroPremiereDonneeCapteur=56 global etatBouton etatBouton=0 global slider1Value slider1Value=0 global slider2Value slider2Value=0 global slider3Value slider3Value=0 global slider4Value slider4Value=0 global slider5Value slider5Value=0 global slider6Value slider6Value=0 global led0Button ################################################################################ def close(): print('close') exit(0) ################################################################################ def commutTimer(): global timer print('commutTimer') if timer.isActive(): timer.stop() commutTimerButton.setText('start stream sensor') else: timer.start() commutTimerButton.setText('stop stream sensor') ################################################################################ def slider1ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider1Value global slider1Value slider1Value=value #labelSlider1Value.setText( "J1: "+str(slider1Value) ) #https://mkaz.blog/code/python-string-format-cookbook/ labelSlider1Value.setText( str( numeroPremiereDonneeCapteur+0)+ " -> J1: {:6d}".format(slider1Value) ) print('changed') ################################################################################ def slider2ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider2Value global slider2Value slider2Value=value labelSlider2Value.setText( str( numeroPremiereDonneeCapteur+1)+ " -> J2: {:6d}".format(slider2Value) ) print('changed') ################################################################################ def slider3ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider3Value global slider3Value slider3Value=value labelSlider3Value.setText( str( numeroPremiereDonneeCapteur+2)+ " -> J3: {:6d}".format(slider3Value) ) print('changed') ################################################################################ def slider4ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider4Value global slider4Value slider4Value=value labelSlider4Value.setText( str( numeroPremiereDonneeCapteur+3)+ " -> J4: {:6d}".format(slider4Value) ) print('changed') ################################################################################ def slider5ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider5Value global slider5Value slider5Value=value labelSlider5Value.setText( str( numeroPremiereDonneeCapteur+4)+ " -> J5: {:6d}".format(slider5Value) ) print('changed') ################################################################################ def slider6ValueChanged( value ): #label.setText( str( value ) global numeroPremiereDonneeCapteur global labelSlider6Value global slider6Value slider6Value=value labelSlider6Value.setText( str( numeroPremiereDonneeCapteur+5)+ " -> J6: {:6d}".format(slider6Value) ) print('changed') ################################################################################ def bouton0(): global numeroPremiereDonneeCapteur global led0Button global etatBouton global labelButtonValue etatBouton=(etatBouton+1)%2 if etatBouton==0: led0Button.setText('Activer la sortie TOR') else: led0Button.setText('Desactiver la sortie TOR') labelButtonValue.setText( str( numeroPremiereDonneeCapteur+6)+" : {:6d}".format(etatBouton) ) print('etatBouton: '+ str(etatBouton) +" \n"); # led0Button.setText('Appuyer sur le bouton') # else: # led0Button.setText('Relacher le bouton') ################################################################################ def boutonReset(): print('reset: \n'); global slider1 global slider2 global slider3 global slider4 global slider5 global slider6 slider1.setValue(0); slider2.setValue(0); slider3.setValue(0); slider4.setValue(0); slider5.setValue(0); slider6.setValue(0); ################################################################################ def sendUDP(i): global numeroPremiereDonneeCapteur global udpSocket global destIP global portOut DestIP = QHostAddress(destIP); global slider1Value global slider2Value global slider3Value global slider4Value global slider5Value global slider6Value global etatBouton # chaine=str(slider1Value)+ " " + str(slider2Value) #ajout @MAC bidon # chaine="00:00:00:00:00:00 "+str(slider1Value)+ " " + str(slider2Value) #ajout champs vides pour être compatible avec le joystick wifi #@MAC: BC:DD:C2:FE:6F:F0 num: 0 0 -> 529.0 , 1 -> 534.0 , 2 -> 0.0 , 3 -> 0.0 , 4 -> 0.0 , 5 -> -73.0 , 6 -> 63.0 , chaine="00:00:00:00:00:00 "+str(numeroPremiereDonneeCapteur)+ " " +str(slider1Value)+ " " + str(slider2Value) + " " + str(slider3Value) + " " + str(slider4Value) + " " + str(slider5Value) + " " + str(slider6Value)+" " +str(etatBouton) #chaines bidons pour générer erreur de parsing sur le serveur # chaine="00:00:00:00:00:00 0 0 0 "+"687f"+" 0 0 "+str(slider1Value)+ " " + str(slider2Value) chaine=chaine+chr(13)+chr(10) print("la chaine envoyée est: "+chaine) udpSocket.writeDatagram(chaine.encode('utf-8'),DestIP, portOut); ################################################################################ def timerUpdate(): sendUDP(0) ################################################################################ global led0Button #destIP='192.168.1.50' #pc simulateur #destIP='192.168.0.112' #pc simulateur #destIP='192.168.1.49' #pc serveur rapid connecté en filaire à réseau AIP #destIP='127.0.0.1' #pc serveur sur la boucle locale destIP='172.16.6.59' #pc serveur mac oldi connecté en filaire à vlan BLEU IUT portOut=10000 posx=10 posy=10 sizex=500 sizey=150 ################################################################################ #par défaut: numeroPremiereDonneeCapteur= 100 #option -n pour choisir numeroPremiereDonneeCapteur if len(sys.argv)==3: if sys.argv[1]=='-n': numeroPremiereDonneeCapteur= int(sys.argv[2]) print("réglage de numeroPremiereDonneeCapteur à: ",str(numeroPremiereDonneeCapteur)); app = QApplication(sys.argv) w=QWidget(None) #QDialog() statusLabel =QLabel('En attente de datagrammes UDP depuis le PIC32') commutTimerButton=QPushButton('stop stream sensor') quitButton = QPushButton('&Quit') led0Button = QPushButton('Activer la sortie TOR') resetButton = QPushButton('reset axes') udpSocket =QUdpSocket() udpSocket.bind(portOut, QUdpSocket.ShareAddress) quitButton.clicked.connect(close) commutTimerButton.clicked.connect(commutTimer) led0Button.clicked.connect(bouton0) resetButton.clicked.connect(boutonReset) ''' app.connect(quitButton,QtCore.SIGNAL('clicked()'), close) app.connect(commutTimerButton, QtCore.SIGNAL('clicked()'), commutTimer) app.connect(led0Button,QtCore.SIGNAL('clicked()'), bouton0) ''' buttonLayout =QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(quitButton) buttonLayout.addWidget(commutTimerButton) buttonLayout.addWidget(resetButton) buttonLayout.addStretch(1) #button1Layout =QHBoxLayout() #button1Layout.addStretch(1) #button1Layout.addWidget(led0Button) #button1Layout.addStretch(1) global labelButtonValue labelButtonValue=QLabel( ) labelButtonValue.setGeometry(250, 50, 50, 35) #labelButtonValue.setText( "x: "+str(0 ) ) labelButtonValue.setText( str( numeroPremiereDonneeCapteur+6)+" : {:6d}".format(0) ) button1Layout =QHBoxLayout() button1Layout.addStretch(1) button1Layout.addWidget(labelButtonValue) button1Layout.addStretch(1) button1Layout.addWidget(led0Button) button1Layout.addStretch(1) #http://koor.fr/Python/CodeSamplesQt/PyQtSlider.wp ################################### global slider1 slider1 = QSlider(Qt.Horizontal); slider1.setMinimum(-160) slider1.setMaximum(160) slider1.setGeometry( 10, 10, 600, 40 ) slider1.valueChanged.connect(slider1ValueChanged ) global labelSlider1Value labelSlider1Value=QLabel( ) labelSlider1Value.setGeometry(250, 50, 50, 35) labelSlider1Value.setText( str( numeroPremiereDonneeCapteur+0)+ " -> J1: "+str(0 ) ) slider1Layout =QHBoxLayout() slider1Layout.addWidget(labelSlider1Value) slider1Layout.addWidget(slider1) slider1.setValue( 0 ) ################################### global slider2 slider2 = QSlider(Qt.Horizontal); #slider2.setMinimum(-127.5) #slider2.setMaximum(127.5) #nouvelle version supporte uniquement les entiers slider2.setMinimum(-128) slider2.setMaximum(128) slider2.setGeometry( 10, 10, 600, 40 ) slider2.valueChanged.connect(slider2ValueChanged ) global labelSlider2Value labelSlider2Value=QLabel( ) labelSlider2Value.setGeometry(250, 50, 50, 35) labelSlider2Value.setText( str( numeroPremiereDonneeCapteur+1)+ " -> J2: "+str(0 ) ) slider2Layout =QHBoxLayout() slider2Layout.addWidget(labelSlider2Value) slider2Layout.addWidget(slider2) slider2.setValue( 0 ) ################################### global slider3 slider3 = QSlider(Qt.Horizontal); #slider3.setMinimum(-134.5) #slider3.setMaximum(134.5) #nouvelle version supporte uniquement les entiers slider3.setMinimum(-135) slider3.setMaximum(135) slider3.setGeometry( 10, 10, 600, 40 ) slider3.valueChanged.connect(slider3ValueChanged ) global labelSlider3Value labelSlider3Value=QLabel( ) labelSlider3Value.setGeometry(250, 50, 50, 35) labelSlider3Value.setText( str( numeroPremiereDonneeCapteur+2)+ " -> J3: "+str(0 ) ) slider3Layout =QHBoxLayout() slider3Layout.addWidget(labelSlider3Value) slider3Layout.addWidget(slider3) slider3.setValue( 0 ) ################################### global slider4 slider4 = QSlider(Qt.Horizontal); slider4.setMinimum(-270) slider4.setMaximum(270) slider4.setGeometry( 10, 10, 600, 40 ) slider4.valueChanged.connect(slider4ValueChanged ) global labelSlider4Value labelSlider4Value=QLabel( ) labelSlider4Value.setGeometry(250, 50, 50, 35) labelSlider4Value.setText( str( numeroPremiereDonneeCapteur+3)+ " -> J4: "+str(0 ) ) slider4Layout =QHBoxLayout() slider4Layout.addWidget(labelSlider4Value) slider4Layout.addWidget(slider4) slider4.setValue( 0 ) ################################### global slider5 slider5 = QSlider(Qt.Horizontal); #slider5.setMinimum(-109.5) #slider5.setMaximum(+120.5) #nouvelle version supporte uniquement les entiers slider5.setMinimum(-110) slider5.setMaximum(+121) slider5.setGeometry( 10, 10, 600, 40 ) slider5.valueChanged.connect(slider5ValueChanged ) global labelSlider5Value labelSlider5Value=QLabel( ) labelSlider5Value.setGeometry(250, 50, 50, 35) labelSlider5Value.setText( str( numeroPremiereDonneeCapteur+4)+ " -> J5: "+str(0 ) ) slider5Layout =QHBoxLayout() slider5Layout.addWidget(labelSlider5Value) slider5Layout.addWidget(slider5) slider5.setValue( 0 ) ################################### global slider6 slider6 = QSlider(Qt.Horizontal); slider6.setMinimum(-270) slider6.setMaximum(270) slider6.setGeometry( 10, 10, 600, 40 ) slider6.valueChanged.connect(slider6ValueChanged ) global labelSlider6Value labelSlider6Value=QLabel( ) labelSlider6Value.setGeometry(250, 50, 50, 35) labelSlider6Value.setText( str( numeroPremiereDonneeCapteur+5)+ " -> J6: "+str(0 ) ) slider6Layout =QHBoxLayout() slider6Layout.addWidget(labelSlider6Value) slider6Layout.addWidget(slider6) slider6.setValue( 0 ) ################################### sliderLayout =QVBoxLayout() sliderLayout.addStretch(1) sliderLayout.addLayout(slider1Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider2Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider3Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider4Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider5Layout) sliderLayout.addStretch(1) sliderLayout.addLayout(slider6Layout) sliderLayout.addStretch(1) mainLayout = QVBoxLayout() mainLayout.addLayout(buttonLayout) mainLayout.addLayout(sliderLayout) mainLayout.addLayout(button1Layout) mainLayout.addWidget(statusLabel) w.setLayout(mainLayout) chaine='@BVDP2021: Port Em '+str(portOut) +' vers IP: '+str(destIP) w.setWindowTitle(chaine) #timer adapted from example 1 of https://www.programcreek.com/python/example/52106/PyQt4.QtCore.QTimer timer = QtCore.QTimer() #app.connect(timer, QtCore.SIGNAL('timeout()'), timerUpdate) timer.timeout.connect(timerUpdate) timer.setInterval(100) timer.start() w.setGeometry(posx,posy,sizex,sizey) #donne le focus au bouton 1 led0Button.setDefault(True) w.show() app.exec_()