1## Example_Thermo/ main.py
2## This is example for thermocouple with WPC DAQ Device with synchronous mode.
3## Copyright (c) 2023 WPC Systems Ltd.
4## All rights reserved.
5
6## Python
7import sys
8import os
9
10## Third party
11from PyQt5 import QtWidgets, QtGui
12from UI_design.Ui_example_GUI_thermocouple import Ui_MainWindow
13
14## WPC
15from wpcsys import pywpc
16
17DEVIDER = 2000
18class MainWindow(QtWidgets.QMainWindow):
19 def __init__(self):
20 super(MainWindow, self).__init__()
21
22 ## UI initialize
23 self.ui = Ui_MainWindow()
24 self.ui.setupUi(self)
25
26 ## Get Python driver version
27 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
28
29 ## Connection flag
30 self.connect_flag = 0
31
32 ## Handle declaration
33 self.dev = None
34
35 ## Material path
36 file_path = os.path.dirname(__file__)
37 self.trademark_path = file_path + "\Material\\trademark.jpg"
38 self.blue_led_path = file_path + "\Material\LED_blue.png"
39 self.red_led_path = file_path + "\Material\LED_red.png"
40 self.green_led_path = file_path + "\Material\LED_green.png"
41 self.gray_led_path = file_path + "\Material\LED_gray.png"
42
43 ## Set trademark & LED path
44 self.ui.lb_trademark.setPixmap(QtGui.QPixmap(self.trademark_path))
45 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.gray_led_path))
46
47 ## Define callback events
48 self.ui.btn_connect.clicked.connect(self.connectEvent)
49 self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
50
51 self.ui.btn_set.clicked.connect(self.setEvent)
52 self.ui.btn_temp.clicked.connect(self.tempEvent)
53
54 def selectHandle(self):
55 handle_idx = int(self.ui.comboBox_handle.currentIndex())
56 if handle_idx == 0:
57 self.dev = pywpc.USBDAQF1TD()
58
59 def updateParam(self):
60 ## Get IP or serial_number from GUI
61 self.ip = self.ui.lineEdit_IP.text()
62
63 ## Get port from GUI
64 self.port = int(self.ui.comboBox_port.currentIndex())+1
65
66 ## Get type from GUI
67 self.type = self.ui.comboBox_type.currentIndex()
68
69 ## Get oversampling from GUI
70 self.oversampling = self.ui.comboBox_oversampling.currentIndex()
71
72 ## Get noiserejection from GUI
73 self.noiserejection = self.ui.comboBox_noiserejection.currentIndex()
74
75 def tempEvent(self):
76 ## Update Param
77 self.updateParam()
78
79 ## Read sensor in Channel 0
80 data = self.dev.Thermal_readSensor(self.port, 0)
81 print("Read channel 0 data:", data, "°C")
82
83 ## Update in GUI
84 self.ui.lineEdit_sensor0.setText(str(data))
85
86 ## Read sensor in Channel 1
87 data = self.dev.Thermal_readSensor(self.port, 1)
88 print("Read channel 1 data:", data, "°C")
89
90 ## Update in GUI
91 self.ui.lineEdit_sensor1.setText(str(data))
92
93 def setEvent(self):
94 ## Update Param
95 self.updateParam()
96
97 ## Set thermo port and type
98 for i in range(2):
99 status = self.dev.Thermal_setType(self.port, i, self.type)
100 print("Thermal_setType status: ", status)
101
102 ## Set thermo port and over-sampling mode
103 for i in range(2):
104 status = self.dev.Thermal_setOverSampling(self.port, i, self.oversampling)
105 print("Thermal_setOverSampling status: ", status)
106
107 def connectEvent(self):
108 if self.connect_flag == 1:
109 return
110
111 ## Select handle
112 self.selectHandle()
113
114 ## Update Param
115 self.updateParam()
116
117 ## Connect to device
118 try:
119 self.dev.connect(self.ip)
120 except pywpc.Error as err:
121 print("err: " + str(err))
122 return
123
124 ## Change LED status
125 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
126
127 ## Change connection flag
128 self.connect_flag = 1
129
130 ## Open thermo port
131 status = self.dev.Thermal_open(self.port)
132 print("Thermal_open status: ", status)
133
134 def disconnectEvent(self):
135 if self.connect_flag == 0:
136 return
137
138 ## Update Param
139 self.updateParam()
140
141 ## Close thermo port
142 status = self.dev.Thermal_close(self.port)
143 print("Thermal_close status: ", status)
144
145 ## Disconnect device
146 self.dev.disconnect()
147
148 ## Change LED status
149 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.green_led_path))
150
151 ## Change connection flag
152 self.connect_flag = 0
153
154 def closeEvent(self, event):
155 if self.dev is not None:
156 ## Disconnect device
157 self.dev.disconnect()
158
159 ## Release device handle
160 self.dev.close()
161
162if __name__ == "__main__":
163 app = QtWidgets.QApplication([])
164 WPC_main_ui = MainWindow()
165 WPC_main_ui.show()
166 sys.exit(app.exec_())