1## Example_analog_output/main.py
2## This is example for Analog Output 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_AO 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 fct_list = [self.hrztSlider_cb0, self.hrztSlider_cb1, self.hrztSlider_cb2, self.hrztSlider_cb3,
49 self.hrztSlider_cb4, self.hrztSlider_cb5, self.hrztSlider_cb6, self.hrztSlider_cb7]
50
51 for i in range(8):
52 obj_hrztSlider = getattr(self.ui, 'horizontalSlider%d' %i)
53 obj_hrztSlider.setRange(0,10000)
54 obj_hrztSlider.setSingleStep(1)
55 obj_hrztSlider.valueChanged.connect(fct_list[i])
56
57 self.ui.btn_update.clicked.connect(self.writeAOValue)
58 self.ui.btn_connect.clicked.connect(self.connectEvent)
59 self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
60 self.ui.btn_setall.clicked.connect(self.setLineEditValueEvent)
61
62 def hrztSlider_cb0(self, value):
63 scaledValue = float(value)/DEVIDER
64 self.ui.lineEdit0.setText(str(scaledValue))
65
66 def hrztSlider_cb1(self, value):
67 scaledValue = float(value)/DEVIDER
68 self.ui.lineEdit1.setText(str(scaledValue))
69
70 def hrztSlider_cb2(self, value):
71 scaledValue = float(value)/DEVIDER
72 self.ui.lineEdit2.setText(str(scaledValue))
73
74 def hrztSlider_cb3(self, value):
75 scaledValue = float(value)/DEVIDER
76 self.ui.lineEdit3.setText(str(scaledValue))
77
78 def hrztSlider_cb4(self, value):
79 scaledValue = float(value)/DEVIDER
80 self.ui.lineEdit4.setText(str(scaledValue))
81
82 def hrztSlider_cb5(self, value):
83 scaledValue = float(value)/DEVIDER
84 self.ui.lineEdit5.setText(str(scaledValue))
85
86 def hrztSlider_cb6(self, value):
87 scaledValue = float(value)/DEVIDER
88 self.ui.lineEdit6.setText(str(scaledValue))
89
90 def hrztSlider_cb7(self, value):
91 scaledValue = float(value)/DEVIDER
92 self.ui.lineEdit7.setText(str(scaledValue))
93
94 def selectHandle(self):
95 handle_idx = int(self.ui.comboBox_handle.currentIndex())
96 if handle_idx == 0:
97 self.dev = pywpc.USBDAQF1AOD()
98
99 def updateParam(self):
100 ## Get IP or serial_number from GUI
101 self.ip = self.ui.lineEdit_IP.text()
102
103 ## Get port from GUI
104 self.port = int(self.ui.comboBox_port.currentIndex())
105
106 def connectEvent(self):
107 if self.connect_flag == 1:
108 return
109
110 ## Select handle
111 self.selectHandle()
112
113 ## Update Param
114 self.updateParam()
115
116 ## Connect to device
117 try:
118 self.dev.connect(self.ip)
119 except pywpc.Error as err:
120 print("err: " + str(err))
121 return
122
123 ## Change LED status
124 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
125
126 ## Change connection flag
127 self.connect_flag = 1
128
129 ## Open AO port
130 status = self.dev.AO_open(self.port)
131 print("AO_open status: ", status)
132
133 def disconnectEvent(self):
134 if self.connect_flag == 0:
135 return
136
137 ## Close AO port
138 status = self.dev.AO_close(self.port)
139 print("AO_close status: ", status)
140
141 ## Disconnect device
142 self.dev.disconnect()
143
144 ## Change LED status
145 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.green_led_path))
146
147 ## Change connection flag
148 self.connect_flag = 0
149
150 def closeEvent(self, event):
151 if self.dev is not None:
152 ## Disconnect device
153 self.dev.disconnect()
154
155 ## Release device handle
156 self.dev.close()
157
158 def writeAOValue(self):
159 voltage_list = []
160 for i in range(8):
161 obj_lineEdit = getattr(self.ui, 'lineEdit%d' %i)
162 voltage_list.append(float(obj_lineEdit.text()))
163
164 ## Set AO port to 0 and write data simultaneously
165 status = self.dev.AO_writeAllChannels(self.port, voltage_list)
166 print("AO_writeAllChannels_async status: ", status)
167
168 def setLineEditValueEvent(self):
169 voltage = self.ui.lineEdit_setall.text()
170 for i in range(8):
171 obj_lineEdit = getattr(self.ui, 'lineEdit%d' %i)
172 obj_lineEdit.setText(voltage)
173
174if __name__ == "__main__":
175 app = QtWidgets.QApplication([])
176 WPC_main_ui = MainWindow()
177 WPC_main_ui.show()
178 sys.exit(app.exec_())