python access to spectrum via VISA and performance optimization

20 Views Asked by At

I am working on a Python program. I'm trying to read data from a spectrum continuously once per second using VISA interface (TCPIP connection). I am using the following code for the purpose:

    myFieldFox.write("*CLS")
    
    myFieldFox.write("SENS:SWE:POIN " + str(numPoints))
    myFieldFox.write("SENS:FREQ:START " + str(startFreq))
    myFieldFox.write("SENS:FREQ:STOP " + str(stopFreq))
    
    while True
        myFieldFox.query("INIT:CONT OFF;*OPC?");
        myFieldFox.query("INIT:IMM;*OPC?");
        trace_data=myFieldFox.query("TRACE:DATA?");
        
        now=datetime.now()
        out_file.write("%s, %s, %s", now.strftime("%H:%M:%S"), str(time.time()), trace_data)
        myFieldFox.write("INIT:CONT ON")
        
        time.sleep(1)

When checking the file, I see that each row is written once per ~1.3 seconds and there are some misses in the code. I want to improve it so that it will be once per second.

Any help or suggestions in this matter are welcome.

0

There are 0 best solutions below