使用 Python 进行检测编程
知识库文章介绍了使用 Python 编程语言对 Magna-Power 可编程电源产品进行编程。Python 是一种流行的编程语言,以其简单性、代码可读性而闻名,并且不需要任何特殊编译。Python 的易用性和快速的学习曲线使其成为创建程序以控制、进行测量甚至为可编程仪器创建绘图的绝佳语言。此外,Magna-Power 对可编程仪器标准命令 (SCPI) 的广泛支持意味着该公司的产品可以通过简单、直观的命令在 Python 中轻松控制。
Magna-Power 的产品支持多种不同的通信接口,包括:RS-232、TCP/IP 以太网、USB、RS-485 和 IEEE-488 GPIB。尽管接口不同,但特定产品系列的 SCPI 命令是相同的。SCPI 命令记录在相应产品系列的用户手册中。创建 Python 程序时,接口之间的唯一区别是设备连接的设置。
USB、串行或 RS-485,它们将使用 pySerial 创建与仪器的串行连接:
import serial
conn = serial.Serial(port='COM4', baudrate=115200)
xGen 产品的串行波特率为 115200,而非 xGen 产品的串行波特率为 19200。端口位置由您的作系统定义。在 Windows 中,可以在设备管理器中找到此端口。
后续通过串行连接发送和接收命令将如下所示:
conn.write('*IDN?\n'.encode())
print(conn.readline())
TCP/IP 以太网连接设置如下:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.86', 50505))
后续通过 TCP/IP 以太网连接发送和接收命令将如下所示:
s.sendall('*IDN?\n'.encode())
print(s.recv(4096))
IEEE-488 GPIB连接将需要PyVISA,连接如下:
import visa
rm = visa.ResourceManager()
inst = rm.open_resource('GPIB0::12::INSTR')
随后通过 IEEE-488 GPIB 连接发送和接收命令将如下所示:
print(inst.query("*IDN?"))
以下示例提供了使用 xGen MagnaDC 电源的更深入的示例 Python 程序。使用 Python 对非 xGen MagnaDC 可编程直流电源进行编程几乎相同,只是对 SCPI 命令进行了细微的更改,如相应产品系列的用户手册中所述。
以下基本示例创建 TCP/IP 以太网连接,发送一些初始化命令,启用 DC 输出,将当前级别提高到 5 Adc,等待 20 秒,然后关闭。
# Import time: Time access and conversions to allow for pausing
# Import socket: Low-level networking interface to allow for socket programming
import time, socket
# Create socket object s with (host, port)
# Define host as hostname in Internet domain
# Define socket type as stream, allowing a port number to be defined
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to product's IP address address at default 50505 socket
s.connect(('192.168.0.86', 50505))
# Send SCPI command requesting the product to identify itself
s.sendall('*IDN?\n'.encode())
# Receive the product's response and display it in the terminal
print(s.recv(4096))
# Send SCPI command to configure the MagnaDC for local control
s.sendall('CONF:SOUR 0\n'.encode())
# Send SCPI command to set the DC output current to 0 Adc before enabling DC input
s.sendall('CURR 0\n'.encode())
# Send SCPI command to enable the MagnaDC power supply output
s.sendall('OUTP:START\n'.encode())
# Send SCPI command to set the DC input current to 5 Adc
s.sendall('CURR 5\n'.encode())
# Wait 20 seconds
time.sleep(20)
# Send SCPI command to disable the DC output
s.sendall('OUTP:STOP\n'.encode())
# Close the communication channel to the product
s.close()
以下示例创建与产品的串行连接,确定它是什么产品,然后发送一系列电流命令,每个电流电平之间间隔 20 秒。这种类型的程序也可以扩展为循环显示电压、功率和电阻值。
# Import pySerial, which encapsulates the serial port access
# Import time: Time access and conversions to allow for pausing
import serial, time
# Create serial connection object with default baudrate for MagnaLOADs
conn = serial.Serial(port='COM4', baudrate=115200)
# Send SCPI command requesting the product to identify itself
conn.write('*IDN?\n'.encode())
# Receive the product's response and display it in the terminal
print conn.readline()
# Create array of current set points
currSetPoints = [50, 100, 150, 250]
# Send SCPI command to configure the MagnaDC power supply for local control
s.sendall('CONF:SOUR 0\n'.encode())
# Send SCPI command to enable the MagnaDC power supply
conn.write('OUTP:START\n'.encode())
# For each entry in currSetPoints array
# Print static text and current set point to the terminal
# Send the new set point to the MagnaDC power supply
# Wait 20 seconds
for currSetpoint in currSetPoints:
print 'Setting Current to %s A' % currSetpoint
conn.write('CURR {0}\n'.format(currSetpoint).encode())
time.sleep(20)
# Send SCPI command to disable the MagnaDC power supply output
conn.write('OUTP:STOP\n'.encode())
# Close the communication channel to the product
conn.close()
在最后一个深入的示例中,xGen MagnaDC 电源被编程为使用从逗号分隔值 (.csv) 文件读取的设定点和时间对电池进行放电,使用产品的高精度测量命令测量直流输入,然后提供测量数据与时间的关系图。该程序可以进一步扩展以生成 PDF 测试报告,集成测量数据、绘图以及来自其他仪器的信息。
# Import plotting library Matplotlib
# Import .csv parser
# Import pySerial, which encapsulates the access for the serial port
# Import time: Time access and conversions to allow for pausing
# Import numpy for mathemetical manipulation of arrays
import matplotlib.pyplot as plt
import csv, serial, time
import numpy as np
# Create serial connection object with default baudrate for xGen products
conn = serial.Serial(port='COM8', baudrate=115200)
# Create an empty numpy data array 999 rows, 4 columns
outputSamples = np.empty([999, 3])
iSample = 0
# Send SCPI command to configure the MagnaDC for local control
s.sendall('CONF:SOUR 0\n'.encode())
# Send SCPI command to enable the MagnaDC power supply output
conn.write('OUTP:START\n'.encode())
# Open data stream to .csv file. .csv has two columns:
# Column 1: Current set point in amperes
# Column 2: Time in seconds
# The first row is headers
with open('example_profile.csv', 'r') as csvfile:
# Read the .csv file using comma (,) as the delimeter
dataset = csv.reader(csvfile, delimiter=',')
# Skip the header row
next(dataset)
# Split the dataset up to rows
rows = list(dataset)
# Create an empty array for measurements, the same length as .csv row numbers
inputSamples = np.empty([len(rows), 2], dtype=float)
# Record the time that the measurements began
testStartTime = time.time()
# Run for loop while there are still rows of data left
for idx, data in enumerate(rows):
# Store row data to array
inputSamples[idx] = [data[0], data[1]]
# Send SCPI command to MagnaDC power supply to current set point at present row
conn.write('CURR {0}\n'.format(data[0]).encode())
# Determine how long the MagnaDC power supply should stay at this current set point
stopTime = testStartTime + int(data[1])
# Run while loop while there is still time left
while time.time() < stopTime:
# Send SCPI command to MagnaDC Power supply to measure all DC output variables
conn.write('MEAS:ALL?\n')
# Get the MagnaDC power supply's response and split it up into its respective variables
[curr, volt, pwr] = (conn.readline()).split(',')
# Round measurements and store them to array, along with with time
outputSamples[iSample] = ([round(float(curr), 2), round(float(volt), 2), round(time.time() - testStartTime, 2)])
iSample += 1
time.sleep(0.5)
# Send SCPI command to disable Magna DC power supply output
conn.write('OUTP:STOP\n'.encode())
# Create a plot series of current vs. time
plt.subplot(2, 1, 1)
plt.plot(outputSamples[0:iSample, 2], outputSamples[0:iSample, 0], 'r--')
plt.ylabel('Output Current(A)')
plt.title('I-V Profile')
# Create a plot series of voltage vs. time
plt.subplot(2, 1, 2)
plt.plot(outputSamples[0:iSample, 2], outputSamples[0:iSample, 1], 'b--')
plt.xlabel('Time (s)')
plt.ylabel('Output Voltage(V)')
# Show the plot
plt.show()
更多推荐


所有评论(0)