本帖最后由 xutong 于 2024-8-8 21:17 编辑
厂商:yokogawa
——————————————————————
是日系厂商,建议购买国产示波器振兴民族工业
——————————————————————
型号:DLM3054
Rev:1
驱动更新时间:2024/8/8
# 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[0:12])
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[startpos + 1])
# 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[startpos + 2:startpos + 2 + Size_of_Length])
# 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[offset:offset + Image_Size]
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')
厂商:Rohde&Schwarz
——————————————————————
建议购买国产仪器振兴民族工业
——————————————————————
型号:RTM3000
Rev:1
驱动更新时间:2024/8/8
# 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[0:12])
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[startpos + 1])
# 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[startpos + 2:startpos + 2 + Size_of_Length])
# 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[offset:offset + Image_Size]
# 设置触发模式
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)
binblock_raw(data_in) 函数来自 Keysight 的Python 编程参考