RIGOL Technologies

Welcome, Guest Login

Support Center

C#.NET basic instrument communication example

Last Updated: Dec 30, 2015 02:55PM PST
C#.NET 2010 example: VISABasic

We have created a small example to show how to remotely connect to an instrument using NI VISA.
This example allows you to establish a message-based VISA session with the instrument, send commands, and receive data from an instrument.
It utilizes .NET framework 4.0 and National Instruments VISA .
Here is the design of the form:

Added Controls Qty 1 Combo box: 
cboVISARsrc - Holds instrument VISA resource ID.
Qty 4 Buttons:   
btnOpen - Opens VISA session btnClose - Close VISA session btnWrite – Writes to instrument btnRead – Reads from instrument Qty 2 Text box
txtWrite– Textbox to hold command to write txtRead– Textbox to show read data
Code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using NationalInstruments.VisaNS;
// VISABasic is a small application created to give
// users an example of VISA based instrument communications.
//
// This app utilizes .NET Framework 4.0 and the associated National Instruments VISA //Common and VISANS // references.
//
// 1) Enter VISA resource ID
// 2) Press Open button to created new message based VISA session // 3) Enter instrument specific command in Command Window
// 4) Press Send Button
// 5) If the command is a query (terminating in a '?'), press Read to return //    instrument response
//
// This application is provided as-is. No support or warranty is expressed or implied. //
// Author: JC
// Company: Rigol Technologies, North America
// Date: 09.09.2011
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private MessageBasedSession mbSession; //Create Message based session
                        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnOpen_Click(object sender, EventArgs e) //Open a VISA Session
        {
            string strVISARsrc = cboVISARsrc.Text; //Get VISA resource ID from combobox
       
                    try                     {
 mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(strVISARsrc);
//Instantiate and open a Message Based VISA Session
       
                    }
                    catch(InvalidCastException)
                    {
                    MessageBox.Show("Resource selected must be a message-based session");
                    }
                    catch(Exception exp)
                    {
                    MessageBox.Show(exp.Message);
                    }
        }
       
        private void btnClose_Click(object sender, EventArgs e) //Close VISA session
        {             try             {
                mbSession.Dispose(); //
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }         }
        private void btnWrite_Click(object sender, EventArgs e)  //Write command to VISA resource
        {
            string strWrite = txtWrite.Text; //Get command from text box
            try             {
                mbSession.Write(strWrite); //Send Write command to VISA resource
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }         }         private void btnRead_Click(object sender, EventArgs e)
        //Read command from VISA resource
        {
            string strRead = null;
            try             {
                strRead = mbSession.ReadString(); //Read string from VISA resource   txtRead.Text = strRead;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }
    }


Figure 1: Project properties and references.
 

Login above for personalized content

Go to RIGOLna.com

License Activation

Firmware Updates

Product Registration

Contact Us

f14c465fd642ac49fa5a4bd437615e15@rigol.desk-mail.com
https://cdn.desk.com/
false
desk
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete