1## Example_digital_output/ main.py
2## This is example for digital output with WPC DAQ Device with synchronous mode.
3## Copyright (c) 2023 WPC Systems Ltd.
4## All rights reserved.
5
6## Python
7import os
8import sys
9
10## Third party
11from PyQt5 import QtWidgets, QtGui
12from PyQt5.QtWidgets import QMessageBox
13from UI_design.Ui_example_GUI_DO import Ui_MainWindow
14
15## WPC
16from wpcsys import pywpc
17
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 ## Initialize parameters
30 self.state_cal = 255
31
32 ## Connection flag
33 self.connect_flag = 0
34
35 ## Handle declaration
36 self.dev = None
37
38 ## Material path
39 file_path = os.path.dirname(__file__)
40 self.trademark_path = file_path + "\Material\\trademark.jpg"
41 self.blue_led_path = file_path + "\Material\LED_blue.png"
42 self.red_led_path = file_path + "\Material\LED_red.png"
43 self.green_led_path = file_path + "\Material\LED_green.png"
44 self.gray_led_path = file_path + "\Material\LED_gray.png"
45 self.switch_blue_path = file_path + "\Material\switch_blue.png"
46 self.switch_gray_path = file_path + "\Material\switch_gray.png"
47
48 ## Convert backward slash to forward slash
49 self.switch_blue_path = self.switch_blue_path.replace('\\', '/')
50 self.switch_gray_path = self.switch_gray_path.replace('\\', '/')
51
52 ## Set trademark & LED path
53 self.ui.lb_trademark.setPixmap(QtGui.QPixmap(self.trademark_path))
54 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.gray_led_path))
55
56 ## Define callback events
57 self.ui.btn_connect.clicked.connect(self.connectEvent)
58 self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
59 for i in range(8):
60 obj_chbox_state = getattr(self.ui, 'checkbox_state%d' %i)
61 obj_chbox_state.stateChanged.connect(self.stateDOEvent)
62 obj_chbox_state.setStyleSheet("QCheckBox::indicator{ width: 60px;height: 60px;} QCheckBox::indicator:unchecked {image: url("+self.switch_gray_path+");} QCheckBox::indicator:checked {image: url("+self.switch_blue_path+");}")
63
64 def selectHandle(self):
65 handle_idx = int(self.ui.comboBox_handle.currentIndex())
66 if handle_idx == 0:
67 self.dev = pywpc.EthanD()
68 elif handle_idx == 1:
69 self.dev = pywpc.USBDAQF1D()
70 elif handle_idx == 2:
71 self.dev = pywpc.USBDAQF1DSNK()
72 elif handle_idx == 3:
73 self.dev = pywpc.USBDAQF1AD()
74 elif handle_idx == 4:
75 self.dev = pywpc.USBDAQF1TD()
76 elif handle_idx == 5:
77 self.dev = pywpc.USBDAQF1RD()
78 elif handle_idx == 6:
79 self.dev = pywpc.USBDAQF1CD()
80 elif handle_idx == 7:
81 self.dev = pywpc.USBDAQF1AOD()
82
83 def updateParam(self):
84 ## Get IP or serial_number from GUI
85 self.ip = self.ui.lineEdit_IP.text()
86
87 ## Get port from GUI
88 self.port = int(self.ui.comboBox_port.currentIndex())
89
90 def stateDOEvent(self):
91 ## Check connection status
92 if self.checkConnectionStatus() == False:
93 return
94
95 ## Initialize parameter
96 self.state_cal = 0
97
98 ## Update Param
99 self.updateParam()
100
101 ## Calculate state
102 for i in range (8):
103 obj_chbox_state = getattr(self.ui, 'checkbox_state%d' % i)
104 state = obj_chbox_state.isChecked()
105 self.state_cal += int(state) << i
106
107 ## Write DO state to MCU
108 self.dev.DO_writePort(self.port, self.state_cal)
109
110 def connectEvent(self):
111 if self.connect_flag == 1:
112 return
113
114 ## Select handle
115 self.selectHandle()
116
117 ## Update Param
118 self.updateParam()
119
120 ## Connect to device
121 try:
122 self.dev.connect(self.ip)
123 except pywpc.Error as err:
124 print("err: " + str(err))
125 return
126
127 ## Change LED status
128 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
129
130 ## Change connection flag
131 self.connect_flag = 1
132
133 ## Open all DO port
134 for i in range(4):
135 status = self.dev.DO_openPort(i)
136 print(f"DO_openPort{i} status: {status}")
137
138 def disconnectEvent(self):
139 if self.connect_flag == 0:
140 return
141
142 ## Close DO port
143 for i in range(4):
144 status = self.dev.DO_closePort(i)
145 print(f"DO_closePort{i} status: {status}")
146
147 ## Disconnect device
148 self.dev.disconnect()
149
150 ## Change LED status
151 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.gray_led_path))
152
153 ## Change connection flag
154 self.connect_flag = 0
155
156 def closeEvent(self, event):
157 if self.dev is not None:
158 ## Disconnect device
159 self.dev.disconnect()
160
161 ## Release device handle
162 self.dev.close()
163
164 def checkConnectionStatus(self):
165 if self.connect_flag == 0:
166 QMessageBox.information(self, "Error Messages", "Please connect server first.", QMessageBox.Ok)
167 return False
168 else:
169 return True
170
171if __name__ == "__main__":
172 app = QtWidgets.QApplication([])
173 WPC_main_ui = MainWindow()
174 WPC_main_ui.show()
175 sys.exit(app.exec_())