1## Example_UART/ main.py
2## This is example for UART 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_UART 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 self.ui.lb_ledport.setPixmap(QtGui.QPixmap(self.gray_led_path))
47
48 ## Define callback events
49 self.ui.btn_connect.clicked.connect(self.connectEvent)
50 self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
51
52 self.ui.btn_open.clicked.connect(self.openPortEvent)
53 self.ui.btn_close.clicked.connect(self.closePortEvent)
54
55 self.ui.btn_write.clicked.connect(self.writeEvent)
56 self.ui.btn_read.clicked.connect(self.readEvent)
57
58 def selectHandle(self):
59 handle_idx = int(self.ui.comboBox_handle.currentIndex())
60 if handle_idx == 0:
61 self.dev = pywpc.USBDAQF1D()
62 elif handle_idx == 1:
63 self.dev = pywpc.USBDAQF1AD()
64 elif handle_idx == 2:
65 self.dev = pywpc.USBDAQF1TD()
66 elif handle_idx == 3:
67 self.dev = pywpc.USBDAQF1RD()
68 elif handle_idx == 4:
69 self.dev = pywpc.USBDAQF1CD()
70 elif handle_idx == 5:
71 self.dev = pywpc.USBDAQF1AOD()
72
73 def updateParam(self):
74 ## Get IP or serial_number from GUI
75 self.ip = self.ui.lineEdit_IP.text()
76
77 ## Get port from GUI
78 self.port = int(self.ui.comboBox_port.currentIndex())+1
79
80 # Get write data from GUI
81 self.write_data = self.ui.lineEdit_write.text()
82
83 ## Get bytes to read from GUI
84 self.read_bytes = self.ui.lineEdit_byteread.text()
85
86 ## Get baudrate from GUI
87 self.baudrate = self.ui.comboBox_baudrate.currentText()
88
89 ## Get databit from GUI
90 self.databit = self.ui.comboBox_databit.currentIndex()
91
92 ## Get parity from GUI
93 self.parity = self.ui.comboBox_parity.currentIndex()
94
95 ## Get stop bit from GUI
96 self.stopbit = self.ui.comboBox_stopbit.currentIndex()
97
98 def openPortEvent(self):
99 ## Update Param
100 self.updateParam()
101
102 ## Open UART port
103 status = self.dev.UART_open(self.port)
104 print("UART_open status: ", status)
105
106 ## Set UART port and baudrate
107 status = self.dev.UART_setBaudRate(self.port, int(self.baudrate))
108 print("UART_setBaudRate status: ", status)
109
110 ## Set UART port and data bit
111 status = self.dev.UART_setDataBit(self.port, self.databit)
112 print("UART_setDataBit status: ", status)
113
114 ## Set UART port and parity
115 status = self.dev.UART_setParity(self.port, self.parity)
116 print("UART_setParity status: ", status)
117
118 ## Set UART port and stop bit
119 status = self.dev.UART_setNumStopBit(self.port, self.stopbit)
120 print("UART_setNumStopBit status: ", status)
121
122 ## Change port LED status
123 self.ui.lb_ledport.setPixmap(QtGui.QPixmap(self.blue_led_path))
124
125 def writeEvent(self):
126 ## Update Param
127 self.updateParam()
128
129 ## Set UART port and and write data to device
130 status = self.dev.UART_write(self.port, self.write_data)
131 print("UART_write status: ", status)
132
133 def readEvent(self):
134 ## Update Param
135 self.updateParam()
136
137 ## Set UART port and read bytes
138 data = self.dev.UART_read(self.port, int(self.read_bytes))
139 self.ui.lineEdit_read.setText(str(data))
140
141 def closePortEvent(self):
142 ## Update Param
143 self.updateParam()
144
145 ## Close UART port
146 status = self.dev.UART_close(self.port)
147 print("UART_close status: ", status)
148
149 ## Change port LED status
150 self.ui.lb_ledport.setPixmap(QtGui.QPixmap(self.green_led_path))
151
152 def connectEvent(self):
153 if self.connect_flag == 1:
154 return
155
156 ## Select handle
157 self.selectHandle()
158
159 ## Update Param
160 self.updateParam()
161
162 ## Connect to device
163 try:
164 self.dev.connect(self.ip)
165 except pywpc.Error as err:
166 print("err: " + str(err))
167 return
168
169 ## Change LED status
170 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
171
172 ## Change connection flag
173 self.connect_flag = 1
174
175 def disconnectEvent(self):
176 if self.connect_flag == 0:
177 return
178
179 ## Disconnect device
180 self.dev.disconnect()
181
182 ## Change LED status
183 self.ui.lb_led.setPixmap(QtGui.QPixmap(self.green_led_path))
184
185 ## Change connection flag
186 self.connect_flag = 0
187
188 def closeEvent(self, event):
189 if self.dev is not None:
190 ## Disconnect device
191 self.dev.disconnect()
192
193 ## Release device handle
194 self.dev.close()
195
196if __name__ == "__main__":
197 app = QtWidgets.QApplication([])
198 WPC_main_ui = MainWindow()
199 WPC_main_ui.show()
200 sys.exit(app.exec_())