To control AFR remotely, it is required to set the settings as specified in Settings section.
This plug-in demonstrates AFR remote control using the Python 3 programming language.
import socket import sys
def runQuery(client, command): client.send(bytes(command, encoding='raw_unicode_escape')) return client.recv(4096).decode("utf-8")
def runCommand(client, command): client.send(bytes(command, encoding='raw_unicode_escape'))
HOST = '127.0.0.1' PORT = 5026
client = socket.socket() client.connect((HOST,PORT)) isReady = True if (runQuery(client, 'AFR:SYST:READ?\n') == '1\n') else False if (not isReady): print('Plugin not ready') client.close() sys.exit() |
---|
stepCount = int(runQuery(client, 'AFR:SYST:STEP:COUN?\n')) for step in range(1, stepCount + 1): type = runQuery(client, 'AFR:SYST:STEP{}:TYPE?\n'.format(step)) print(type)
if (type == 'TRANSMISSION\n'): runCommand(client, 'AFR:CALC:STEP{}:THRU\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:THRU:OFFS ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:THRU:OFFS 100,200\n'.format(step)) elif (type == 'REFLECT\n'): runCommand(client, 'AFR:SYST:STEP{}:REF:APPR ON\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:REF:OPEN ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:REF:OPEN\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:REF:SHOR ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:REF:SHOR\n'.format(step))
elif (type == 'DUT\n'): runCommand(client, 'AFR:CALC:STEP{}:DUT\n'.format(step))
runCommand(client, 'AFR:CALC:STEP{}:THRU\n'.format(stepCount + 1)) print(runQuery(client, 'AFR:SYST:ERR?\n'))
runCommand(client, 'AFR:SYST:CORRECT:SAVE \"C:/Users/User/Desktop/File\"\n')
runQuery(client, '*OPC?\n') client.close() |
---|
Program description:
1. Connects the libraries that will be used in the program code.
import socket import sys |
The socket library is used to communicate with the TCP server. The sys library is used to interact with the Python interpreter.
2. Declares auxiliary functions that allow reuse of server communication code.
def runQuery(client, command): client.send(bytes(command, encoding='raw_unicode_escape')) return client.recv(4096).decode("utf-8")
def runCommand(client, command): client.send(bytes(command, encoding='raw_unicode_escape')) |
The runQuery(client, command) function receives a socket and a command as input. The command is converted from a string to an array of bytes. The array is sent to the server using the socket. The socket then waits for a response from the server. The recv() method of the socket is used for this action. The recv() method of the socket requires to specify the number of bytes to be received from the server, e. g. 4096. The result is then converted to a string.
The runCommand(client, command) function also receives a socket and a command as input. The command is converted from a string to an array of bytes, which is then sent to the server using the socket. The runCommand(client, command) function does not get the result from the server.
3. Saves the server IP address (127.0.0.1) and the port used to connect (5026) to variables. Then creates a socket that connects to the server at the specified IP address and port.
HOST = '127.0.0.1' PORT = 5026
client = socket.socket() client.connect((HOST,PORT)) |
4. Sends the 'AFR:SYST:READ?\n' command to the server, which queries the connection status of the plugin to VNA. If the answer is 1, i.e. the plug-in is connected to VNA, it sets the variable to True.
Checks the connection status. If there is no connection, it displays the message Plug-in is not ready. Then closes the socket connection and terminates the program.
isReady = True if (run_query(client, 'AFR:SYST:READ?\n') == '1\n') else False if (not isReady): print('Plugin is not ready') client.close() sys.exit() |
5. Retrieves the number of measurement steps with the command 'AFR:SYST:STEP:COUN?\n'. Then for each step starting from the first one it retrieves its type by using the command 'AFR:SYST:STEP{}:TYPE?\n'.
The format() method is used to write values to the command line. Inputting the step number into this method will replace the curly brackets. The measurement step is written similarly for the rest of the commands.
stepCount = int(runQuery(client, 'AFR:SYST:STEP:COUN?\n')) for step in range(1, stepCount + 1): type = runQuery(client, 'AFR:SYST:STEP{}:TYPE?\n'.format(step)) |
6. If the measurement step type is 'TRANSMISSION', it performs an S-parameter measurement of the two fixtures with the 'AFR:CALC:STEP{}:THRU\n' command. Then it activates the offset mode of the calibration plane with the command 'AFR:SYST:STEP{}:THRU:OFFS ON\n'. After activating the mode, the offset values can be set with the command 'AFR:CALC:STEP{}:THRU:OFFS 100,200\n'. It passes the necessary offset values set in picoseconds, e.g. 100 and 200 in place of parameters.
if (type == 'TRANSMISSION\n'): runCommand(client, 'AFR:CALC:STEP{}:THRU\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:THRU:OFFS ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:THRU:OFFS 100,200\n'.format(step)) |
7. If the step type is 'REFLECT\n' the command 'AFR:SYST:STEP{}:REF:APPR ON\n' is executed, which enables filtering and smoothing of the maximum signal.
'AFR:SYST:STEP{}:REF:OPEN ON\n' then activates the mode to measure the reflection coefficients of the fixture in the second step in the Open state. When this mode is activated, you can measure fixture reflection coefficients with the 'AFR:CALC:STEP{}:REF:OPEN\n' command.
'AFR:SYST:STEP{}:REF:SHOR ON\n' then activates the mode to measure fixture reflection coefficients in the second step in the Short state. When the mode is activated, fixture reflection coefficients can be measured with the command 'AFR:CALC:STEP{}:REF:SHOR\n'..
elif (type == 'REFLECT\n'): runCommand(client, 'AFR:SYST:STEP{}:REF:APPR ON\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:REF:OPEN ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:REF:OPEN\n'.format(step))
runCommand(client, 'AFR:SYST:STEP{}:REF:SHOR ON\n'.format(step)) runCommand(client, 'AFR:CALC:STEP{}:REF:SHOR\n'.format(step)) |
8. Provided that the measurement step type is ‘DUT\n, performs a measurement of the input and output reflection coefficient of the fixture with the DUT using the 'AFR:CALC:STEP{}:DUT\n' command.
elif (type == 'DUT\n'): runCommand(client, 'AFR:CALC:STEP{}:DUT\n'.format(step)) |
9. When executing a command for a non-existent step, the 'AFR:SYST:ERR?\n' command can be used to obtain the error code. The received value is displayed on the screen as follows:
runCommand(client, 'AFR:CALC:STEP{}:THRU\n'.format(stepCount + 1)) print(runQuery(client, 'AFR:SYST:ERR?\n')) |
10. Saves the results using the command 'AFR:SYST:CORRECT:SAVE \"C:/Users/User/Desktop/File\"\n', specifying the file path.
run_command(client, 'AFR:SYST:CORRECT:SAVE \"C:/Users/User/Desktop/File\"\n') |
11. Using the command '*OPC?\n' makes the pug-in wait for completion of all requested commands. Before execution, it performs socket connection to ensure that all commands are completed.
run_query(client, '*OPC?\n') client.close() |