NI VISA 串口读写示例

/********************************************************************/
/*                Read and Write to a Serial Instrument             */
/*                                                                  */
/* This code demonstrates sending synchronous read & write commands */
/* through the serial port using VISA.                              */
/* The example writes the "*IDN?\n" string to the serial port (COM1)*/
/* and attempts to read back a result using the write and read      */
/* functions.                                                       */
/*                                                                  */
/* The general flow of the code is                                  */
/*    Open Resource Manager                                         */
/*    Open VISA Session to an Instrument                            */
/*    Configure the Serial Port                                     */
/*    Write the Identification Query Using viWrite                  */
/*    Try to Read a Response With viRead                            */
/*    Close the VISA Session                                        */
/********************************************************************/

#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
/* Functions like strcpy are technically not secure because they do */
/* not contain a 'length'. But we disable this warning for the VISA */
/* examples since we never copy more than the actual buffer size.   */
#define _CRT_SECURE_NO_DEPRECATE
#endif

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

#include "visa.h"

static ViSession defaultRM;     
static ViSession instr;     
static ViUInt32 retCount;
static ViUInt32 writeCount;
static ViStatus status;
static unsigned char buffer[100];
static char stringinput[512];


int main(void)
{
   /*
    * First we must call viOpenDefaultRM to get the manager
    * handle.  We will store this handle in defaultRM.
    */
   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 VISA session to the serial port (COM1).
    * We must use the handle from viOpenDefaultRM and we must   
    * also use a string that indicates which instrument to open.  This
    * is called the instrument descriptor.  The format for this string
    * can be found in the function panel by right clicking on the 
    * descriptor parameter. After opening a session to the
    * device, we will get a handle to the instrument which we 
    * will use in later VISA functions.  The AccessMode and Timeout
    * parameters in this function are reserved for future
    * functionality.  These two parameters are given the value VI_NULL.
    */
   status = viOpen (defaultRM, "ASRL1::INSTR", VI_NULL, VI_NULL, &instr);
   if (status < VI_SUCCESS) 
   {
      printf ("Cannot open a session to the device.\n");
      goto Close;
   }
  
   /* 
    * At this point we now have a session open to the serial instrument. 
    * Now we need to configure the serial port:
    */                                      
      
   /* Set the timeout to 5 seconds (5000 milliseconds). */
   status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 5000);
  
   /* Set the baud rate to 4800 (default is 9600). */
   status = viSetAttribute (instr, VI_ATTR_ASRL_BAUD, 4800);
  
   /* Set the number of data bits contained in each frame (from 5 to 8). 
    * The data bits for  each frame are located in the low-order bits of
    * every byte stored in memory.    
    */
   status = viSetAttribute (instr, VI_ATTR_ASRL_DATA_BITS, 8);
  
   /* Specify parity. Options: 
    * VI_ASRL_PAR_NONE  - No parity bit exists, 
    * VI_ASRL_PAR_ODD   - Odd parity should be used, 
    * VI_ASRL_PAR_EVEN  - Even parity should be used,
    * VI_ASRL_PAR_MARK  - Parity bit exists and is always 1,
    * VI_ASRL_PAR_SPACE - Parity bit exists and is always 0.
    */
   status = viSetAttribute (instr, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE);
  
   /* Specify stop bit. Options:
    * VI_ASRL_STOP_ONE   - 1 stop bit is used per frame,
    * VI_ASRL_STOP_ONE_5 - 1.5 stop bits are used per frame,
    * VI_ASRL_STOP_TWO   - 2 stop bits are used per frame.
    */
   status = viSetAttribute (instr, VI_ATTR_ASRL_STOP_BITS, VI_ASRL_STOP_ONE);
  
    /* Specify that the read operation should terminate when a termination 
     * character is received.
     */
   status = viSetAttribute (instr, VI_ATTR_TERMCHAR_EN, VI_TRUE); 
 
    /* Set the termination character to 0xA                            
     */
   status = viSetAttribute (instr, VI_ATTR_TERMCHAR, 0xA);
  
 
    /* We will use the viWrite function to send the device the string "*IDN?\n",
     * asking for the device's identification.  
    */
   strcpy (stringinput,"*IDN?\n");
   status = viWrite (instr, (ViBuf)stringinput, (ViUInt32)strlen(stringinput), &writeCount);
   if (status < VI_SUCCESS)    
   {
      printf ("Error writing to the device.\n");
      goto Close;
   }
    
    /*
    * Now we will attempt to read back a response from the device to
    * the identification query that was sent.  We will use the viRead
    * function to acquire the data.  We will try to read back 100 bytes.
    * This function will stop reading if it finds the termination character
    * before it reads 100 bytes.
    * After the data has been read the response is displayed.
    */
   status = viRead (instr, buffer, 100, &retCount);
   if (status < VI_SUCCESS) 
   {
      printf ("Error reading a response from the device.\n");
   }
   else
   {
      printf ("\nData read: %*s\n", retCount, buffer);
   }


   /*
    * Now we will close the session to the instrument using
    * viClose. This operation frees all system resources.                     
    */
Close:
   status = viClose (instr);
   status = viClose (defaultRM);
   printf ("Hit enter to continue.");
   fflush (stdin);
   getchar();

   return 0;
}

 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值