I2C test panel

I2C_gui.PNG

  1##  Example_I2C/ main.py
  2##  This is example for I2C 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_I2C import Ui_MainWindow
 13
 14## WPC
 15from wpcsys import pywpc
 16
 17class MainWindow(QtWidgets.QMainWindow):
 18    def __init__(self):
 19        super(MainWindow, self).__init__()
 20
 21        ## UI initialize
 22        self.ui = Ui_MainWindow()
 23        self.ui.setupUi(self)
 24
 25        ## Get Python driver version
 26        print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 27
 28        ## Connection flag
 29        self.connect_flag = 0
 30
 31        ## Handle declaration
 32        self.dev = None
 33
 34        ## Material path
 35        file_path = os.path.dirname(__file__)
 36        self.trademark_path = file_path + "\Material\\trademark.jpg"
 37        self.blue_led_path = file_path + "\Material\LED_blue.png"
 38        self.red_led_path = file_path + "\Material\LED_red.png"
 39        self.green_led_path = file_path + "\Material\LED_green.png"
 40        self.gray_led_path = file_path + "\Material\LED_gray.png"
 41
 42        ## Set trademark & LED path
 43        self.ui.lb_trademark.setPixmap(QtGui.QPixmap(self.trademark_path))
 44        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.gray_led_path))
 45
 46        ## Define callback events
 47        self.ui.btn_connect.clicked.connect(self.connectEvent)
 48        self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
 49        self.ui.btn_write.clicked.connect(self.writeEvent)
 50        self.ui.btn_read.clicked.connect(self.readEvent)
 51        self.ui.btn_set.clicked.connect(self.setClockEvent)
 52
 53    def selectHandle(self):
 54        handle_idx = int(self.ui.comboBox_handle.currentIndex())
 55        if handle_idx == 0:
 56            self.dev = pywpc.USBDAQF1D()
 57        elif handle_idx == 1:
 58            self.dev = pywpc.USBDAQF1AD()
 59        elif handle_idx == 2:
 60            self.dev = pywpc.USBDAQF1TD()
 61        elif handle_idx == 3:
 62            self.dev = pywpc.USBDAQF1RD()
 63        elif handle_idx == 4:
 64            self.dev = pywpc.USBDAQF1CD()
 65        elif handle_idx == 5:
 66            self.dev = pywpc.USBDAQF1AOD()
 67
 68    def updateParam(self):
 69        ## Get IP or serial_number from GUI
 70        self.ip = self.ui.lineEdit_IP.text()
 71
 72        ## Get port from GUI
 73        self.port = int(self.ui.comboBox_port.currentIndex())+1
 74
 75        ## Get clock rate from GUI
 76        self.clock_mode = self.ui.comboBox_clockrate.currentIndex()
 77
 78        ## Get write address (Hex) from GUI
 79        self.write_addr = int(self.ui.lineEdit_writeAddr.text(), 16)
 80
 81        ## Get write (Hex) from GUI
 82        data = self.ui.lineEdit_write.text()
 83
 84        ## Convert string to int list
 85        self.write_data = self.converStrtoIntList(data)
 86
 87        ## Get byte to read from GUI
 88        self.byte_read = int(self.ui.lineEdit_byteread.text())
 89
 90        ## Get read address (Hex) from GUI
 91        self.read_addr = int(self.ui.lineEdit_readAddr.text(), 16)
 92
 93    def setClockEvent(self):
 94        ## Update Param
 95        self.updateParam()
 96
 97        ## Set I2C port and clock rate
 98        status = self.dev.I2C_setClockRate(self.port, self.clock_mode)
 99        print("I2C_setClockRate status: ", status)
100
101    def writeEvent(self):
102        ## Update Param
103        self.updateParam()
104
105        ## Set I2C port and write bytes
106        status = self.dev.I2C_write(self.port, self.write_addr, self.write_data)
107        print("I2C_write status: ", status)
108
109    def readEvent(self):
110        ## Update Param
111        self.updateParam()
112
113        ## Set I2C port and read bytes
114        data = self.dev.I2C_read(self.port, self.read_addr, self.byte_read)
115        self.ui.lineEdit_read.setText(str(data))
116
117    def connectEvent(self):
118        if self.connect_flag == 1:
119            return
120
121        ## Select handle
122        self.selectHandle()
123
124        ## Update Param
125        self.updateParam()
126
127        ## Connect to device
128        try:
129            self.dev.connect(self.ip)
130        except pywpc.Error as err:
131            print("err: " + str(err))
132            return
133
134        ## Change LED status
135        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
136
137        ## Change connection flag
138        self.connect_flag = 1
139
140        ## Open I2C port
141        status = self.dev.I2C_open(self.port)
142        print("I2C_open status: ", status)
143
144    def disconnectEvent(self):
145        if self.connect_flag == 0:
146            return
147
148        ## Close I2C port
149        status = self.dev.I2C_close(self.port)
150        print("I2C_close status: ", status)
151
152        ## Disconnect device
153        self.dev.disconnect()
154
155        ## Change LED status
156        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.green_led_path))
157
158        ## Change connection flag
159        self.connect_flag = 0
160
161    def closeEvent(self, event):
162        if self.dev is not None:
163            ## Disconnect device
164            self.dev.disconnect()
165
166            ## Release device handle
167            self.dev.close()
168
169    def converStrtoIntList(self, str_):
170        ## Split string by commas
171        write_data_strlist = str_.replace(' ','').split(',')
172
173        ## Convert string list to int list
174        write_data_int = []
175        for item in write_data_strlist:
176            write_data_int.append(int(item, 16))
177        return write_data_int
178
179if __name__ == "__main__":
180    app = QtWidgets.QApplication([])
181    WPC_main_ui = MainWindow()
182    WPC_main_ui.show()
183    sys.exit(app.exec_())