PYVISA:如何传输二进制数据块以便图像格式正确并保存到我的PC上?

时间:2017-03-08 20:53:37

标签: python-3.x byte file-transfer instrumentation pyvisa

我正在尝试转移Rhode&屏幕截图。 Schwarz - FSV信号分析仪到我的电脑。

如何传输二进制数据块,以便将图像从仪器正确保存到我的PC上?我的代码如下。文件会保存到我的桌面,但我的计算机会抛出错误而无法打开,因为文件格式不正确。

import visa
rm = visa.ResourceManager()
inst = rm.open_resource('TCPIP0::178.168.48.31::inst0::INSTR')

inst.write("mmem:name 'C:\eswScreen.wmf\'")
inst.write("MMEM:DATA? 'C:\eswScreen.wmf\'")
img = inst.read_raw()

target = open(r"C:\Users\myName\Desktop\screenShot.wmf", 'wb')
target.write(img)
target.close()

file shows up on Desktop

Error when trying to open file on Desktop

我确认屏幕截图已保存在仪器上。还使用其他工具验证了SCPI语法是否正确。所以它必须是binblock数据的传输方式。非常感谢任何建议。

2 个答案:

答案 0 :(得分:1)

1 : 2 : [3]

这对我有用。

答案 1 :(得分:1)

PyVisa 1.1版本 罗德与施瓦茨FSWP

def SaveScreenImage(filepath,filename):
    pna.write('HCOPy:DEVice:LANGuage PNG')
    pna.write(f'MMEMory:NAME "{filepath}\{filename}.png"')
    pna.write('HCOPy:IMMediate')

def ScreenCap(filepath,filename):
    """Capture screen image from anaylzer and save to a file on the host PC

    Example Input:
    
        filepath = 'C:\Data'
        filename = 'test'
    """

    
    SaveScreenImage(r'C:\\',filename)   #create temporary file on analyzer
    img = pna.query_binary_values(f'MMEMory:DATA? "C:\{filename}.png"',datatype='B',is_big_endian=False,container=bytearray)
    
    with open(f'{filepath}\{filename}.png', 'wb') as target:
        target.write(img)
        
    pna.write(f'MMEMory:DELete:IMMediate "C:\{filename}.png"')   #remove file from analyzer