Author Topic: VB .NET 2019 and NI-VISA Example Needed  (Read 363 times)

0 Members and 1 Guest are viewing this topic.

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
VB .NET 2019 and NI-VISA Example Needed
« on: April 10, 2020, 03:12:21 am »
I am teaching myself VS 2019 Community with VB .NET using NI-VISA to communicate with a Siglent device.  All of the examples I can find are in C# or old VB6.  I am having difficultty with the NI-VISA ResourceManager class.  I have found older examples that use

Code: [Select]
Public Class Form1
    Dim result As String
    Dim dut_tcpip As TcpipSession
    Dim visa_string As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        visa_string = TextBox1.Text
        dut_tcpip = ResourceManager.GetLocalManager.Open(visa_string)

        visa_string = TextBox1.Text
        dut_write("*IDN?")
        dut_read()
        MsgBox(result)

    End Sub
 

The problem is the latest version of NI-VISA ResourceManager does not list GetLocalManager.  I have searched through current and previous NI-VISA documents and cannot find how this changed or to what.  Can someone help me get past this first door?

Thank you in advance and stay safe and healthy
 
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #1 on: April 10, 2020, 03:27:15 am »
Just a quick check...do you have:

Code: [Select]
Imports NationalInstruments.VisaNS
at the top of the file?

Then try

Code: [Select]
Public Class Form1

    Dim mbSession As MessageBasedSession
    Dim visa_string As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        visa_string = TextBox1.Text
        mbSession = CType(ResourceManager.GetLocalManager().Open(visa_string ), MessageBasedSession)

        Dim responseString As String = mbSession.Query("*IDN?")
        MsgBox(responseString)

        mbSession.Dispose()

    End Sub

From the documentation here:  https://www.ni.com/en-us/support/documentation/supplemental/17/archived--c--and-visual-basic--net-instrument-control.html

You can then use the mbSession object to transfer data and get the response.

** NOTE  I realized this is from the old library....Let me see if I can find the new one **
« Last Edit: April 10, 2020, 03:40:17 am by BroMarduk »
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #2 on: April 10, 2020, 03:37:50 am »
Just a quick check...do you have:

Code: [Select]
Imports NationalInstruments.VisaNS
at the top of the file?

Thanks for the reply.  VisaNS is no longer in the current NI-VISA download.  I believe the VisaNS version had  'GetLocalManager' under ResourceManager.   I have added the new NI version under References in Visual Studio and in the code I have:

Code: [Select]
Imports NationalInstruments.Visa
Imports Ivi.Visa

I cannot find any examples in VB .NET with the new version of NI-VISA Resource Manager... I have Googled literally for a few hours looking
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #3 on: April 10, 2020, 03:40:46 am »
LOL... I caught that a bit late. 

Let me see...
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #4 on: April 10, 2020, 03:41:24 am »
Do you have a C# example I can translate?
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #5 on: April 10, 2020, 03:50:34 am »
First, Many many thanks for your kind help.  Yes, I think I can find a C example from NI-VISA.  Gi8ve me a few minutes. In the meantime I did your code:

Code: [Select]
Imports NationalInstruments.Visa
Imports Ivi.Visa


Public Class Form1
    Dim mbSession As MessageBasedSession
    Dim visa_string As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        visa_string = TextBox1.Text
        mbSession = CType(ResourceManager.GetLocalManager().Open(visa_string), MessageBasedSession)

        Dim responseString As String = mbSession.Query("*IDN?")
        MsgBox(responseString)

        mbSession.Dispose()
    End Sub
End Class

I get errors that GetLocalManager is not a member of ResourceManager and that Query is not a member of mbSession.


Let me get that C example
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #6 on: April 10, 2020, 03:58:23 am »
Here is a read/write example from NI-VISA with C NET.  Its probably a lot more than you need

Code: [Select]
using System;
using System.Windows.Forms;
using NationalInstruments.Visa;

namespace NationalInstruments.Examples.SimpleReadWrite
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private MessageBasedSession mbSession;
        private string lastResourceString = null;
        private System.Windows.Forms.TextBox writeTextBox;
        private System.Windows.Forms.TextBox readTextBox;
        private System.Windows.Forms.Button queryButton;
        private System.Windows.Forms.Button writeButton;
        private System.Windows.Forms.Button readButton;
        private System.Windows.Forms.Button openSessionButton;
        private System.Windows.Forms.Button clearButton;
        private System.Windows.Forms.Button closeSessionButton;
        private System.Windows.Forms.Label stringToWriteLabel;
        private System.Windows.Forms.Label stringToReadLabel;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            SetupControlState(false);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(mbSession != null)
                {
                    mbSession.Dispose();
                }
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.queryButton = new System.Windows.Forms.Button();
            this.writeButton = new System.Windows.Forms.Button();
            this.readButton = new System.Windows.Forms.Button();
            this.openSessionButton = new System.Windows.Forms.Button();
            this.writeTextBox = new System.Windows.Forms.TextBox();
            this.readTextBox = new System.Windows.Forms.TextBox();
            this.clearButton = new System.Windows.Forms.Button();
            this.closeSessionButton = new System.Windows.Forms.Button();
            this.stringToWriteLabel = new System.Windows.Forms.Label();
            this.stringToReadLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // queryButton
            //
            this.queryButton.Location = new System.Drawing.Point(5, 83);
            this.queryButton.Name = "queryButton";
            this.queryButton.Size = new System.Drawing.Size(74, 23);
            this.queryButton.TabIndex = 3;
            this.queryButton.Text = "Query";
            this.queryButton.Click += new System.EventHandler(this.query_Click);
            //
            // writeButton
            //
            this.writeButton.Location = new System.Drawing.Point(79, 83);
            this.writeButton.Name = "writeButton";
            this.writeButton.Size = new System.Drawing.Size(74, 23);
            this.writeButton.TabIndex = 4;
            this.writeButton.Text = "Write";
            this.writeButton.Click += new System.EventHandler(this.write_Click);
            //
            // readButton
            //
            this.readButton.Location = new System.Drawing.Point(153, 83);
            this.readButton.Name = "readButton";
            this.readButton.Size = new System.Drawing.Size(74, 23);
            this.readButton.TabIndex = 5;
            this.readButton.Text = "Read";
            this.readButton.Click += new System.EventHandler(this.read_Click);
            //
            // openSessionButton
            //
            this.openSessionButton.Location = new System.Drawing.Point(5, 5);
            this.openSessionButton.Name = "openSessionButton";
            this.openSessionButton.Size = new System.Drawing.Size(92, 22);
            this.openSessionButton.TabIndex = 0;
            this.openSessionButton.Text = "Open Session";
            this.openSessionButton.Click += new System.EventHandler(this.openSession_Click);
            //
            // writeTextBox
            //
            this.writeTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.writeTextBox.Location = new System.Drawing.Point(5, 54);
            this.writeTextBox.Name = "writeTextBox";
            this.writeTextBox.Size = new System.Drawing.Size(275, 20);
            this.writeTextBox.TabIndex = 2;
            this.writeTextBox.Text = "*IDN?\\n";
            //
            // readTextBox
            //
            this.readTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.readTextBox.Location = new System.Drawing.Point(5, 136);
            this.readTextBox.Multiline = true;
            this.readTextBox.Name = "readTextBox";
            this.readTextBox.ReadOnly = true;
            this.readTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.readTextBox.Size = new System.Drawing.Size(275, 119);
            this.readTextBox.TabIndex = 6;
            this.readTextBox.TabStop = false;
            //
            // clearButton
            //
            this.clearButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.clearButton.Location = new System.Drawing.Point(6, 257);
            this.clearButton.Name = "clearButton";
            this.clearButton.Size = new System.Drawing.Size(275, 24);
            this.clearButton.TabIndex = 7;
            this.clearButton.Text = "Clear";
            this.clearButton.Click += new System.EventHandler(this.clear_Click);
            //
            // closeSessionButton
            //
            this.closeSessionButton.Location = new System.Drawing.Point(97, 5);
            this.closeSessionButton.Name = "closeSessionButton";
            this.closeSessionButton.Size = new System.Drawing.Size(92, 22);
            this.closeSessionButton.TabIndex = 1;
            this.closeSessionButton.Text = "Close Session";
            this.closeSessionButton.Click += new System.EventHandler(this.closeSession_Click);
            //
            // stringToWriteLabel
            //
            this.stringToWriteLabel.Location = new System.Drawing.Point(5, 34);
            this.stringToWriteLabel.Name = "stringToWriteLabel";
            this.stringToWriteLabel.Size = new System.Drawing.Size(91, 14);
            this.stringToWriteLabel.TabIndex = 8;
            this.stringToWriteLabel.Text = "String to Write:";
            //
            // stringToReadLabel
            //
            this.stringToReadLabel.Location = new System.Drawing.Point(5, 117);
            this.stringToReadLabel.Name = "stringToReadLabel";
            this.stringToReadLabel.Size = new System.Drawing.Size(101, 15);
            this.stringToReadLabel.TabIndex = 9;
            this.stringToReadLabel.Text = "String Read:";
            //
            // MainForm
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(287, 289);
            this.Controls.Add(this.stringToReadLabel);
            this.Controls.Add(this.stringToWriteLabel);
            this.Controls.Add(this.closeSessionButton);
            this.Controls.Add(this.clearButton);
            this.Controls.Add(this.readTextBox);
            this.Controls.Add(this.writeTextBox);
            this.Controls.Add(this.openSessionButton);
            this.Controls.Add(this.readButton);
            this.Controls.Add(this.writeButton);
            this.Controls.Add(this.queryButton);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimumSize = new System.Drawing.Size(295, 316);
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Simple Read/Write";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new MainForm());
        }

        private void openSession_Click(object sender, System.EventArgs e)
        {
            using (SelectResource sr = new SelectResource())
            {
                if(lastResourceString != null)
                {
                    sr.ResourceName = lastResourceString;
                }
                DialogResult result = sr.ShowDialog(this);
                if(result == DialogResult.OK)
                {
                    lastResourceString = sr.ResourceName;
                    Cursor.Current = Cursors.WaitCursor;
                    using (var rmSession = new ResourceManager())
                    {
                        try
                        {
                            mbSession = (MessageBasedSession)rmSession.Open(sr.ResourceName);
                            SetupControlState(true);
                        }
                        catch (InvalidCastException)
                        {
                            MessageBox.Show("Resource selected must be a message-based session");
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show(exp.Message);
                        }
                        finally
                        {
                            Cursor.Current = Cursors.Default;
                        }
                    }
                }
            }
        }

        private void closeSession_Click(object sender, System.EventArgs e)
        {
            SetupControlState(false);
            mbSession.Dispose();
        }

        private void query_Click(object sender, System.EventArgs e)
        {
            readTextBox.Text = String.Empty;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                string textToWrite = ReplaceCommonEscapeSequences(writeTextBox.Text);
                mbSession.RawIO.Write(textToWrite);
                readTextBox.Text = InsertCommonEscapeSequences(mbSession.RawIO.ReadString());
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void write_Click(object sender, System.EventArgs e)
        {
            try
            {
                string textToWrite = ReplaceCommonEscapeSequences(writeTextBox.Text);
                mbSession.RawIO.Write(textToWrite);
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

        private void read_Click(object sender, System.EventArgs e)
        {
            readTextBox.Text = String.Empty;
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                readTextBox.Text = InsertCommonEscapeSequences(mbSession.RawIO.ReadString());
            }
            catch(Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void clear_Click(object sender, System.EventArgs e)
        {
            readTextBox.Text = String.Empty;
        }

        private void SetupControlState(bool isSessionOpen)
        {
            openSessionButton.Enabled = !isSessionOpen;
            closeSessionButton.Enabled = isSessionOpen;
            queryButton.Enabled = isSessionOpen;
            writeButton.Enabled = isSessionOpen;
            readButton.Enabled = isSessionOpen;
            writeTextBox.Enabled = isSessionOpen;
            clearButton.Enabled = isSessionOpen;
            if(isSessionOpen)
            {
                readTextBox.Text = String.Empty;
                writeTextBox.Focus();
            }
        }

        private string ReplaceCommonEscapeSequences(string s)
        {
            return s.Replace("\\n", "\n").Replace("\\r", "\r");
        }

        private string InsertCommonEscapeSequences(string s)
        {
            return s.Replace("\n", "\\n").Replace("\r", "\\r");
        }
    }
}

 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #7 on: April 10, 2020, 04:05:08 am »
Try it without the GetLocalManager.: 

Code: [Select]
Imports NationalInstruments.Visa
Imports Ivi.Visa

Public Class Form1
    Dim result As String
    Dim dut_tcpip As TcpipSession
    Dim visa_string As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        visa_string = TextBox1.Text
        dut_tcpip = ResourceManager.Open(visa_string)

        dut_tcpip.RawIO.Write("*IDN?" + vbLf)
        MsgBox(dut_tcpip.RawIO.ReadString())

    End Sub
« Last Edit: April 10, 2020, 04:15:25 am by BroMarduk »
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #8 on: April 10, 2020, 04:08:31 am »
The new line character may be wrong...You can try it without the + VbLf as well.
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #9 on: April 10, 2020, 04:21:21 am »
I tried it and getting an error for line:

Code: [Select]
dut_tcpip = ResourceManager.GetLocalManager.Open(visa_string)

Error says "Reference to a non-shared member requires an object reference"
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #10 on: April 10, 2020, 05:07:09 am »
Looks like you have to create an instance of ResourceManager class:

Code: [Select]
Imports NationalInstruments.Visa
Imports Ivi.Visa

Public Class Form1
    Dim result As String
    Dim dut_tcpip As TcpipSession
    Dim visa_string As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        visa_string = TextBox1.Text

        Using rm As New ResourceManager
                dut_tcpip = rm.Open(visa_string)
        End Using

        dut_tcpip.RawIO.Write("*IDN?" + vbLf)
        MsgBox(dut_tcpip.RawIO.ReadString())

    End Sub
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #11 on: April 10, 2020, 01:02:16 pm »
No errors building but getting exception on the Open line.  Reading NI-VISA changes from the now legacy VisaNS:

Quote
However, because the new API implements interfaces specified by the IVI specification and uses several types defined in that specification, the actual members of these Session-derived classes are different from those in VisaNS and therefore will require manual changes to your applications to migrate from VisaNS to NI-VISA .NET. In some cases, such as the formatted I/O functionality, the API is significantly different from VisaNS and may require larger changes for migration.

Some features supported by the VisaNS API are not supported or are supported differently in the NI-VISA .NET API. Some of the major differences are described below.

Message-based I/O operations are provided in a different way
Simple message-based operations, such as ReadString or Write, that are implemented by some Session-derived classes in VisaNS are now provided by the RawIO property of the Ivi.Visa. IMessageBasedSession interface that some Session-derived classes implement. The RawIO property is of a type that implements the Ivi.Visa.IMessageBasedRawIO interface.

Similarly, the formatted message-based operations that are implemented by MessageBasedSessionReader and MessageBasedSessionWriter in VisaNS are now provided by the FormattedIO property of the Ivi.Visa.IMessageBasedSession interface that some Session-derived classes implement. The FormattedIO property is of a type that implements the Ivi.Visa.IMessageBasedFormattedIO interface.
Format specifiers used for formatted I/O are different
The formatted I/O methods in VisaNS support format strings similar to those used by the String.Format method. NI-VISA .NET, however, supports format strings with format specifiers similar to those used by VISA viPrintf and viScanf functions. For more details about the FormattedIO methods and supported format specifiers, see the description of Ivi.Visa.IMessageBasedFormattedIO interface in the VPP-4.3.6 specification.
ResourceManager class is disposable
The ResourceManager class is used differently. To get an instance of a ResourceManager object, construct a new instance using the public constructor ResourceManager(). The ResourceManager object is disposable and should be disposed once no longer needed. Note that any Session-derived object obtained by using the Open method of a ResourceManager remains valid after the ResourceManager object is disposed.

You would think that NI would provide at least some examples of code with these changes.
 

Offline NY2KW

  • Regular Contributor
  • *
  • Posts: 112
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #12 on: April 10, 2020, 02:56:08 pm »
Thank you again for all your help.  I found an obscure thread in an NI Forum on Instrument control discussing the changes to ResourceManager from legacy VisaNS to NI-VISA.   You were 100% in your suggestions.  In console style program, the following returns the Instrument type, name and serial number in a Command window.  Now moving to Windows Form

Code: [Select]
Imports System
Imports System.Collections.Generic
Imports Ivi.Visa
Imports NationalInstruments.Visa

Module Module1
   Dim visa_string = "USB?*"
   
   Sub Main()
        Using rm = New ResourceManager()
            Try
                Dim resources = rm.Find(visa_string)
                'Dim resources As IEnumerable(Of String) = rm.Find(visa_string)
                For Each s As String In resources
                    Dim parseResult As ParseResult = rm.Parse(s)
                    System.Console.WriteLine(s, parseResult.InterfaceType)
                Next
            Catch ex As Exception
                System.Console.WriteLine("Exception" + ex.Message)
            End Try

        End Using
       
        System.Console.ReadLine()
    End Sub

I didn't understand the commented line why IEnumerable was used so I simplified it and still worked fine.   Now onto using VS to put this and more into a Windows Form app.

Will keep you posted.
 

Offline BroMarduk

  • Supporter
  • ****
  • Posts: 60
  • Country: us
Re: VB .NET 2019 and NI-VISA Example Needed
« Reply #13 on: April 10, 2020, 09:00:24 pm »
Glad you found the solution.  It's hard debugging an API you don't know in your head (but the basics still applied).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf