import pyvisa
import numpy as np
import matplotlib.pyplot as plt
import time
import math

def main():
    print("Starting")

    avgs = [0]

    rm = pyvisa.ResourceManager()
    print(rm.list_resources())
    VOLT = "GPIB0::4::INSTR"
    AMP = "GPIB::2::INSTR"

    volt = rm.open_resource(VOLT)
    amp = rm.open_resource(AMP)

    volt.write("*RST")
    amp.write("*RST")

    volt.write("CONF:VOLT:DC")
    volt.write("INP:IMP:AUTO ON")
    amp.write("CONF:CURR:DC")

    while True:
        v = float(volt.query("READ?"))
        i = float(amp.query("READ?"))

        r = v/i
        avgs.append(r)
        print(avgs)
        if len(avgs) > 10:
            del avgs[0]

        print(f"V:{v} I:{i}")
        print(f"R:{r}")
        print(f"avg {np.average(avgs)}")


    

if __name__ == "__main__":
    main()

#  !uv run kok.py
