Python - Thread on Python

 
Vista:
sin imagen de perfil
Val: 5
Ha disminuido su posición en 17 puestos en Python (en relación al último mes)
Gráfica de Python

Thread on Python

Publicado por Humberto (2 intervenciones) el 01/12/2020 16:59:51
Hello friends, I need help, I am learning to work with visual python, at the moment I am using pyQT, but my question lies in the use of threads.

My application receives data from a device all the time through the Modbus protocol, and this is done in a different thread than the main program of the visual application, that is, from that thread I cannot modify any variable of the main program.

From the thread, Rx_Thread, I need to receive a data and show it in a textEdit of the main program.

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
from PyQt5 import QtWidgets
from Test_ui import Ui_MainWindow
import sys
from pyModbusTCP.client import ModbusClient
import time, threading
 
c=ModbusClient(host="127.0.0.1", port=502, auto_open=True)
text = "Hola"
 
class Main (QtWidgets.QMainWindow):
    def __init__(self):
        super (Main, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi (self)
 
    def Rx_Thread (): #este es el hilo que recibe los datos
        while (True):
            reg = c.read_holding_registers(0, 5)
            time.sleep(1)
            if reg:
                #self.ui.textEdit.setPlainText("%s\n" % text) #esto es lo que quiero modificar
                print(reg)
            else:
                print("not")
 
    _thread = threading.Thread(name='Rx_Thread',target=Rx_Thread, daemon = True)
    _thread.start()
 
 
if __name__ == '__main__':
    app=QtWidgets.QApplication([])
    main_app = Main()
    main_app.show()
    sys.exit(app.exec())
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Thread on Python

Publicado por tincopasan (1082 intervenciones) el 01/12/2020 18:14:00
podrías por lo menos mostrar que es lo que devuelve la variable reg, sino no hay forma de adivinar el formato que necesita para agregarlo y tampoco decis que tipo de error te da(si es que lo hace) o que resultado esperas, leyendo el código no hay forma de adivinar.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar
sin imagen de perfil
Val: 5
Ha disminuido su posición en 17 puestos en Python (en relación al último mes)
Gráfica de Python

Thread on Python

Publicado por Humberto (2 intervenciones) el 01/12/2020 18:26:51
Hola, gracias por responder, la variable reg recibe datos de temperatura, presion, pero no importa el tipo de variable, lo que no puedo es acceder a algun objeto fuera del hilo, o sea en este caso esos datos que me envia el sistema debo mostarlo en un label o un textedit da igual, el problema está en que en esa instancia del hilo como que no puede modificar un objeto de otro, no me da error, simplemente no lo hace
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar