automatic updating pyqt gui displayed value (opc-ua client)


automatic updating pyqt gui displayed value (opc-ua client)



Hello python enthousiasts/pro's,



I would like to create an OPC-UA client Graphical User Interface in python using the FreeOpc library, and PyQt & QtDesigner. I am starting with a simple code where two process values are displayed and updated every DeltaT, where DeltaT is a time interval specified by the user. (In the code I provided, the DeltaT is not implemented.)



Here is the OPC server code I use:


from opcua import Client
import time

url = "opc.tcp://localhost:4840"

client = Client(url)

client.connect()
print("Client Connected")

while True:
Temp = client.get_node("ns=2;i=2")
Temperature = Temp.get_value()
print(Temperature)

Press = client.get_node("ns=2;i=3")
Pressure = Press.get_value()
print(Pressure)

TIME = client.get_node("ns=2;i=4")
TIME_Value = TIME.get_value()
print(TIME_Value)

time.sleep(1)



Here is the code of the GUI created by first designing the layout with QtDesigner and afterwards changing the file format from *.ui to *.py and making some modifications, like adding the client specification.


from PyQt5 import QtCore, QtGui, QtWidgets
from opcua import Client #added
import time #added

url = "opc.tcp://localhost:4840"

client = Client(url)

client.connect()



class Ui_MainWindow(object):

def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(80, 120, 195, 68))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.label = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.label.setFont(font)
self.label.setObjectName("label")
self.verticalLayout_2.addWidget(self.label)
self.label_2 = QtWidgets.QLabel(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.verticalLayout_2.addWidget(self.label_2)
self.horizontalLayout.addLayout(self.verticalLayout_2)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.lineEdit = QtWidgets.QLineEdit(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.lineEdit.setFont(font)
self.lineEdit.setObjectName("lineEdit")
self.verticalLayout.addWidget(self.lineEdit)
self.lineEdit_2 = QtWidgets.QLineEdit(self.widget)
font = QtGui.QFont()
font.setPointSize(14)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setObjectName("lineEdit_2")
self.verticalLayout.addWidget(self.lineEdit_2)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)



Temp = client.get_node("ns=2;i=2")
Temperature = Temp.get_value()
self.lineEdit.setText(str(Temperature))
self.lineEdit.update()

Press = client.get_node("ns=2;i=3")
Pressure = Press.get_value()
self.lineEdit_2.setText(str(Pressure))
self.lineEdit_2.update()




def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Pressure"))
self.label_2.setText(_translate("MainWindow", "Temperature"))


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())



The result is that the window displays one value of 'Pressure' and 'Temperature' without updating this value when it changes. Which code should I add to make sure this does happen? And what would be a good way to implement a user defined DeltaT (time interval for updating the values that are displayed)?



I have tried to study the opc-client-gui, but as I am not very experienced with Python & PyQt, it is quite hard to extract the information I need. My background is Matlab, statistics, and energy system modeling.



Versions: Python 3.6, PyQt5, Qt 5.9.1





You are mixing two things together -- is your OPC-UA layer works? Yes -- you have the problem with GUI and OPC-UA is irrelevant. No -- you have the problem with OPC-UA and GUI is irrelevant. So the question is, did you do your homework and tested which layer is relevant here? You need to subscribe in OPC-UA for given nodes (for changes).
– astrowalker
2 days ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API