Pyvisa 示波器驱动
<div class='showpostmsg'> 本帖最后由 xutong 于 2024-8-8 21:17 编辑<p>厂商:yokogawa </p>
<p>——————————————————————</p>
<p>是日系厂商,建议购买国产示波器振兴民族工业</p>
<p>——————————————————————</p>
<p>型号:DLM3054</p>
<p>Rev:1</p>
<p>驱动更新时间:2024/8/8</p>
<pre>
<code class="language-python"># DLM3054
# 2024/8/8
# Xutong
import pyvisa
import time
def binblock_raw(data_in):
# Grab the beginning section of the image file, which will contain the header.
Header = str(data_in)
print("Header is " + str(Header))
# Find the start position of the IEEE header, which starts with a '#'.
startpos = Header.find("#")
# print("Start Position reported as " + str(startpos))
# Check for problem with start position.
if startpos < 0:
raise IOError("No start of block found")
# Find the number that follows '#' symbol.This is the number of digits in the block length.
Size_of_Length = int(Header)
# print("Size of Length reported as " + str(Size_of_Length))
##Now that we know how many digits are in the size value, get the size of the image file.
Image_Size = int(Header)
# print("Number of bytes in image file are: " + str(Image_Size))
# Get the length from the header
offset = startpos + Size_of_Length
# Extract the data out into a list.
return data_in
class oscilloscope:
scope='None'
def __init__(self,scope_addres,FileLocation='None'):
rm = pyvisa.ResourceManager()
#连接示波器
oscilloscope.scope = rm.open_resource(scope_addres)
self.filelocation = FileLocation
def RUN(self):
oscilloscope.scope.write(':START')
# oscilloscope.scope.close()
def STOP(self):
oscilloscope.scope.write(':STOP')
def GetScreen(self,filename):
# 发送查询命令获取波形数据
oscilloscope.scope.write('IMAGe:SEND?')
# 读取原始数据
raw_data = oscilloscope.scope.read_raw()
# 数据转换成可以写入的文件
Image_Data = binblock_raw(raw_data)
# 将屏幕截图数据保存为文件
imgFile = open(self.filelocation + filename + '.PNG', "wb")
# 写入图像数据
imgFile.write(Image_Data)
# 关闭图像
imgFile.close()
# oscilloscope.scope.close()
# time out 单位是mS 返回值是1代表没有采集到
# ch 设置触发通道 参数 1~4
# Level 设置触发电压/电流等级 参数例子 0.1V 10A
# 需要手动改上升沿下降沿
def SingleTrigger(self,TimeOut,Ch,Level):
oscilloscope.scope.write(":TRIGGER:SOURCE:CHANNEL"+str(Ch)+':LEVEL '+str(Level))
oscilloscope.scope.write(":SSTART? "+str(TimeOut))
if TimeOut==0:
time.sleep(5)
else:
time.sleep(TimeOut/8)
# 读取返回值
oscilloscope.scope.read()
# 设置时基 例子 TimeBase(100MS)
def TimeBase(self,time_div):
oscilloscope.scope.write(':TIMEBASE:TDIV '+time_div)
def ChanelLable(self,Ch,Lable):
oscilloscope.scope.write(':CHANNEL%d:LABEL:DISPLAY ON'%Ch)
time.sleep(0.1)
oscilloscope.scope.write(':CHANNEL%d:LABEL:DEFINE "%s"'% (Ch,Lable))
# 例子 ChanelDiv(self,1,2V,DC)
def ChanelDiv(self,Ch,Div,Coupling='DC'):
# DIV 1,2,5,10 {mV,V,10V}
oscilloscope.scope.write(':CHANNEL%d:VDIV '% Ch+str(Div))
# Coupling 支持AC DC DC 50ohm
oscilloscope.scope.write(':CHANNEL%d:COUPLING ' % Ch + Coupling)
# 默认使用ZOOM1
def ZoomSet(self,Ratio,Status=0,Pos=0,Yscale=20):
# 设置小窗大小,值越大小窗越小
oscilloscope.scope.write(":ZOOM1:MAG %d"%Ratio)
# 设置ZOOM的显示位置范围,-5 到5
oscilloscope.scope.write(":ZOOM1:POSITION %d"%Pos)
# 设置ZOOM1在屏幕上的显示比例
oscilloscope.scope.write(":ZOOM1:MAIN %d"%Yscale)
if Status==1 or Status=='ON':
oscilloscope.scope.write(':ZOOM1:DISPLAY ON')
else:
oscilloscope.scope.write(':ZOOM1:DISPLAY OFF')</code></pre>
<p> </p>
<p>厂商:Rohde&Schwarz</p>
<p>——————————————————————</p>
<p> 建议购买国产仪器振兴民族工业</p>
<p>——————————————————————</p>
<p>型号:RTM3000</p>
<p>Rev:1</p>
<p>驱动更新时间:2024/8/8</p>
<p> </p>
<pre>
<code># RTM3000
# 2024/8/8
# Xutong
import time
import os
import pyvisa
from enum import Enum
def binblock_raw(data_in):
# Grab the beginning section of the image file, which will contain the header.
Header = str(data_in)
print("Header is " + str(Header))
# Find the start position of the IEEE header, which starts with a '#'.
startpos = Header.find("#")
# print("Start Position reported as " + str(startpos))
# Check for problem with start position.
if startpos < 0:
raise IOError("No start of block found")
# Find the number that follows '#' symbol.This is the number of digits in the block length.
Size_of_Length = int(Header)
# print("Size of Length reported as " + str(Size_of_Length))
##Now that we know how many digits are in the size value, get the size of the image file.
Image_Size = int(Header)
# print("Number of bytes in image file are: " + str(Image_Size))
# Get the length from the header
offset = startpos + Size_of_Length
# Extract the data out into a list.
return data_in
# 设置触发模式
class triggermode(Enum):
Auto="TRIGger:A:MODE AUTO"
Normal="TRIGger:A:MODE NORM"
# 创建 OSC的类
class oscilloscope:
scope='None'
def __init__(self,scope_addres,FileLocation='None'):
rm = pyvisa.ResourceManager()
#连接示波器
oscilloscope.scope = rm.open_resource(scope_addres)
# 初始化示波器图像保存地址
self.filelocation = FileLocation
def RUN(self):
# 随机插入 Xutong
oscilloscope.scope.write("RUN")
def STOP(self):
oscilloscope.scope.write("STOP")
def TriggerMode(self,Param:triggermode):
oscilloscope.scope.write(Param.value)
def SingleTrigger(self,TimeOut,Ch,Level):
# 设置触发通道
# oscilloscope.scope.write("TRIGger:A:SOURce CH%d"%1)
# 设置上升沿还是下降沿采样
# oscilloscope.scope.write("TRIGger:A:EDGE:SLOPe NEG")
# Rohde 没有
print('TimeOUT show %d'%TimeOut)
# oscilloscope.scope.write("TRIGger:A:EDGE:SLOPe POS")
oscilloscope.scope.write("TRIGger:A:LEVel%d:VALue %d"%(Ch,Level))
# 开始触发
oscilloscope.scope.write("SING")
# 使用示例 ChanelLable(2, '"VCS"')
def ChanelLable(self,Ch,Lable):
oscilloscope.scope.write('CHANnel%d:LABel %s'%(Ch,Lable))
def GetScreen(self, filename):
oscilloscope.scope.write("HCOPy:DATA?")
time.sleep(0.2)
# 读取原始数据
raw_data = oscilloscope.scope.read_raw()
# 数据转换成可以写入的文件
Image_Data = binblock_raw(raw_data)
# 将屏幕截图数据保存为文件
imgFile = open(self.filelocation + filename + '.PNG', "wb")
# 写入图像数据
imgFile.write(Image_Data)
# 关闭图像
imgFile.close()
def ChanelDiv(self,Ch,Div):
# 必须使用科学计数法
oscilloscope.scope.write("CHANnel%d:SCALe %s"%(Ch,Div))
def TimeBase(self,time_div):
# 可以使用科学计数法设置 比如 1e-3代表1mS
oscilloscope.scope.write('TIMebase:SCALe %s'%time_div)
# oscilloscope.scope.write('TIMebase:SCALe 1e-3')
def ZoomSet(self, Xscale, Status=0, Pos=0, Ztime=0):
if Status==1 or Status=='ON':
oscilloscope.scope.write('TIMebase:ZOOM:STATe ON')
else:
oscilloscope.scope.write('TIMebase:ZOOM:STATe OFF')
# 设置Zoom的放大的大小 越小放大倍数越大
oscilloscope.scope.write('TIMebase:ZOOM:SCALe %d' % Xscale)
oscilloscope.scope.write('TIMebase:ZOOM:TIME %d' % Ztime)
# 设置偏移
oscilloscope.scope.write('TIMebase:ZOOM:POSition %d' % Pos)
# 获取指定通道Csv文件 必须 示波器在RUN的状态才可以导出CSV
def GetChannelCSV(self,Ch,Filename):
oscilloscope.scope.write("FORM CSV")
# print(oscilloscope.scope.query("FORM?"))
oscilloscope.scope.write("CHAN%d:DATA:POINts DMAX"%Ch)
oscilloscope.scope.write("CHAN%d:DATA?"%Ch)
CSVCH= oscilloscope.scope.read_raw()
CSVFile = open(self.filelocation + Filename + '.txt', "wb")
CSVFile.write(CSVCH)
# 关闭图像
CSVFile.close()
with open(self.filelocation+str(Filename) + '.txt', 'r') as file_in, open(
self.filelocation+str(Filename) + '.csv', 'w') as file_out:
next(file_in)# 跳过第一行
for line in file_in:
file_out.write(line)
file_path = self.filelocation+str(Filename) + '.txt'
os.path.exists(file_path)
os.remove(file_path)
</code></pre>
<p><a href="https://docs.keysight.com/kkbopen/python-example-program-for-e5080b-csv-trace-data-save-and-binary-transfer-to-controller-pc-721232696.html" target="_blank"><span style="color:#e74c3c;"><span style="font-size:18px;"><em>binblock_raw</em>(<em>data_in</em>) 函数来自 Keysight 的Python 编程参考</span></span></a></p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>
页:
[1]