import vxi11
import time
import numpy as np
import datetime
import matplotlib.pyplot as plt

instr = vxi11.Instrument("192.168.10.53")

print(instr.ask("*IDN?"))

# voltage scale for scope channels
#CH4_scale = '500E-3'
#CH4_scale = '2E0'
#CH3_scale = '200E-3'
#CH3_scale = '1E0'

######

#%%

N_points = 15625

mdepth = np.int(instr.ask(":ACQuire:MDEPth?"))

print('Memory depth: ', mdepth)

#%%
N_win = np.int(np.ceil(mdepth / N_points))

print('Number of windows to acquire: ', N_win)

#%%

ch = 1

instr.write(":WAVeform:SOURce CHAN{}".format(ch))
print(instr.ask(":WAVeform:SOURce?"))

#time.sleep(1)

instr.write("WAVeform:FORMat ASCii")

instr.write("WAVeform:MODE RAW")

V = np.array([], dtype=np.float64)

for i in range(N_win):
    
    print('Window ', (i+1))
    
    start_idx = (i*N_points+1)
    stop_idx = (i+1)*N_points
    
    if(stop_idx > mdepth):
        stop_idx = mdepth
    
    instr.write("WAVeform:STAR {}".format(start_idx))
    instr.write("WAVeform:STOP {}".format(stop_idx))
     
    data_ascii = instr.ask(":WAVeform:DATA?")
    data_ascii = data_ascii[11:]
    
    #print(data_ascii)

    V = np.append(V, np.array(data_ascii.split(','), dtype=np.float64) )

print(V)

plt.figure()

plt.plot(V)

#%%

fs = np.float64(instr.ask(":ACQuire:SRATe?"))

#fs = 50e3

#file_out = '2021-05-07-scope-17'

#np.savez(file_out, V=V, fs = fs)

#%%

instr.close()