Labwindows_cvi is a program-controlled vector network analyzer based on C language

        Requirements: Use Labwindows_cvi software to develop the whole machine calibration system.

        Analysis: The whole machine calibration system is an integrated control system. The hardware that needs to be programmed mainly includes: vector network analyzer, scanning frame, turntable, timing controller, etc. A set of calibration process is formed through the control of these hardware. At present, These hardwares are controlled separately, because the scanning frame, turntable, and timing controller are non-standard instruments, so they will not be described in detail here. So this chapter mainly describes the Labwindows_cvi program-controlled vector network analyzer based on c language.

        This chapter mainly consists of several parts: connecting the instrument, sending instrument commands, and receiving instrument data.


1 Use of the VISA function library in Labwindows_cvi

        The VISA function library provided in Labwindows_cvi is an implementation of the standard VISA. Open the function panel selection window of the VISA function library, and you can see that it contains 3 subclasses.

        ● resource management;

        ● resource template;

        • Operations on specific resources.

        The resource management subclass implements the resource location and interface communication life cycle control in the VISA standard; the resource template subclass implements the attribute control, resource locking service and event mechanism in the VISA standard; the specific resource operation subclass implements the VISA standard Medium I/O control.

1.1 Resource Management

        Resource management includes resource location and life cycle control of communication. Resource location is a function viOpenDefaultRM, which realizes the location of all VISA resources on the machine, and returns the resource handle of ViSession type through the pointer. Communication lifecycle control for all specific resources is implemented through this handle. Communication lifecycle control includes opening resources, finding resources, and closing resources.

1.2 Resource template

        Resource template subclass provides VISA attribute control, resource locking service and event mechanism. This part of the content is not reflected in this article, so it will not be described in detail.

1.3 Specific Resource Operations

        Resource-specific operations include message-based communication functions and register-based communication functions.

1.3.1 Message-based communication functions

        Message-based instruments have separate processors that interpret received information and interpret the information into instructions or data. VISA provides formatted I/O and basic I/O functions for message-based communication.

        (1) Format I/O function

        Very similar to the standard C formatting functions, it can send or receive formatted data, including: viPrintf(), viScanf(), viQueryf(), viFlush().

        (2) Basic I/O functions

        Basic I/O supports synchronous and asynchronous data transmission in two ways.

        The function of synchronous transmission terminates the execution of other programs during the calling process, and does not return until the data is received or sent. The functions that support synchronous transmission include viWrite, viRead, viClear, viAssertTrigger and viReadSTB.

        The asynchronous transfer function returns immediately after being called, and an asynchronous read and write completion event (VI_EVENT_IO_COMPLETION) is generated after the data transfer is completed, and the VI_EVENT_IO_COMPLETION event can be processed through event queuing or callback function.

1.3.2 Register-based communication functions

        Do not elaborate here.

1.4 VISA common functions

1.4.1 Positioning of VISA resource manager - viOpenDefaultRM

        原型:ViStatus viOpenDefaultRM ( ViSession sesn);

        Description: This function is used to initialize a VISA resource manager, this function must be called before any other VISA functions.

Table 1-1 viOpenDefaultRM function parameters

parameter

describe

sound

Returns an individual VISA resource manager ID

Table 1-2 return value of viOpenDefaultRM function

identifier

value

describe

VI_SUCCESS

0

VISA Explorer initialized successfully.

VI_ERROR_ALLOC

Insufficient system resources.

VI_ERROR_INV_SETUP

The configuration file is invalid or does not exist.

VI_ERROR_SYSTEM_ERROR

VISA system initialization failed.

1.4.2 Connecting Instruments——viOpen

        原型:ViStatus viOpen ( ViSession sesn, ViRsrc name, ViAccessMode mode, ViUInt32 timeout, ViSession vi);

        Description: Connect to a specified device and return a connection identifier that can be used to call other functions.

Table 1-3 viOpen function parameters

parameter

direction

describe

sound

IN

VISA Explorer Logical Identifier

name

IN

address name

mode

IN

Connection opening method, which can be the following values:

VI_EXCLUSIVE_LOCK is opened exclusively;

VI_LOAD_CONFIG opens according to the external configuration file

VI_NULL normal multi-access open

timeout

IN

If it is opened exclusively, the parameter is an absolute time (in ms), and an error will be returned when the timeout expires. Other connection methods ignore this value.

vi

OUT

Returns the open connection logical identifier

Table 1-4 return value of viOpen function

identifier

value

describe

VI_SUCCESS

0

connection succeeded.

VI_SUCCESS_DEV_NPRESENT

The connection was successful, but there is no response from the specified address.

VI_WARN_CONFIG_NLOADED

The specified address does not exist or the address format is incorrect.

VI_ERROR_ALLOC

Insufficient system resources.

VI_ERROR_INTF_NUM_NCONFIG

The specified address is valid, but cannot be used.

VI_ERROR_INV_ACC_MODE

Illegal access mode.

VI_ERROR__INV_RSRC_NAME

Address name syntax error.

VI_ERROR__INV_SESSION

VI_ERROR__INV_OBJECT

invalid vi.

VI_ERROR_LIBRARY_NFOUND

The VISA library is not fully loaded.

VI_ERROR_NSUP_OPER

Explorer seen does not support this function.

VI_ERROR_RSRC_BUSY

seen is valid, but currently inaccessible.

VI_ERROR_RSRC_LOCKED

seen is monopolized.

VI_ERROR_RSRC_NFOUND

Insufficient information or resource does not exist.

VI_ERROR_TMO

0Xbfff0015

The operation timed out.

1.4.3 Sending Instrument Commands - viPrintf and viWrite

(1)viPrintf

        原型:ViStatus viPrintf ( ViSession vi, ViString writeFmt, ...);

        Description: Format a string and send the formatted string to the device. (String formatting can refer to the Format function in the CString class in MFC.)

        The viWrite and viPrintf functions cannot be used simultaneously on the same resource.

        The parameters of some VISA functions are variable (such as viPrintf, viScanf and viQueryf), which causes VB to fail to call. Users can use functions equivalent to these functions instead.

        The writeFmt parameter can contain ordinary characters, general formatting characters, and special characters. Ordinary characters (including spaces) are written as usual without any modification. A backslash (\) should be added in front of the special character; the format character is composed of a percent sign (%) and an operation flag, which will be introduced later.

Table 1-5 Special characters

\n

Sends an ASCII newline character, and the END flag is automatically sent along with it.

\r

Send an ASCII carriage return.

\t

Send an ASCII tab character.

\###

Send a specified octal number.

\”

Send an ASCII double quote.

\\

Send an ASCII backslash.

Table 1-6 viPrintf function parameters

parameter

direction

describe

vi

IN

object identifier

writeFmt

IN

format string

1-7 viPrintf函数返回值

标识符

描述

VI_SUCCESS

0

参数成功格式化。

VI_ERROR_ALLOC

内存不足。

VI_ERROR_INV_FMT

writeFmt包含无效格式化说明符。

VI_ERROR__INV_SESSION

VI_ERROR__INV_OBJECT

vi不能标志正确的连接。

VI_ERROR_IO

位置IO错误。

VI_ERROR_NSUP_FMT

writeFmt有不支持的格式说明符。

VI_ERROR_RSRC_LOCKED

vi被独占。

VI_ERROR_TMO

操作超时。

2)viWrite

        原型:ViStatus  viWrite (ViSession vi, ViBuf  buf, ViUInt32 cnt, ViPUInt32 retCnt);

        描述:同步写入数据。写入的数据存储在buf中。当数据写入完毕函数才返回。任何时间都只能存在一个同步写入。

如果retCnt的值为VI_NULL,将不返回写入数据长度值。

表1-8 viWrite函数参数

参数

方向

描述

vi

IN

对象标识符

buf

IN

写入数据存放地址。

cnt

IN

指定写入长度。

retCnt

OUT

实际写入长度,如果为VI_NULL表示不关心该值。

1-9 viWrite函数返回值

标识符

描述

VI_SUCCESS

0

成功读取,读取到END指示器结束。

VI_SUCCESS_MAX_CNT

成功读取,已经达到最大长度cnt。

VI_SUCCESS_TERM_CHAR

成功读取,读取到特定终止符。

VI_ERROR_ASRL_FRAMING

格式错误。

VI_ERROR_ASRL_OVERRUN

溢出错误。

VI_ERROR_ASRL_PARITY

同步错误。

VI_ERROR_BERR

总线错误。

VI_ERROR_CONN_LOST

连接丢失。

VI_ERROR_INV_SESSION

VI_ERROR_INV_OBJECT

vi不能标志正确的连接。

VI_ERROR_INV_SETUP

设置无效,不能执行操作。

VI_ERROR_IO

位置I/O是错误。

VI_ERROR_NCIC

非法控制器。

VI_ERROR_NLISTENERS

没有检测到听者。

VI_ERROR_NSUP_OPER

vi不支持此函数。

VI_ERROR_OUTP_PROT_VIOL

设备报告输出协议错误。

VI_ERROR_RAW_RD_PROT_VIOL

传输时读协议被破坏。

VI_ERROR_RAW_WD_PROT_VIOL

传输时写协议被破坏。

VI_ERROR_RSRC_LOCKED

vi被独占。

VI_ERROR_TMO

操作超时。

1.4.4 接收仪器数据——viScanf或viRead

(1)viScanf        

        原型:ViStatus  viScanf (ViSession vi, ViString readFmt,arg1,arg2, ...);

        描述:这操作从设备读取一个字符串,然后格式化后保存值arg变量中。格式化字符串包含说明符、空格字符和普通字符。

VISA中参数不确定的函数(viPrintf,viScanf,和viQueryf)不能被 VB调用。可以用功能相似的viVPrintf,viVScanf,和viVQueryf代替。

表1-10 viScanf函数参数

参数

方向

描述

vi

IN

对象标识符

readFmt

IN

格式化字符串。

表1-11 viScanf函数返回值

标识符

描述

VI_SUCCESS

0

成功读到数据并且格式化到arg参数里。

VI_ERROR_ALLOC

内存不足。

VI_ERROR_INV_FMT

readFmt包含无效格式化说明符。

VI_ERROR__INV_SESSION

VI_ERROR__INV_OBJECT

vi不能标志正确的连接。

VI_ERROR_IO

位置IO错误。

VI_ERROR_NSUP_FMT

readFmt有不支持的格式说明符。

VI_ERROR_RSRC_LOCKED

vi被独占。

VI_ERROR_TMO

操作超时。

(2)viRead

        原型:ViStatus  viRead (ViSession vi, ViPBuf buf, ViUInt32 cnt, ViPUInt32 retCnt);

        描述:同步读取数据。读取的数据存在buf中,当数据读取完毕函数才返回。任何时间都只能存在一个同步读取。遇到以下情况同步读取结束:

        1)收到END指示器;

        2)读取到终止符;

        3)读取的数据大小达到cnt的值。

        注意:必须设置读取终止符。

表1-12 viRead函数参数

参数

方向

描述

vi

IN

对象标识符

buf

OUT

返回数据存取地址。

cnt

IN

指定读取长度。

retCnt

OUT

实际读取长度,如果为VI_NULL表示不关心该值。

1-13 viRead函数返回值

标识符

描述

VI_SUCCESS

0

成功读取,读取到END指示器结束。

VI_SUCCESS_MAX_CNT

成功读取,已经达到最大长度cnt。

VI_SUCCESS_TERM_CHAR

成功读取,读取到特定终止符。

VI_ERROR_ASRL_FRAMING

构架错误。

VI_ERROR_ASRL_OVERRUN

溢出错误。

VI_ERROR_ASRL_PARITY

同步错误。

VI_ERROR_BERR

总线错误。

VI_ERROR_CONN_LOST

连接丢失。

VI_ERROR_INV_SESSION

VI_ERROR_INV_OBJECT

vi不能标志正确的连接。

VI_ERROR_INV_SETUP

设置无效,不能执行操作。

VI_ERROR_IO

位置I/O是错误。

VI_ERROR_NCIC

非法控制器。

VI_ERROR_NLISTENERS

没有检测到听者。

VI_ERROR_NSUP_OPER

vi不支持此函数。

VI_ERROR_OUTP_PROT_VIOL

设备记录一个输出协议错误。

VI_ERROR_RAW_RD_PROT_VIOL

传输时读协议被破坏。

VI_ERROR_RAW_WD_PROT_VIOL

传输时写协议被破坏。

VI_ERROR_RSRC_LOCKED

vi被独占。

VI_ERROR_TMO

操作超时。

1.4.5关闭资源管理器和设备连接——viClose

        原型:ViStatus  viClose (ViObject vi);

        描述:关闭一个资源管理器或者设备连接,并释放内存。

表1-14 viClose函数参数

参数

方向

描述

vi

IN

需要关闭的对象。

表1-15 viClose函数返回值

标识符

描述

VI_SUCCESS

0

关闭成功。

VI_WARN_NULL_OBJECT

要关闭的对象时空对象。

VI_ERROR_CLOSING_FAILED

vi不能标识正当对话通道。

VI_ERROR__INV_SESSION

VI_ERROR__INV_OBJECT

无法释放与该对话通道相关联的内存数据结构。

2   Labwindows_cvi基于c语言编程

        案例:以3672系列的矢量网络分析仪为例,实现矢网连接,发送命令到矢网,接收矢网数据,关闭矢网连接等功能。 

2.1界面设计

        界面主要包含矢网连接、发送矢网重置命令、接收矢网数据、断开矢网连接、消息提示框等功能。

图2-1 demo界面

 

2.2实现代码

        所有功能实现代码如下:

#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>		
#include <userint.h>
#include <visa.h> 
#include "PNA_CONECT.h"

static int pnaConect;

ViSession  defaultRM ,vi; 
unsigned char Data_Mag[5000];  // 幅度测试数据 
char c_currentMessage[300];   // 提示消息

void GetDateTime(void);
/*==========VISA资源管理器定位==========*/    
int RMConnect(void);  

int main (int argc, char *argv[])
{
	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	if ((pnaConect = LoadPanel (0, "PNA_CONECT.uir", PNA_CONECT)) < 0)
		return -1;
	DisplayPanel (pnaConect);
	RunUserInterface ();
	DiscardPanel (pnaConect);
	return 0;
}

int CVICALLBACK PNA_CONECT_CALLBACK (int panel, int event, void *callbackData,
									 int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_GOT_FOCUS:

			break;
		case EVENT_LOST_FOCUS:

			break;
		case EVENT_CLOSE:
			 QuitUserInterface (0);
			break;
	}
	return 0;
}

/*==========VISA资源管理器定位==========*/   
int RMConnect()
{
	 	int error,result; 
		error=	viOpenDefaultRM(&defaultRM);
		while(error)
		{
			result = GenericMessagePopup("Error", "VISA资源管理器定位失败!", "重试", "忽略", "", 0, 0, 0, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_NO_CTRL);
			if(result==1)
			{
				 error=	viOpenDefaultRM(&defaultRM);
			}
			else
				break;
		}
	    return defaultRM;
}


/* 连接矢网 */ 
int CVICALLBACK ConnectPna_CALLBACK (int panel, int control, int event,
									 void *callbackData, int eventData1, int eventData2)
{
	int error, select; 
	switch (event)
	{
		case EVENT_COMMIT:
			   
			RMConnect();
			error =  viOpen(defaultRM, "TCPIP0::192.168.1.41::5025::SOCKET", VI_NULL,VI_NULL, &vi); //3672B
			
			if(error==0)
			{
					  GetDateTime()  ;
					  strcat(c_currentMessage," 矢网连接成功!\n");
					  SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage); 
			}
			while ( error )
			{
				select= GenericMessagePopup ("Error", "矢网连接错误,请重试或检查连接状态", "重试", "忽略", "", 0, 0, 0, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_BTN1, VAL_GENERIC_POPUP_NO_CTRL);
			
				if ( select == 1 )
				{
				error =  viOpen(defaultRM, "TCPIP0::192.168.1.41::1024::SOCKET", VI_NULL,VI_NULL, &vi);
				}
				else 
				{
					GetDateTime()  ;
					strcat(c_currentMessage," 矢网连接错误,请重试或检查连接状态!\n");		
					SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage);
				}
				break; 
			}
	}
	return vi; 
}

//发送矢网命令
int CVICALLBACK SEND_RST_CALLBACK (int panel, int control, int event,
								   void *callbackData, int eventData1, int eventData2)
{  

	switch (event)
	{
		case EVENT_COMMIT:
			
			// 矢网重置
			viPrintf(vi,"*RST\n"); 	
			// 删除所有测试设置  
			viPrintf(vi,"CALC:PAR:DEL:ALL\n"); 	
			 // 关闭所有窗口
			viPrintf(vi,"DISPlay:WIND OFF\n");
			 // 打开新建窗口  
	  		viPrintf(vi,"DISPlay:WIND1 ON\n");
			 // 定义测试内容  
			 	
	         // 定义测试内容 
			 viPrintf(vi,"CALC1:PAR:DEF:EXT 'mytrace','S21'\n");
			 
			 // 显示测试迹线 
			 viPrintf(vi,"DISPlay:WIND1:TRAC1:FEED 'mytrace'\n");    
			 
       	     // 选定测试迹线     
			 viPrintf(vi,"CALC1:PAR:SEL 'mytrace'\n");	
			 
             // 设置测试类型(对数幅度) 
			 viPrintf(vi,"CALC1:FORM MLOG\n");
			 
			  // 设置扫描类型为段扫描 */       
			  viPrintf(vi,"SENS1:SWE:TYPE SEGM\n");	
			  
			 // 段扫描开始频率 */   
			 viPrintf(vi,"SENS1:SEGM:FREQ:STAR 12GHz\n");
			 
			 // 段扫描终止频率 */ 
			viPrintf(vi, "SENS1:SEGM:FREQ:STOP 12GHz\n"); 
			
			 // 段扫描频率点数 */  
			viPrintf(vi,"SENS1:SEGM:SWE:POIN 1\n");  		
			 
		    GetDateTime();
			strcat(c_currentMessage," 矢网命令已发送!\n");		
			SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage);  
		 
			 
			break;
	}
	return 0;
}
//接收矢网数据
int CVICALLBACK RECEIVE_DATA_CALLBACK (int panel, int control, int event,
									   void *callbackData, int eventData1, int eventData2)
{
	// 频点个数
	int  length=1;  

	switch (event)
	{
		case EVENT_COMMIT:
	
			// 从通道 1 所选测量轨迹中读取格式化数据。 
			viPrintf(vi,"CALC1:DATA? FDATA\n");			
			 
			// 读取矢网测试数据	 length:频点个数
			viRead (vi,Data_Mag ,length*20,VI_NULL);
		
			GetDateTime();
			strcat(c_currentMessage,"  ");
			strcat(c_currentMessage,Data_Mag);	
			//将读取的数据显示	
			SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage);  
			
			GetDateTime();
			strcat(c_currentMessage," 数据读取完成!\n");		
			SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage);  
		
			break;
	}
	return 0;
}
//释放矢网资源 
int CVICALLBACK DISCONNECT_CALLBACK (int panel, int control, int event,
									 void *callbackData, int eventData1, int eventData2)
{
 
	switch (event)
	{
		case EVENT_COMMIT:
		
			viClose(vi); 
			viClose(defaultRM);
		
			GetDateTime();
			strcat(c_currentMessage," 已经释放矢网资源并关闭!\n");		
			SetCtrlVal (panel, PNA_CONECT_TEXTBOX, c_currentMessage);  
			break;
	}
	return 0;
}

//获取系统日期时间
void GetDateTime()
{
	int i_hour, i_minute , i_second ;
	int i_month , i_day , i_year; 
	char c_hour[9], c_minute[3], c_second[3];
	char c_year[10], c_month[3], c_day[3];
	char c_diagonal[2]="/", c_colon[2]=":",c_space[2]=" "; 
	char c_date[19], c_currenttime[9];
   
	
	//获取系统时间
	GetSystemTime(&i_hour,&i_minute , &i_second   );
	//获取系统日期
	GetSystemDate(&i_month,&i_day, &i_year);   
	
	//将int类型数据转为字符串数据
	sprintf(c_hour,"%d",i_hour);
	sprintf(c_minute,"%d",i_minute); 
	sprintf(c_second,"%d",i_second); 
			
	sprintf(c_year,"%d",i_year);
	sprintf(c_month,"%d",i_month); 
	sprintf(c_day,"%d",i_day);
	
	//拼接字符串
	    strcpy(c_date,strcat(strcat(strcat(strcat(c_year,c_diagonal),c_month),c_diagonal),c_day));
	strcpy(c_currenttime,strcat(strcat(strcat(strcat(c_hour,c_colon),c_minute),c_colon),c_second));   
	strcpy(c_currentMessage,strcat(strcat(c_date,c_space),c_currenttime));
}

2.3功能显示

        连接的矢量网络分析仪型号时AV3672B矢量网络分析仪,展示功能如下:

图2-2 demo实现效果

 

Guess you like

Origin blog.csdn.net/caimeihua5369/article/details/119581479