Open Socket LAN connection using Python

Posted on: August 20th, 2021 by James

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 utilising 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 favourite python editor (I use IDLE):

https://www.python.org/

https://docs.python.org/2/library/idle.html

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 = β€œ192.168.0.17” # should match the instrument’s IP address
port = 5024 # the port number of the instrument service
count = 0

def SocketConnect():
try:
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print (β€˜Failed to create socket.’)
sys.exit();
try:
#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
Sock.sendall(cmd)
time.sleep(1)
except socket.error:
#Send failed
print (β€˜Send failed’)
sys.exit()
reply = Sock.recv(4096)
return reply

def SocketClose(Sock):
#close the socket
Sock.close()
time.sleep(.300)

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
SocketClose(s)
input(β€˜Press β€œEnter” to exit’)

if __name__ == β€˜__main__’:
proc = main()

Products Mentioned In This Article:

  • SPD3000X please see HERE