• Buffer reading of Keithley 2182A nanovoltmeter with python and pyvisa

    My full code is below for clarity. When sending TRAC:DATA? to the 2182A I get the number of elements back I set as TRAC:POIN. But only the first values are different and after that the last different value repeats until the number of points is reached. I am wondering why it is doing this.
    Furthermore with my configuration of 0.1 PLC (and 50 Hz) -> minimum 2ms between measurements, 200 measurements, waiting 1 sec before reading the buffer, I am expecting 50 measurements but get 80. 
    With PLC at 0.01, 200 measurements, 1 sec before reading the buffer, I get 20 measurements (and repetitions after that), although I am expecting more than with 0.1 PLC.
    The measurement seems to be rather slow and the correlation between configuration and retrned measurements is strange. 

    Could someone please help me with making my code faster and more reliable? It might be better to only read the buffer when it is full but I haven't found the commands for that. So please help me with that as well.

    Thank you very much in advance.


    # import libraries
    from subprocess import Popen
    import time
    import numpy as np
    import matplotlib.pyplot as plt
    from datetime import datetime
    import pyvisa
    from collections import Counter

    # set parameters:
    resistance = 48 #Ohm
    max_current_ampl = float(5e-3)

    min_range = resistance*max_current_ampl
    act_range = min_range + min_range*0.05

    # initialize the resource manager
    rm = pyvisa.ResourceManager()

    def nanovoltmeter_com():
        try:
            time.sleep(6)
            nanovolt_start_time = time.time_ns()
            k218 = rm.open_resource('GPIB0::7::INSTR')   # k218 = Keithley 2182A = 'GPIB0::7::INSTR'
            k218.read_termination = '\n'
            k218.write_termination = '\n'
            k218.timeout = 20000    # 7 sec
            print(f"Instrument name: {k218.query('*IDN?')}")    # prints instrument name
            # set range
            nanovolt_opened = time.time_ns()
            k218.write('SENSe:VOLTage:CHANnel1:RANGe:AUTO OFF')
            k218.write(f'SENSe:VOLTage:CHANnel1:RANGe:UPPer {act_range}')
            # set plc
            k218.write('SENSe:VOLTage:NPLCycles 0.1') #can be 0.01 to 50

            time.sleep(2)

            k218.write('TRAC:POIN 200')
            k218.write('TRAC:FEED SENS')
            k218.write('TRAC:FEED:CONT NEXT')
            time.sleep(1)
            data = k218.query('TRAC:DATA?')
            print(data)
         

        except pyvisa.errors.VisaIOError as e:
            print(f"An error occured: {e}")


    if __name__ == "__main__":
        nanovoltmeter_com()