import pyvisa as visa
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication, QPushButton, QLineEdit, QTextEdit


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.rm = visa.ResourceManager()

    def initUI(self):
        self.setGeometry(300, 300, 660, 400)
        self.setWindowTitle('DemoPython')

        lbl1 = QLabel('Instrument Address', self)
        lbl1.move(22, 22)
        self.textBox_address = QLineEdit(
            "USB0::0x049F::0x605E::CN99999998::0::INSTR", self)
        self.textBox_address.setMinimumWidth(362)
        self.textBox_address.height = 23
        self.textBox_address.move(162, 19)
        self.btn_connect = QPushButton("connect", self)
        self.btn_connect.move(548, 19)

        lbl2 = QLabel('Command', self)
        lbl2.move(22, 88)
        self.textBox_command = QLineEdit('read?', self)
        self.textBox_command.setMinimumWidth(362)
        self.textBox_command.height = 23
        self.textBox_command.move(162, 85)

        self.btn_send = QPushButton("Send Command", self)
        self.btn_send.move(171, 138)
        self.btn_read = QPushButton("Read Response", self)
        self.btn_read.move(302, 138)
        self.btn_sendAndRead = QPushButton("Send & Read", self)
        self.btn_sendAndRead.move(432, 138)

        self.logEdit = QTextEdit('', self)
        self.logEdit.isReadOnly = True
        self.logEdit.setMinimumWidth(600)
        self.logEdit.setMaximumHeight(142)
        self.logEdit.move(22, 214)

        self.btn_connect.clicked.connect(self.btn_connect_click)
        self.btn_send.clicked.connect(self.btn_send_click)
        self.btn_read.clicked.connect(self.btn_read_click)
        self.btn_sendAndRead.clicked.connect(self.btn_sendAndRead_click)
        self.show()

    def btn_connect_click(self):
        address = self.textBox_address.text()
        if address != '':
            self.inst = self.rm.open_resource(address)
        else:
            addressList = self.rm.list_resources()
            for _address in addressList:
                _inst = self.rm.open_resource(_address)
                if _inst != None:
                    _inst.visalib.write(_inst.session, '*IDN?' + '\n')
                    readData = _inst.visalib.read(_inst.session, 100)
                    readValue = str(readData[0])
                    if readValue.__contains__('HDM'):
                        self.inst = _inst
                        self.textBox_address.setText(_address)
                        return

    def btn_send_click(self):
        cmdStr = self.textBox_command.text() + '\n'
        self.inst.visalib.write(self.inst.session, cmdStr)

    def btn_read_click(self):
        readData = self.inst.visalib.read(self.inst.session, 100)
        readValue = str(readData[0])
        self.logEdit.append(readValue)

    def btn_sendAndRead_click(self):
        cmdStr = self.textBox_command.text() + '\n'
        self.inst.visalib.write(self.inst.session, cmdStr)
        readValue = self.inst.visalib.read(self.inst.session, 100)
        # print(readValue)
        self.logEdit.append(str(readValue[0]))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())