Using Python and Sockets: System Power Supply Programming
Using Python and Sockets: System Power Supply Programming
Using Python and Sockets: System Power Supply Programming
Program Structure
The code is structured to emphasize programming the system power supply.
Four functions were created to make it easy to explain the power supplies
commands (SCPI) without having to understand Python or sockets. For example,
outPut(‘*RST’) sends the SCPI command *RST to the power supply. Future
white papers will be more focused on the power supply SCPI commands. This
white paper will provide a little background on Python, sockets and the four
functions used to send the SCPI commands.
Figure 1. Four functions were created to send the SCPI commands and receive the
measurement data from the power supply.
Python
If you are new to Python, you can find many useful tutorials and code snippets
to explain every Python concept. One of the core benefits of Python is that it is easy
to learn. There are a couple of things to keep in mind. Python is whitespace sensitive
and uses indentation for flow control. Using the development environment Visual Studio
Code helps with creating the proper indenting. Figure 2. provides an example of using
white space (indenting) for flow control. Typically, variables are not declared or provided
a data type and can be re-used. Although, a global declaration is available to share
variables and their values between all functions.
Sockets
Socket connections provide a way to communicate with an instrument connected via
LAN. System power supplies often use the LAN interface as they are frequently located
remotely in a test system. If you are interested in further exploring sockets, our power
supplies use internet domain, streaming sockets. Software must use the same type
of socket along with an IP address and port to connect. Sockets have built-in error
trapping which is helpful for debugging. In addition, the code example, Figure 3 is
using the socket-timeout feature to trap any errors that occur in communicating with
the instrument.
try:
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.settimeout(8) # 8 second timeout
except socket.error as e:
print(‘Error creating socket: %s’ % e)
sys.exit(1)
try:
skt.connect((IPaddr, port))
except socket.gaierror as e:
print(‘Address-related error connecting to instrument: %s’ % e)
sys.exit(1)
except socket.error as e:
print(‘Error connecting to socket on instrument: %s’ % e)
sys.exit(1)
Figure 4. Output and Enter functions pass ASCII data to and from the instrument.
The enter function receives ASCII data from the instrument. Spaces and escape codes
are stripped from the string. Print(enTer()) will display the string received from the
instrument. The routine can return numerical data or messages.
try:
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skt.settimeout(8) # 8 second timeout
except socket.error as e:
print(‘Error creating socket: %s’ % e)
sys.exit(1)
try:
skt.connect((IPaddr, port))
except socket.gaierror as e:
print(‘Address-related error connecting to instrument: %s’ % e)
sys.exit(1)
except socket.error as e:
print(‘Error connecting to socket on instrument: %s’ % e)
sys.exit(1)
Sample program