NI VISA Tcpip 操作示例

/*********************************************************************/
/* This example demonstrates opening a simple TCPIP connection and   */
/* does a read and checks a few properties.							 */
/*                                                                   */
/* The general flow of the code is                                   */
/*      Open Resource Manager                                        */
/*      Open a session to the TCP/IP site at NI                      */
/*      Perform a read, and check properties                         */
/*      Close all VISA Sessions                                      */
/*********************************************************************/


#include <stdio.h>
#include <stdlib.h>

#include "visa.h"

static char outputBuffer[VI_FIND_BUFLEN];
static ViSession defaultRM, instr;
static ViStatus status;
static ViUInt32 count;
static ViUInt16 portNo;

int main(void)
{
   /* First we will need to open the default resource manager. */
   status = viOpenDefaultRM (&defaultRM);
   if (status < VI_SUCCESS)
   {
      printf("Could not open a session to the VISA Resource Manager!\n");
      exit (EXIT_FAILURE);
   }  

   /* Now we will open a session via TCP/IP to ni.com */
   status = viOpen (defaultRM, "TCPIP0::ftp.ni.com::21::SOCKET", VI_NULL, VI_NULL, &instr);
   if (status < VI_SUCCESS)
   {
      printf ("An error occurred opening the session to TCPIP0::ftp.ni.com::21::SOCKET\n");
      viClose(defaultRM);
      exit (EXIT_FAILURE);
   }
   
   viSetAttribute (instr, VI_ATTR_TCPIP_NODELAY, VI_TRUE);
   
   status = viRead (instr, (ViBuf)outputBuffer, 25, &count);
   if (status < VI_SUCCESS)
   {
      printf("viRead failed with error code %x \n",status);
      viClose(defaultRM);
      exit (EXIT_FAILURE);
   }

   printf("The server response is:\n %s\n\n",outputBuffer);
   status = viGetAttribute (instr, VI_ATTR_TCPIP_ADDR, outputBuffer);
   printf(" Address:  %s\n",outputBuffer);
   
   status = viGetAttribute (instr, VI_ATTR_TCPIP_HOSTNAME, outputBuffer);
   printf(" Host Name:  %s\n",outputBuffer);
   
   status = viGetAttribute (instr, VI_ATTR_TCPIP_PORT, &portNo);
   printf(" Port:  %d\n",portNo);
   
   status = viGetAttribute (instr, VI_ATTR_RSRC_CLASS, outputBuffer);
   printf(" Resource Class:  %s\n",outputBuffer);

   status = viClose (instr);
   status = viClose (defaultRM);
   printf ("\nHit enter to continue.");
   fflush(stdin);
   getchar();

   return 0;  
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值