write a code to control keithley GPIB0::1::INSTR with pyqt GUI, i have a voltage list…
- User
write a code to control keithley GPIB0::1::INSTR with pyqt GUI, i have a voltage list 10,1,9 and duration list 2,1,2. it means source 10 volt and stay for 2 second by SOUR:DEL command then change to 1 volt and stay for 1 second and so on. I need a GUI to type voltage and duration list as input, and I need to record the voltage and current value with real time every second in an excel file
- Assistant
To create a PyQt GUI to control a Keithley instrument via GPIB and log voltage and current values to an Excel file with real-time updates, you'll need to:
- Create a GUI for user input.
- Implement the GPIB communication.
- Schedule voltage changes and logging intervals.
- Write data to an Excel file.
Here's an example script that outlines how you could accomplish this using
pyqt5,pyvisafor GPIB communication, andopenpyxlfor Excel operations. Please note that you might need to install the required packages (PyQt5,pyvisa,pyvisa-py,openpyxl) if you haven't done so yet.import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QLabel from PyQt5.QtCore import QTimer import pyvisa from openpyxl import Workbook from datetime import datetime # Keithley class to handle GPIB communication and operations class KeithleyController: def __init__(self): self.rm = pyvisa.ResourceManager() self.instr = self.rm.open_resource('GPIB0::1::INSTR') self.instr.write('*RST') def set_voltage(self, voltage): self.instr.write(f'SOUR:VOLT {voltage}') def read_current(self): return self.instr.query('READ?').strip() def close(self): self.instr.close() # GUI class class KeithleyGUI(QWidget): def __init__(self): super().__init__() self.init_ui() self.keithley = KeithleyController() self.timer = QTimer() self.workbook = Workbook() self.worksheet = self.workbook.active self.worksheet.append(["Time", "Voltage", "Current"]) self.start_time = None self.voltage_list = [] self.duration_list = [] self.current_index = 0 self.logging = False def init_ui(self): self.setWindowTitle('Keithley Controller') layout = QVBoxLayout() # Input for Voltages self.voltage_input = QLineEdit(self) self.voltage_input.setPlaceholderText("Enter voltages (comma-separated)") layout.addWidget(QLabel('Voltages:')) layout.addWidget(self.voltage_input) # Input for Durations self.duration_input = QLineEdit(self) self.duration_input.setPlaceholderText("Enter durations (comma-separated)") layout.addWidget(QLabel('Durations:')) layout.addWidget(self.duration_input) # Start Button self.start_button = QPushButton('Start', self) self.start_button.clicked.connect(self.start_sequence) layout.addWidget(self.start_button) # Set the layout for the QWidget self.setLayout(layout) def start_sequence(self): # Parse input strings into lists of voltages and durations self.voltage_list = [float(v.strip()) for v in self.voltage_input.text().split(',')] self.duration_list = [float(d.strip()) for d in self.duration_input.text().split(',')] # Start the process self.timer.timeout.connect(self.update_voltage) self.timer.start(1000) # 1000ms = 1 second self.start_time = datetime.now() self.logging = True self.current_index = 0 self.apply_voltage() self.start_button.setEnabled(False) # Disable the start button def update_voltage(self): if self.logging: current_time = datetime.now() elapsed_time = (current_time - self.start_time).total_seconds() # Log the current and voltage every second try: current = self.keithley.read_current() voltage = self.voltage_list[self.current_index] self.worksheet.append([current_time.strftime("%H:%M:%S"), voltage, current]) except Exception as e: print(f"An error occurred while reading from the Keithley: {e}") # Check if it's time to change the voltage if elapsed_time >= sum(self.duration_list[:self.current_index + 1]): self.current_index += 1 if self.current_index >= len(self.voltage_list): # We're done with all the voltages, stop the timer and save the workbook self.logging = False self.timer.stop() self.workbook.save("Keithley_Log.xlsx") self.keithley.close() self.start_button.setEnabled(True) # Re-enable the start button else: self.apply_voltage() def apply_voltage(self): try: voltage = self.voltage_list[self.current_index] self.keithley.set_voltage(voltage) # Here you could apply the delay if required # self.keithley.instr.write(f'SOUR:DEL {self.duration_list[self.current_index]}') except Exception as e: print(f"An error occurred while setting the voltage: {e}") # Main function to start the GUI application if __name__ == '__main__': app = QApplication(sys.argv) keithley_gui = KeithleyGUI() keithley_gui.show() sys.exit(app.exec_())In this script:
KeithleyControllerhandles communication with the Keithley instrument.KeithleyGUIprovides a simple interface for the user to enter voltage and duration lists.- Once the 'Start' button is clicked, a timer is started to log the current and voltage values every second.
- The sequence of voltages is applied based on the user-provided list and corresponding durations.
PyVISA requires the installation of a backend to communicate with the instruments.
pyvisa-pyis a pure-Python implementation of this backend.Please replace the GPIB address (
'GPIB0::1::INSTR') and the commands ('SOUR:VOLT','READ?') accordingly to match the specific SCPI (Standard Commands for Programmable Instruments) commands for your Keithley model.This code assumes that your Keithley device operates as expected with the commands provided. Make sure to test each command individually with your device outside of the GUI to ensure proper operation. Additionally, you may need to modify the code to handle errors and edge cases according to your project's requirements.