Open Socket LAN connection using Python

October 20, 2017

Automating a test can dramatically increase the productivity, throughput, and accuracy of a process. Automating a setup involves connecting a computer to the test instrumentation using a standard communications bus like USB or LAN and then utilizing code entered via a software layer (like LabVIEW, .NET, Python, etc..) to sequence the specific instrument commands and process data.

In this note, we are going to show how to use Python to create a communications link between an instrument and a remote computer using a LAN connection. Once the connection is verified, you can begin to work on the control software.

NOTE: This example requires open sockets on the instrumentation. At this time, not all SIGLENT products feature open sockets. Check the product page FAQs or with your local SIGLENT support office for more information.

Python is an interpreted programming language that lets you work quickly and is very portable. Python has a low-level networking module that provides access to the socket interface. Python scripts can be written for sockets to do a variety of test and measurements tasks.

Here is a short script that opens a socket, sends a query, and closes the socket. It does this loop for 10 times.

1. Power on and connect the instrument to the network via LAN

2. Verify that the Gateway, Subnet Mask, and IP address of the instrument are valid for the network you wish to use. This information is typically located in the System Information or IO menu. See the specific instrument user’s guide for more information on LAN settings.

3. Download python and your favorite python editor (I use IDLE):

Start your python editor:

5. Open a new file by pressing File > New File.. and name the file

6. Copy and paste the code at the end of this note into the file editing window:

7. Change the IP address so that it matches the IP address of the instrument you wish to connect to:

Save the file:

8. To Run, select Run and Run Module:

#!/usr/bin/env python
#-*- coding:utf-8 –*-
# The short script is a example that open a socket, sends a query,
# print the return message and closes the socket.
import socket # for sockets
import sys # for exit
import time # for sleep
remote_ip = “” # should match the instrument’s IP address
port = 5024 # the port number of the instrument service
count = 0

def SocketConnect():
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print (‘Failed to create socket.’)
#Connect to remote server
s.connect((remote_ip , port))
info = s.recv(4096)
print (info)
except socket.error:
print (‘failed to connect to ip ‘ + remote_ip)
return s

def SocketQuery(Sock, cmd):
try :
#Send cmd string
except socket.error:
#Send failed
print (‘Send failed’)
reply = Sock.recv(4096)
return reply

def SocketClose(Sock):
#close the socket

def main():
global remote_ip
global port
global count

# Body: send the SCPI commands *IDN? 10 times and print the return message
s = SocketConnect()
for i in range(10):
qStr = SocketQuery(s, b’*IDN?’)
print (str(count) + “:: ” + str(qStr))
count = count + 1
input(‘Press “Enter” to exit’)

if __name__ == ‘__main__’:
proc = main()