上位机使用python/matlab通过网线VISA/SCPI编程远程控制旧版A.06.04.32的安捷伦agilent矢量网络分析仪(VNA)采集S21参数

       近日需要通过上位机连接矢网采数,但是可用的矢网只有一个零几年的矢网安捷伦 agilent e8363b。一开始想使用labview,但是使用NI MAX控制不成功(问题在instrument VISA Address),只好自己从底层控制(如果你的NI MAX能成功,可以直接使用labview控制呢)。本来想通过GPIB来控制,但是实验室没有GPIB转USB的线。一看矢网上有网口,故准备通过网络来控制矢网。经过一番摸索,成功地使用matlab和python通过SCIP协议来用网线控制矢网采数。(如果你熟悉VB的话,请参考VB的例子。VB的资料明显更多。)

下面对控制过程进行记录以留待后续查阅。

  • 查看自己矢网固件版本!在矢网应用中是可以看到的。
  • 查看矢网设置,注意看是否一直有SCPI支持,注意看设置的内网IP。
  • 安装 Keysight IO library suite。
  • 安装Keysight Command Expert 用于检测矢网连接是否成功。
  • 使用python的话安装pyvisa(pip install 即可)。
  • 使用matlab要安装对应驱动和库。自行下载安装。
    • matlab的例子参看Two Port S-Parameter Measurement(matlab file exchange)或者Characterizing a low-noise amplifier by measuring its S-parameters。文中注释写得非常详细。请注意一段话。
      • This MATLAB example connects to and configures a Keysight (Agilent) PNA (N5222A) to measure the S-parameters of a low noise amplifier (LNA). The instrument used in this example is running firmware version A.09.90.02. The Standard Commands for Programmable Instruments (SCPI) for the PNA in this example may need to be modified if you are using a different instrument or firmware. The SCPI command set for most instruments is available in the instrument programmer's manual from your instrument vendor.
      • 固件版本非常重要。例程中的版本是A09.90.02的(我的是06年的一个版本A.06.04.32)。不同的版本支持的SCPI语法是有差异的。具体可以看各个公司的编程手册——这个网上都可以搜索得到。
  • 连接网线,设置上位机IP使之与矢网处于同一个子网,使用ping命令检测联通性。
  • 使用command expert 检测是否能够识别。注意建立instrument连接的时候如果不能自动识别设备,需要手动添加其名字,比如我这台的格式是这样的
    • instrumentVISAAddress = 'TCPIP0::192.168.1.10::hpib7,16::INSTR'
    • 注意中间有个hpib7,16 。可以使用手动输入的方式来连接这台设备。IP是你矢网的IP。
    • matlab官方 网站上的例子的格式是这样的 instrumentVISAAddress = 'TCPIP0::127.0.0.1::inst0::INSTR'。注意二者的区别
  • 在command expert 能检测到你的设备之后,你就可以自行编程了。此时你可以使用NI MAX+LABVIEW(如果能成功识别的话),否则就只有自己用VB VC PYTHON MATLAB等按照VISA/SCPI来编程了(就是我所走的路)。
  • 下面是个matlab控制采数的例子。
    • %% 在官方的matlab例程上安装对应固件版本的编程手册修改的
      instrumentVISAAddress = 'TCPIP0::192.168.1.10::hpib7,16::INSTR';
      % Define frequency range  
      frequencyRange = [f1 f2];% 起始频率
      % Number of points in measurement
      numPoints = NN; 
      
      
      
      instrObj = visa('agilent',instrumentVISAAddress); % 创立链接。
      instrObj.InputBufferSize = 10e6;% set buffer
      instrObj.ByteOrder = 'littleEndian';
      fopen(instrObj);
      clrdevice(instrObj); % clear 
      % Display information about instrument
      IDNString = query(instrObj,'*IDN?');%信息查询
      fprintf('Connected to: %s\n',IDNString); 
      %fprintf(instrObj,'SYSTem:PREset');
      %fprintf(instrObj,'SYSTem:FPReset');
      fprintf(instrObj,'*CLS;*wai');
      
      
      % Define a measurement name and parameter
      fprintf(instrObj,'CALCulate:PARameter:DEFine ''MySMeaS21'',S21'); % s21
      % Create a new display window and turn it on
      fprintf(instrObj,'DISPlay:WINDow1:STATE ON');
      % Associate the measurements to WINDow1
      fprintf(instrObj,'DISPlay:WINDow1:TRACe1:FEED ''MySMeaS21''');
      % Turn ON the Title, Frequency, and Trace Annotation to allow for
      % visualization of the measurements on the instrument display
      fprintf(instrObj,'DISPlay:WINDow1:TITLe:STATe ON');
      fprintf(instrObj,'DISPlay:ANNotation:FREQuency ON');
      fprintf(instrObj,'DISPlay:WINDow1:TRACe1:STATe ON');
      
      % Set the number of points
      fprintf(instrObj, sprintf('SENSe:SWEep:POINts %s',num2str(numPoints)));
      % Set the frequency ranges
      fprintf(instrObj, sprintf('SENSe:FREQuency:STARt %sHz',num2str(frequencyRange(1))));
      fprintf(instrObj, sprintf('SENSe:FREQuency:STOP %sHz',num2str(frequencyRange(2))));
      fprintf(instrObj,'TRIG:SOUR MANual'); %手动触发
      fprintf(instrObj,'TRIG:SCOPe ALL');
      fprintf(instrObj,'SENSe:SWEep:MODE CONTinuous');
      % Set instrument to return the data back using binblock format
      %fprintf(instrObj, 'FORMat:DATA REAL,64'); % 64位 浮点数
      fprintf(instrObj, 'FORMat:DATA ASCII'); % 编码方式
      fprintf(instrObj,'CALCulate:PARameter:SELect ''MySMeaS21''');
      % i=0;
      % while(i<2) % 多次读取的话使用循环即可
      fprintf(instrObj,'INITiate:IMMediate;*wai');
      fprintf(instrObj, 'Display:WINDow1:TRACe1:Y:Scale:AUTO');
      fprintf(instrObj,'CALCulate:DATA? SDATA');
      %rawDataDB = binblockread(instrObj, 'double');
      data=fread(instrObj);% 读取数据
      fid=fopen('test.txt','wb');
      data=fwrite(fid,data,'char');% 保存下来
      %read terminating character
      %d=fread(instrObj,1,'char');
      % i=i+1;
      % end
       
      % Close, delete, and clear instrument connections.
      fclose(instrObj);
      delete(instrObj);
      clear instrObj;
      

       

  • 下面是个python的例子
    • python 中把 query 改成 instrObj.write 即可。请参看pyvisa的说明。SCPI语法和matlab中的是一致的。
  •  

划重点:

  • 一定要查看对应版本的固件。固件版本不同,支持的SCIP语句是不同的。网上有matlab控制矢网的例子,但是直接用会报错——比如undefined header。
  • VISA address 注意其格式。使用NI MAX的时候它的默认格式和实际的格式不一样,需要手动添加。
  • 读取数据保存的时候选择格式时需要注意。尝试过许多方法后,发现使用ascii码的方式是最简单直接的,保存的数据格式是使用的科学记数法。读取的数据直接用matlab导入即可。

 

 

你可能感兴趣的:(仪器测试自动化)