Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

TTR500 TDR Measurement

Programming examples for remote communication with instruments
C/C++, C#, Python, Matlab, LabVIEW
Post Reply
profanum429
Posts: 1
Joined: September 19th, 2020, 1:10 pm
Country: United States

TTR500 TDR Measurement

Post by profanum429 » September 19th, 2020, 6:58 pm

We've got a TTR506A and I'm really enjoying it but I was a little sad to see that TDR in VectorVu had been discontinued so I figured I'd try my hand at writing some Python code to interface to the VNA and pull the data and transfer it over to the VNWA software for some basic TDR work (since I've used that quite a bit with a VNWA3E at home).

It's my first go around with talking to test equipment using VISA and also about my second time using Python so it's pretty rough but it seems to work pretty well for my purposes. I'm getting the TTR506A data and transfering it over to the VNWA software and letting it do the TDR calculations in pretty much realtime compared to the normal sweeping speed of the TTR without doing this.

Here's a copy of the code, figured it might be interesting for other folks if you're looking to do some basic stuff with the TTR and want to it in real time as opposed to post-processing with touchstone files.

Code: Select all

import pyvisa
import time
import socketserver
import threading
import queue

# Data queue for passing data between socket server threads
data_q = queue.Queue()

# Defaults for a TTR506A
start_freq = "300000"
stop_freq = "6000000000"
points = "201"

# Setup state
setup = 1

# Handle Command Messages
class RCHandler(socketserver.BaseRequestHandler):

	# Tell the TTR to do the sweep
    def do_sweep(self):
        print('Do the sweep -> {0} to {1} with {2} points'.format(start_freq, stop_freq, points))

        rm = pyvisa.ResourceManager()
        vna = rm.open_resource('GPIB8::1::INSTR',query_delay=0, timeout=5000000)
        ready = vna.query('*OPC?')

        # Only do setup if necessary
        global setup
        if(setup == 1):
            vna.write(':TRIG:SEQ:SOUR MAN');
            vna.write(':SENSE:FREQUENCY:START {0}'.format(start_freq))
            vna.write(':SENSE:FREQUENCY:STOP {0}'.format(stop_freq))
            vna.write(':SENSE:SWE:POIN {0}'.format(points))
            setup = 0

        # Do the sweep
        vna.write(':INIT:IMM;')
        vna.write(':TRIG:SEQ:SING;')
        ready=vna.query('*OPC?')

        # Save the data
        textdata =vna.query('CALC1:DATA:SDAT?')
        values=textdata.split(',')

        # Tell the VNWA software we're sending a sweep
        data_q.put(bytes('sweep_start {0} {1} {2} 1\00'.format(points, start_freq, stop_freq), 'utf-8'))

        step_freq = (int(stop_freq) - int(start_freq)) / points

        # Send over all the data points -> Point #, Frequency, S11 RE, S11 IM
        for idx in range(len(values) //2):
            data_string = 'data {0} {1} {2} {3} 0 0 0 0 0 0\00'.format(idx, idx*step_freq, values[2*idx], values[2*idx+1])
            data_q.put(bytes(data_string, 'utf-8'))

        # Finished
        data_q.put(bytes('sweep_complete\00', 'utf-8'))

        return

    def handle(self):
    	# Always running
        while 1:
            self.data = self.request.recv(1024).strip().decode('utf-8')
            print('In Data -> {0}'.format(self.data))

            # Respond correctly
            if('hardware?' in self.data):
                print('Init found -> Send reply')
                reply = "- 7 VNWA3E\00"
                self.request.sendall(bytes(reply, 'utf-8'))

            # Fill out requested values
            if('setgrid' in self.data):
                args = self.data.split()

                global setup
                global start_freq 
                global stop_freq
                global points

                if(start_freq != args[2]):
                    setup = 1
                start_freq = args[2]

                if(stop_freq != args[3]):
                    setup = 1
                stop_freq = args[3]

                if(points != int(args[4].rstrip('\x00'))):
                    setup = 1
                points = int(args[4].rstrip('\x00'))

                self.request.sendall(bytes('Error Code 0\00', 'utf-8'))

            # Ignore this message but reply to speed things up
            if('timeperpoint' in self.data):
                self.request.sendall(bytes('Error Code 0\00', 'utf-8'))

            # Do the actual sweep now
            if('sweep' in self.data):
                self.request.sendall(bytes('Error Code 0\00', 'utf-8'))
                self.do_sweep()

            if(self.data == ''):
                break
        return

# This handles the data connection server
class DataHandler(socketserver.BaseRequestHandler):
    def handle(self):
        # Always run this
        while 1:
#            self.data = self.request.recv(1024).strip().decode('utf-8')
#            if(self.data == ''):
#                break
			
			# Send the next message in the queue
            try:
                self.request.sendall(data_q.get())
            except:
                print('exception when sending data')
                break;
        return

if __name__ == '__main__':
	# Start our command server socket
    address_rc = ('0.0.0.0', 55555)
    server_rc = socketserver.TCPServer(address_rc, RCHandler)

    # Run it as a daemon
    t_rc = threading.Thread(target=server_rc.serve_forever)
    t_rc.setDaemon(True)
    t_rc.start()

    print('Serving RC...')

    # Start the data server socket
    address_data = ('0.0.0.0', 55556)
    server_data = socketserver.TCPServer(address_data, DataHandler)

    # And run it as a daemon too
    t_data = threading.Thread(target=server_data.serve_forever)
    t_data.setDaemon(True)
    t_data.start()

    print('Serving Data...')

    # Never exit
    while True:
        time.sleep(1)

Post Reply

Return to “Remote Instrument Communication (Programming) Examples”

Who is online

Users browsing this forum: No registered users and 1 guest